- Home /
Question by
ahm8d2010 · Jun 26, 2017 at 08:31 PM ·
unity 5eventsystemeventlistener
How to add external method from class to Unity3dEvents AddLisitener()?
Hello,
I've two inquires :
1- I'm trying to add listener for external method to Unity3D .. but don't know how to pass the external method to addLisitener
method .. i know only the method for passing local method .. i'm trying something like that :
public class DoorEvent : MonoBehaviour {
public void opencloseDoor(string password){
if (password == "123") {
GetComponent<Animator> ().SetBool ("isOpen", !GetComponent<Animator> ().GetBool ("isOpen"));
}
}
}
and
public class Person : MonoBehaviour {
public PersonEvents ev;
public string password;
[Serializable]
public class PersonEvents:UnityEvent<string>
{
};
void Awake(){
// I'm trying here
ev.AddListener (new DoorEvent().opencloseDoor);
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.Space)) {
ev.Invoke (password);
}
}
}
but it's not working ??? how can i do that ?
2- I've read that UnityEvents is slower ~38 times than C# delegates in the performance , is that true ? and when i use UnityEvents and when no ?
Comment
Answer by mwilli12 · Jun 28, 2017 at 09:34 PM
I believe you have to find the listener on the Game Object first by calling it out. something like this.
Private GameObject listener;
void Awake () {
listener = GameObject.GetComponent<Listener> ();
ev.AddListener (new DoorEvent().opencloseDoor);
}