Reputation: 7809
I need to write a script that will call methods in another scripts on key events. How to write it to I could specify event type (i.e. using custom enum
), game object, script, method and arguments.
In short I need to realize similar functionality as for Button
script:
For example I choose an event Escape, a game object Main camera, my another script that attached to this game object GameManager, method Exit and its arguments (there aren't arguments in Exit method but it can be in others).
Is it possible to make so in the custom script?
Upvotes: 0
Views: 692
Reputation: 1704
What you're looking at there is a class field of type UnityEvent called OnClick. Here's the code for it:
public class ClickEvent : MonoBehaviour {
public UnityEngine.Events.UnityEvent OnClick;
public void Update() {
if (Input.GetMouseButtonDown(0))
OnClick.Invoke();
}
}
Upvotes: 2