Denis Steinman
Denis Steinman

Reputation: 7809

How to write event handler to setup listeners in the inspector?

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:

Button editor

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

Answers (1)

Immersive
Immersive

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

Related Questions