Interfaces as public Inspector properties

Hey guys!

Have an idea here. I think it would be great to have possibility to make Inspector properties (public props) of Interface types. In this way, I would be able to create generic (more universal) components and bind them to any other components using interfaces. It would use some interface implementation and generic component will depend on interface and it’s implementation, and not on some MonoBehaviour script. Some sort of dependency injection.

What do you guys will say on this? Am I thinking of things that already are realized? Some hints in this case?

I agree that it’d be better if Unity supported this, even though I think not too many of Unity developers care for abstraction or even OOP that much.

The problem lies in the way Unity serialize component properties, and as far as I know there are mainly two ways to solve this problem: a) Use a custom serialization stragegy that supports interfaces (some assets exist which can do that) b) Use an actual DI framework.

Personally, I’m using the second option and it seems to be working quite well for my purpose.

1 Like

Hi, mysticfall! Thank you for answer.

DI framework seems to be solution with some flaws:

  • excessive heavyweight - there is opinions that using DI framework in your Unity project is something like building bench on top of chair, because Unity already have some tools of IoC principles, whose are more lightweight and can be exposed much easely
  • centralized dependency binding - in my opinion it’s not comfortable to bind components in one centralized place. making generic components as prefabs, saving them into my own component collection and using them by necessity seems to be more handy. I imagine this method as analagous to electronic circuiting where you have set of components with appropriate in/out interfaces (pinouts), which are in some way universal components. it’s easy to compose such type components into well-behaved consistent system, and by the needs easely make changes or refactor it.

Found this:

Seems this workaround is what I am looking for

Component-Based software design principles
“With regard to system-wide co-ordination, components communicate with each other via interfaces. When a component offers services to the rest of the system, it adopts a provided interface that specifies the services that other components can utilize, and how they can do so.”

“It claims that software components, like the idea of hardware components, used for example in telecommunications, can ultimately be made interchangeable and reliable.”

hey Unity engine developers, you had implemented Component-Based design framework without giving us possibility wire-up components via interfaces! :slight_smile:

3 Likes