Paul Masri-Stone
Paul Masri-Stone

Reputation: 3139

How to work around Unity not displaying interfaces in the Inspector?

If have an interface, say...

public interface ITool
{
    void Use();
}

and I have a MonoBehaviour class that implements the interface, say...

public class Equipment : MonoBehaviour, ITool
{
    ...

    public void Use()
    {
        ...
    }
}

and I want to reference the interface in another MonoBehaviour class, say...

public class Player : MonoBehaviour
{
    public ITool tool;
    ...

    void Update()
    {
        ...
        tool.Use();
        ...
    }
}

then I find that the inspector in Unity, looking at Player doesn't show tool and so I cannot drag a reference from some object that has an ITool component.

From searching, I can see people saying that interfaces aren't supported in the Unity inspector and that people have found ways of avoiding interfaces.

What ways?

Without creating a custom wrapper class for each interface, such as this one, how can I retain the benefits of interface classes and keep my scripts decoupled? (i.e. I don't want to have to put public Equipment tool in Player as Player shouldn't have to know about the Equipment script... and should also let me reference a different type of ITool.)

Upvotes: 10

Views: 8418

Answers (1)

derHugo
derHugo

Reputation: 90659

depending on your exact use case maybe [SerializeReference] already might solve your issue.

Beyond that unfortunately there is no easy straight forward way. A lot of people already did this via custom property drawers / Editor scripts though. A full implementation would be a bit too broad IMO.


However, - again depending on your actual use case - a complete alternative to interfaces might be a common base class like

public abstract class BaseTool : MonoBehaviour 
{ 
    public abstract void Use(); 
} 

and then

public class Equipment : BaseTool 
{ 
    public override void Use()
    { 
        ...
    }
} 

and finally in player use a

 public BaseTool tool;

This is basically exactly the way Unity does it with e.g. Collider or Renderer or Texture which are abstract base classes for many different child types.

Upvotes: 2

Related Questions