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?

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/

why you are not using the Unity - Scripting API: GUILayout.Button