I would like to be able to add a public variable to my script that can accept a game object and then select a public function to execute. Basically, I want to do something exactly like this screenshot in my own custom script, but I’m not sure how to accomplish it:
This can be accomplished with UnityEvents. The usage is nice and simple too! There is a little bit of weirdness to deal with if the event takes parameters, though.
using UnityEngine;
using UnityEngine.Events;
public class ExecuteStuffOnClick : MonoBehaviour {
public UnityEvent myUnityEvent;
void Awake() {
if (myUnityEvent == null)
myUnityEvent = new UnityEvent();
}
public void OnMouseDown() {
myUnityEvent.Invoke();
}
}
Thanks, but it is not exactly what I need.
I need to do some custom property drawer, for functions(methods)
This bugged me too.
My solution is to use an interface.
Implement your function using the interface, than in your ‘sender’ use GetComponent();
or like I did, make your Inspector field take a Monobehavior instead of a GameObj for even more control.
In both cases you can write an custom Inspector and check if interface is present. If not, draw a messageBox and reject the dragged obj.
Has other advantages. You can communicate both ways (return values) and have more than one function.