Exposing Interfaces in the inspector

Hi,

I’m working on a project where I want to dynamically change the way certain function calls behave.

basically it’s your typical Strategy patern where I have something like this:

class MyClass : MonoBehavior {

	public IBehavior myBehavior

	// class code

}

interface IBehavior {
	void DoSomething();
}

class BehaviorImpl1 : MonoBehavior , IBehavior {
	void DoSomething(){
		// one way of doing something
	}
}

class BehaviorImpl2 : MonoBehavior , IBehavior {
	void DoSomething(){
		// another way of doing something
	}
}

so all I need to do do change the implementation dynamically is to change which implementation I’m using by doing something like

myClassInstance.myBehavior = new BehaviorImpl2();

But what I’d like to be able to do, is to set, via the inspector, which implementation of behavior I have when my scene is loaded. But the inspector doesn’t expose myBehavior. I guess there’s a way to do this with a custom Inspector script, but I have no idea how to. Any ressource (or perhaps a piece of code) to enlighten my quest for encapsulation?

By the way, I don't know why I extended MonoBehavior in my IBehavior implementations. A reflex I guess :-) but ultimately they don't HAVE to be a MonoBehavior

consider creating an enum of behaviour type

1 Answer

1

Exposing/serializing interfaces, properties, abstract system objects, etc is what I’ve done in ShowEmAll, see this sample implementation if you’re interested.

I’m currently developing a new API for ShowEmAll that allows you to write drawers that you could apply easily on both fields, properties and even methods regardless of ‘where’ i.e. in a MonoBehaviour, regular Serializable class, an EditorWindow, etc. The new API will be strongly typed and have better support for attributes composition.

I currently have support for Dictionary drawing, will be adding other collection types such as Stack, Queue, etc. Here’s how the interface drawer looks like - it lets you choose either a System or Unity implementer…

I'll definitely have a look at that. Thanks, looks promissing :-)

@jeango if you could just hold your horses for a bit longer, I'm working on nesting as we speak - you can go nasty, nest anything in anything. [32928-nasty.png|32928]