Sign up ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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
share|improve this question

3 Answers 3

up vote 21 down vote accepted

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<EventTrigger>();

//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<BaseEventData> callback =
    new UnityEngine.Events.UnityAction<BaseEventData>(DropEventMethod);

//Add our callback to the listeners
entry.callback.AddListener(callback);

//Add the EventTrigger entry to the event trigger component
    eventTrigger.delegates.Add(entry);
share|improve this answer
    
It seems that when you dynamically add a handler it doesn't seem to appear in the inspector. Drawer still shows "Empty list" for the delegates – vexe Dec 6 '14 at 16:13

The word is that the delegate{} syntax found in my previous answer is obsolete, there is another way of doing this using lambda notation:

void buttonSetup(Button button) {
    //Remove the existing events
    button.onClick.RemoveAllListeners();
    //Add your new event using lambda notation
    button.onClick.AddListener (handleButton);
}

void handleButton() {
    Debug.Log("Button pressed!");
}

Or you can pass the button along to make things a little more dynamic:

void buttonSetup(Button button) {
    button.onClick.RemoveAllListeners();
    //Add your new event
    button.onClick.AddListener(() => handleButton(button));
}

void handleButton(Button b) {
    Debug.Log("Button '" + b.name + "' pressed!");
}
share|improve this answer
    
Keep in mind, this strategy is only for the OnClick event. In order to dynamically add other events, you need to follow the instructions in my other answer. – Byte56 Feb 21 at 3:22

How unity implements the UI is just like any other unity components. You add GameObject and attach UI components to it. Once you have the game object you can get the UI components from it and change the properties.

API reference for UI can be found under UnityEngine.UI namespace, API reference for BUtton is http://docs.unity3d.com/ScriptReference/UI.Button.html

See the post http://chikkooos.blogspot.jp/2015/03/new-ui-implementation-using-c-scripts.html for more information on accessing UI from C# scripts

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.