- Home /
Defining a public variable with 'where'
I would like to define a public, user-assignable variable that is a Behaviour. The Behaviour will be a target script to call when certain conditions are true. This is the easy part
I also want to define a specific interface to implement, so I have some known public functions to call. However, if I just ask for the interface like so:
public EventReceiver EventController;
The user can't assign a MonoBehaviour from the editor - the only way to be able to assign is to use the following:
public Behaviour EventController;
What I'm wondering is, is there any way in C# to do something like this:
public Behaviour EventController where EventController : EventReceiver;
?
I know that I can check the type at runtime and deliver a message to the user if they've assigned an incorrect target - this is what I've already done. I'm just curious if there's a better way.
Answer by ScroodgeM · Aug 02, 2012 at 08:25 PM
you can check it in editor on assigning action instead of runtime, making incorrect assigning impossible.
http://docs.unity3d.com/Documentation/Components/gui-ExtendingEditor.html
'Making a Custom Editor' chapter
I figured someone would bring up a custom editor :). I know that's an option too, but it's a lot of work for such a $$anonymous$$or thing I'm trying to do. Still, if nobody has any better idea about how to do it directly in C#, I'll definitely give your answer the check mark.
ok. it's will be interesting for me too if there's another way. i didn't found it and currently using custom editors. with 'drawdefaulteditor' it's just a few lines more to achieve what you want.
Answer by whydoidoit · Aug 02, 2012 at 10:03 PM
Well you could make it a base class derived from MonoBehaviour - perhaps an abstract one, that would allow editor assignment - but not the query for an interface.
it can be a soulution in very simple case, but if you need more then one derive (what is usually so).... or you derived your class already from some abstract....