Search Unity

Unity UI [Solved] Trying to make it so a player cannot move the cursor too much when clicking inside a button

Discussion in 'UGUI & TextMesh Pro' started by Deleted User, Apr 19, 2017.

  1. Deleted User

    Deleted User

    Guest

    The code:

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityEngine.EventSystems;
    4. using UnityEngine.UI;
    5.  
    6. namespace MyUI
    7. {
    8.     [RequireComponent(typeof(Button))]
    9.     public sealed class FixedButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
    10.     {
    11.         [SerializeField] private float _sensitivity;
    12.         [NonSerialized] private Vector2 _initialPointerPosition;
    13.  
    14.         void IPointerDownHandler.OnPointerDown(PointerEventData eventData)
    15.         {
    16.             _initialPointerPosition = eventData.position;
    17.         }
    18.  
    19.         void IPointerUpHandler.OnPointerUp(PointerEventData eventData)
    20.         {
    21.             var distance = Vector2.Distance(_initialPointerPosition, eventData.position);
    22.             Debug.LogFormat("Distance is {0}.", distance);
    23.             if (distance > _sensitivity)
    24.             {
    25.                 eventData.Reset();
    26.             }
    27.         }
    28.     }
    29. }
    What it should do is check to see how much the player has moved the cursor / finger when tapping / clicking and if he moved it too much, don't do anything but it's not working.
     
    Last edited by a moderator: Apr 20, 2017
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I had to modify your code slightly : changed 'Behaviour' to MonoBehaviour, Log to "Debug.Log" and re-wrote the Debug.Log.
    However, other than that , this is printing the distance for me at least.
    How much of the script is "Not working" ?
    If everything is working, except that it still activates the button, I want to ask if you're using a UnityEvent inside the button?
    If you are, maybe just remove that, and add the event/method call inside this, only when the distance <= _sensitivity.
     
  3. Deleted User

    Deleted User

    Guest

    Yes, the OnPointerUp method gets called fine. The problem is that the button still activates. I am using the default Unity button and that includes the UnityEvent I suppose but I can't modify the code for it.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, it's a bit odd that you don't want to allow movement beyond a certain distance on a button. However, since you want to try it...
    try adding a UnityEvent to the code you already have and removing the events from the regular button's OnClick area.
    Then, in your own script, after you've checked the distance, you can decide to call the event yourself (or not) based on your distance criteria :)
     
  5. Deleted User

    Deleted User

    Guest

    Don't really like this solution because my buttons will also have another script attached to them "TimeLimitButton" that makes it you can't click it for more than a certain length of time.
    Also seems like it would make things more confusing. Which script do I add my actions to in the Editor? The button or the button modifier scripts? Seems confusing for the designer.
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, another option I thought of mentioning before was to derive a new class from Button and override what you need there to accomplish your task.
    Gotta be straightforward to ask this time, out of curiousity: Why have a button, in which you want to only click part of it? :)
    One does not normally consider a button to have a certain limit on where you can click.
     
  7. Deleted User

    Deleted User

    Guest

    Well one can click the button anywhere but once clicked they can't move the cursor. It's for gameplay purposes and not really UI purposes.
    I just looked at the code for button and there's no method that are overrideable. :(
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I understand (kinda). I guess you didn't really describe the scenario, because in the back of my mind I wonder if there are other ways. However, it's your game :)
    You looked at the code? I opened it and looked right now, and it's there ;)
    Try again, and if you don't spot it, I can tell ya :)
     
    Deleted User likes this.
  9. Deleted User

    Deleted User

    Guest

  10. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Oh, that's cool. :) I wasn't even aware of that off the top of my head. Glad you got it fixed.
    Btw, there was a virtual void OnPointerClick in the Button source, which calls a private "Press" method, or something like that. You could have overriden that one (and submit is virtual, too) :)
    Regardless, this is even cooler for your purposes. Cheers.