- Home /
Add onClick handler to button from editor script
I have set up an editor script to generate a bunch (700+) of buttons, which is working except for the part that adds the onClick handler. Here's the code I have so far:
// i is an int from the enclosing for loop
// temp is a RectTransform defined outside the loop
// container is the part of the scroll view which will contain all of the buttons
temp = Instantiate(prefab);
temp.SetParent(container, false);
AddListener(temp.GetComponent<Button>(), i);
void AddListener(Button b, int id)
{
b.onClick.AddListener(() => t.LoadInfo(id));
}
This works at runtime, but when running from a button in the editor the buttons are generated but the click handlers are not added. How do I get this to work?
Answer by fugogugo · Mar 02, 2016 at 06:44 AM
the method onClick.AddEventListener() will give non persistent event listener to the button which will not shown on inspector on edit mode
what you're looking for is UnityEventTools.AddPersistentListener
check it here http://forum.unity3d.com/threads/how-to-create-persistent-listener-to-an-event.264228/
Answer by Masterio · Mar 01, 2016 at 07:56 PM
why you are not using the http://docs.unity3d.com/ScriptReference/GUILayout.Button.html
Your answer

Follow this Question
Related Questions
Unity UI 4.6 - Programmatically adding events - EventTrigger.delegates is null 0 Answers
Creating a custom editor window with a grid 1 Answer
Color change of GUI button in custom property drawer is unresponsive 1 Answer
Looking for comprehensive resource for Legacy UI in Editor 1 Answer
Callback for when a Graphic (Ui) Component is Added to any GameObject using the Editor 0 Answers