Domanda

The latest versions of NGUI have this great tool that lets you pick which function within any of the target's scripts will be called when you click on it.

Basically, it's a select box within the inspector that gets automatically filled with all the functions from all the scripts attached to the game object.

How can I generate a function list that fills up automatically like this?

I don't want to have to maintain an enum with all the possible functions (including some that the current object might not have)

I tried looking at the code NGUI used, but it was a bit too complicated for me to understand right now.

È stato utile?

Soluzione

If you want to do it like in NGUI then use the tools that are available within NGUI itself and define a public variable like this:

public List<EventDelegate> DelegateList = new List<EventDelegate>();

With this, you can drop MonoBehaviour scripts in the Inspector field and can then select public methods/delegates contained in that script.

You can then invoke them like this:

void Start() {
  EventDelegate.Execute(DelegateList);
}

Now every method in your delegate list will be invoked. You can see this for example in the UIButton script where this is used to handle the OnClick delegates.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top