How to add Persistent Listener to Button.onClick in Unity Script Editor

I am trying to do a simple thing:

  • Create new GameObject
  • Add a component Buttonto the gameObject.
  • Add a persistent listener to the onClick event.

The method I'm trying to register is in another script. Here is the code snippet I'm trying to do:

MyScript myScriptInstance = FindObjectOfType<MyScript>(); 
var go = new GameObject();
var btn = go.AddComponent<Button>();

var targetinfo = UnityEvent.GetValidMethodInfo(myScriptInstance,
"OnButtonClick", new Type[]{typeof(GameObject)});

var action = (UnityAction) Delegate.CreateDelegate(typeof(UnityAction),go, targetinfo, false);
UnityEventTools.AddPersistentListener(btn.onClick, action);

MyScript.cs looks like this:

public class MyScript : MonoBehaviour
{
    public void OnButtonClick(GameObject sender)
    {
        // do some stuff here.
    }
}

When I run this code, the Onclick buttons to listen for empty ones:

enter image description here

If I changed the line

var action = (UnityAction) Delegate.CreateDelegate(typeof(UnityAction),
go, targetinfo, false);

to

var action = (UnityAction) Delegate.CreateDelegate(typeof(UnityAction),
go, targetinfo, true);

I get:

ArgumentException: mismatch of the argument length of the System.Delegate.CreateDelegate method (Type System.Type, System.Object method firstArgument, System.Reflection.MethodInfo, Boolean throwOnBindFailure)

I followed these instructions , but I don’t know what is wrong here.

Any help is really appreciated.

+4
2

, , . Mono.

:

1, 2, 3   4

Unity . , Mono, Mono.

, :

1. AddObjectPersistentListener UnityAction , Delegate.CreateDelegate.

MyScript myScriptInstance = FindObjectOfType<MyScript>();
var go = new GameObject();
var btn = go.AddComponent<Button>();

var targetinfo = UnityEvent.GetValidMethodInfo(myScriptInstance,
"OnButtonClick", new Type[] { typeof(GameObject) });

UnityAction<GameObject> action = Delegate.CreateDelegate(typeof(UnityAction<GameObject>), myScriptInstance, targetinfo, false) as UnityAction<GameObject>;

UnityEventTools.AddObjectPersistentListener<GameObject>(btn.onClick, action, go);

2. Delegate.CreateDelegate . AddObjectPersistentListener.

MyScript myScriptInstance = FindObjectOfType<MyScript>();
var go = new GameObject();
var btn = go.AddComponent<Button>();

UnityAction<GameObject> action = new UnityAction<GameObject>(myScriptInstance.OnButtonClick);
UnityEventTools.AddObjectPersistentListener<GameObject>(btn.onClick, action, go);

, :

enter image description here

. . .

, , , , .

+2

, , script onClick, ( ).

, :

// Use this for initialization
void Start () {

    MyScript myScriptInstance = FindObjectOfType<MyScript> ();

    GameObject go = DefaultControls.CreateButton (new DefaultControls.Resources());
    var btn = go.GetComponent<Button> ();

    btn.onClick.AddListener (myScriptInstance.TestMethod);
}

MyScript.TestMethod:

public void TestMethod ()
{
    Debug.Log ("TestMethod");   
}

:

  • GameObject Button. , RectTransform Canvas .
  • , (. ). , .
  • : TestMethod I, , . , Button - onClick. , , .
0

Source: https://habr.com/ru/post/1661054/


All Articles