Search Unity

How to handle MANY UI-buttons' OnMouseUp() in one .cs file? (u5.5+)

Discussion in 'Scripting' started by PizzaProgram, Apr 8, 2017.

  1. PizzaProgram

    PizzaProgram

    Joined:
    Feb 12, 2014
    Posts:
    17
    I would like to keep scene logic simple.
    Let's say I have a "statistics" scene, with 20 different UI-buttons.
    Each button needs an OnMouseUp() and Update() void, looks similar (but not the same) like this:
    Code (CSharp):
    1. public class Button_OK: MonoBehaviour {
    2.     void Update () {
    3.         if(Input.GetKeyUp(KeyCode.Return))
    4.             OnMouseUp();
    5.     }[
    6.     public void OnMouseUp() {
    7.        // ... do something
    8.    }
    9. }
    Is there any easy way to do this?
     
  2. PizzaProgram

    PizzaProgram

    Joined:
    Feb 12, 2014
    Posts:
    17
    The only way I've found to do it is creating and adding event system at runtime with this kind of complicated code:
    https://docs.unity3d.com/ScriptReference/EventSystems.EventTrigger.html
    Code (CSharp):
    1. public class Button_Cancel_Script : MonoBehaviour {
    2.     void Start()
    3.     {
    4.         EventTrigger trigger = GetComponent<EventTrigger>();
    5.         EventTrigger.Entry entry = new EventTrigger.Entry();
    6.         entry.eventID = EventTriggerType.PointerUp;
    7.         entry.callback.AddListener((data) => { OnPointerUp((PointerEventData)data); });
    8.         trigger.triggers.Add(entry);
    9.     }
    10.  
    11.     public void OnPointerUp(PointerEventData data)
    12.     {
    13.          // ... do something
    14.     }
    15.  
    16.     void Update ()
    17.     {
    18.         if(Input.GetKeyUp(KeyCode.Escape))
    19.         {
    20.             OnPointerUp(null);
    21.         }
    22.     }
    23. }
    ... and of course I also have to:
    • - define a gameObject for each button
    • - drag each one in the inspector
    • - add the corresponding script at Start
    Code (CSharp):
    1. public class Statistics_Script: MonoBehaviour {
    2.   public GameObject Cancel_Button;
    3.   ...
    4.   void Start() {
    5.      Cancel_Button.AddComponent<Button_Cancel_Script>();
    6.     ...
    7.   }
    8. }
    9. }
     
    Last edited: Apr 8, 2017
  3. PizzaProgram

    PizzaProgram

    Joined:
    Feb 12, 2014
    Posts:
    17
    Would be nice, if the same .cs file could be used for ALL UI buttons > just drag it on the inspector.
    But you can not choose a class named different than the .cs file itself. :(

    Any ideas how to simplify all these?
     
  4. JoshuaMcKenzie

    JoshuaMcKenzie

    Joined:
    Jun 20, 2015
    Posts:
    916
    Can you state your problem clearly? What you've said so far can easily be handled with copy/pasting and via multi-object editing in the editor.

    is this something that needs to be done dynamically? are the buttons generated at runtime? or are you simply looking for a single distributor script handling all the buttons?
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Yes, your explanation is slightly difficult to follow.
    It's worth noting, however, that in a UI, one wouldn't use OnMouseUp, but rather an IPointerClick (or Up) interface.
    Also, getting a keycode will not mean that it got it from that button.. but you did say that wasn't the exact code.. but still.