Saturday, November 26, 2016

c# - Unity new UI - Dynamically change the functions called by GUI elements


Looking to dynamically change the value in the button OnClick and slider On Value Changed example 1 example 2.


If I had to guess on how this was done, get a reference to the button/gui element. Next get the Button script component and access that list of function calls. However I'm not sure how to modify that list. Anyone have any ideas?


More Details that might help:




  • The function being called is apart of a custom script component

  • Using the 'dynamic' variable option for the sliders

  • Could be used to create dynamic buttons



Answer



There's a super simple way to change events:


EDIT


See my other answer for the quick and easy way to add an event for the OnClick event only. For other events, like OnDrag see below.





Additionally, if you need more than just the events provided by default, I'd suggest instead attaching a EventTrigger to your game object. This gives us access to the BaseEventData object returned from the event, telling us stuff like the object that created the event. Then you can do something like:


//Create an event delegate that will be used for creating methods that respond to events
public delegate void EventDelegate(UnityEngine.EventSystems.BaseEventData baseEvent);

Then we can create a method for handling events, the signature must match that of our delegate. So, it needs to return void and accept BaseEventData as its first and only parameter:


public void DropEventMethod(UnityEngine.EventSystems.BaseEventData baseEvent) {
Debug.Log(baseEvent.selectedObject.name + " triggered an event!");
//baseEvent.selectedObject is the GameObject that triggered the event,
// so we can access its components, destroy it, or do whatever.
}


Finally, to dynamically add the event:


//Get the event trigger attached to the UI object
EventTrigger eventTrigger = buttonObject.GetComponent();

//Create a new entry. This entry will describe the kind of event we're looking for
// and how to respond to it
EventTrigger.Entry entry = new EventTrigger.Entry();

//This event will respond to a drop event

entry.eventID = EventTriggerType.Drop;

//Create a new trigger to hold our callback methods
entry.callback = new EventTrigger.TriggerEvent();

//Create a new UnityAction, it contains our DropEventMethod delegate to respond to events
UnityEngine.Events.UnityAction callback =
new UnityEngine.Events.UnityAction(DropEventMethod);

//Add our callback to the listeners

entry.callback.AddListener(callback);

//Add the EventTrigger entry to the event trigger component
eventTrigger.delegates.Add(entry);

If you're using version 5.3.3 or above, use this line instead instead of the last line above, delegates is depreciated:


eventTrigger.triggers.Add(entry); 

No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...