Skip to content

A simple implementation which allows for interface-type fields to be accepted in the Unity inspector.

License

Notifications You must be signed in to change notification settings

TheDudeFromCI/Unity-Interface-Support

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Unity Interface Support

Unity Interface Support is a small Unity package that adds an attribute for allowing interfaces to be used on MonoBehaviours in the Unity inspector. Interfaces are fully serialized and blend seamlessly with standard Unity workflows.


Summary

This tiny Unity package allows for a user to quickly and easily add interface support to the Unity inspector.

In modern programming, proper dependency injection is essential for scaling up a project. Sadly, Unity's serializer does not currently allow for an easy implementation of interface-based components. This plugin adds a quick-and-simple approach to allowing an interface to be used within your MonoBehavior safe to assign from the editor without any fears of serialization getting in the way, or adding components of the wrong type.

Usage

Using interfaces is as simple as adding an attribute, and writing a single property field.

Example:

// Here, we want to accept objects of the specific interface.
// The field should always be specified as a UnityEngine.Object type.
// We can now be promised that the object field will be correctly
// serialized exactly as expected. The field will always be of
// the requested interface type, or null if not assigned.
[SerializeField, InterfaceType(typeof(IMyInterface))]
private Object myObject;

// Optionally, adding a private property is recommended for
// automatically casting the object field to the interface.
private IMyInterface MyInterface => myObject as IMyInterface;


// ...

// If using the property field, you can reference the property
// directly and us it as the interface.
void Start()
{
    if (MyInterface != null)
        MyInterface.SayHi();
}

Arrays & Lists

Using collections is nearly identical, but each element in the array must be casted individually. The simplest way to do this is via System.Linq.

public IMyInterface[] MyInterfaceArray => myObjects.OfType<IMyInterface>().ToArray();
public IMyInterface[] MyInterfaceList => myObjects.OfType<IMyInterface>().ToList();

// For performance reasons, it's recommended that you cache your collection wherever possible,
// as you may not want this being calculated every time you reference this property.
IMyInterface[] _myInterfaceArray;

void Awake()
{
    _myInterfaceArray = MyInterfaceArray;
}

If you would like to avoid using Linq, you can make a quick getter and setter function.

[SerializeField, InterfaceType(typeof(IMyInterface))]
private Object[] myObjects;

public IMyInterface MyInterface(int index) => myObjects[index] as IMyInterface;
public IMyInterface MyInterface(int index, IMyInterface value) => myObjects[index] = value;

Namespace

The InterfaceType attribute exists under the WraithavenGames.UnityInterfaceSupport namespace.

If you're using an assembly definition, in same rare cares, the GUID of the assemblies may not load correctly when importing the package. This can be fixed by opening the UnityInterfaceSupport.Editor assembly definition file in the package directly and pointing the the GUID link back to the UnityInterfaceSupport runtime. This can be seen here.

Installation

You can install this package by using the following line in the Unity package manager:

"net.wraithavengames.unityinterfacesupport": "https://github.com/TheDudeFromCI/Unity-Interface-Support.git?path=/Packages/net.wraithavengames.unityinterfacesupport",

(Please see the Official Unity Documention for more information.)

This project can also be installed through OpenUPM, here.

Additional Information

You can find more information about using this Unity package by checking out the Documentation page. This page is also included in local installations within the UnityInterfaceSupport/Documentation~ folder in your Unity Packages directory.

Video Demo

https://www.youtube.com/watch?v=2xYdQgXGhe0

About

A simple implementation which allows for interface-type fields to be accepted in the Unity inspector.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages