Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Get Persistent Method Name(int index);

Discussion in 'Scripting' started by eyalchess, Oct 7, 2016.

  1. eyalchess

    eyalchess

    Joined:
    Feb 18, 2016
    Posts:
    30
    How do i use Get Persistent Method Name(int index);
     
  2. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    It looks like most of those functions are now internal use only or deprectated. In Unity 5.40f3 the only valid I can use is:
    • GetValidMethodInfo(Object obj, string name, System.Type[] arguments)
    So if you know that ButtonOne had a callback function on it called ButtonPressed(int) you could do this:
    Code (CSharp):
    1. delegate void HandlerDelegate(int value);
    2. GameObject button;  // assign this in editor, or use FindObject
    3.  
    4. void SomeFunction()
    5. {
    6.        System.Type[] arguments = new System.Type[1];
    7.        arguments[0] = typeof(int);
    8.        HandlerDelegate myHandle = UnityEventBase.GetValidMethodInfo(button, "ButtonPressed",arguments);
    9. }
    I haven't actually tested if this works but seems like it would. Why do you want to use GetPersistentMethodName?
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
  4. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    I realize the documentation lists quite a few functions under UnityEventBase GetPersistentMethodName being one of them. However (at least using 5.4.0f3) if i tried to use UnityEvent, or UnityEventBase the only exposed method is the one I listed above:
    • GetValidMethodInfo
    I was unable to get access to GetPersistentMethodName. If you can I'd like to know what using, or subclass you used to get access to it.
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Hmm, I was just going by the docs, it's odd for them to be different to actual Unity.

    It may be an editor only function?
     
  6. Kalladystine

    Kalladystine

    Joined:
    Jan 12, 2015
    Posts:
    227
    Interesting. I'm using 5.4.1f1 and I can use them just fine.
    Please note that these are member methods, so you need to have an object of UnityEvent (or UnityEventBase) type.

    Example:
    Code (CSharp):
    1. public class MyClassWithEvent : MonoBehaviour
    2. {
    3.     public UnityEvent MyEvent;
    4.  
    5.     void Update()
    6.     {
    7.         if (MyEvent.GetPersistentEventCount() == 1 && MyEvent.GetPersistentMethodName(0) == "Firestrike")
    8.         {
    9.             MyEvent.Invoke();
    10.         }
    11.     }
    12. }
    Above code checks that:
    1. MyEvent has exactly one Persistent Listener (added on Inspector, not on runtime).
    2. Method that is subscribed as the first persistent listener is Firestrike
    3. If yes, fires it every frame.

    I don't see much use for it, or I'm misinterpreting it. Haven't really used it anywhere in practice, as I can't see a use case for it.
     
    Kiwasi and takatok like this.
  7. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Ah I tried to instantiate a UnityBaseEvent and got an error, Didn't try to instantiate a UnityEvent. So I gueuss GetValidMethodInfo must be a static event you can access from the just the Class itself without needing an object. The rest of them of course need an instantiated UnityEvent.

    Neither could I which is why I asked the OP why he wanted to use this? There maybe something he's trying to do with this, that is done in a much better way.