There are no custom steps to adding the package to your project. Just choose one of following installation methods:
-
Download ZIP of the repository and extract it to your project Assets folder.
-
Add package through Unity Package Manager by choosing "Add package from git URL..." option and then typing "https://github.com/Bipolar-Games/Interface-Serialization.git".
-
Add the Interfaces Serialization repository as submodule to your project git repository.
-
Use any other method you prefer.
Assuming you have a following interface:
public interface IMyInterface
{
void MyMethod();
}
You can implement it in Components:
using UnityEngine;
public class MyInterfaceComponent : MonoBehaviour, IMyInterface
{
public void MyMethod()
{
}
}
And also in ScriptableObjects:
using UnityEngine;
[CreateAssetMenu]
public class MyInterfaceScriptableObject : ScriptableObject, IMyInterface
{
public void MyMethod()
{
}
}
There are a few different ways to create field for the interface.
You can enclose your interface type with Serialized<>
generic type to create a serialized version of your interface.
using UnityEngine;
using Bipolar;
public class MyBehaviour : MonoBehaviour
{
[SerializeField]
private Serialized<IMyInterface> mySerializedInterface;
private void CallMyMethod()
{
mySerializedInterface.Value.MyMethod();
}
}
If you are going to reference your interface in many classes, you might find it more convenient to create your own serialized class of interface by inheriting Serialized<>
class.
using UnityEngine;
using Bipolar;
[System.Serializable]
public class MyInterface : Serialized<IMyInterface>, IMyInterface
{
public void MyMethod() => Value.MyMethod();
}
It requires some preparations, but it makes creation and usage of serialized interfaces simpler and more natural.
using UnityEngine;
public class MyBehaviour : MonoBehaviour
{
[SerializeField]
private MyInterface mySerializedInterface;
private void CallMyMethod()
{
mySerializedInterface.MyMethod();
}
}
If you prefer using an attribute instead of Serialized<>
class you can add the RequireInterface
attribute to your UnityEngine.Object
field. However this method requires casting your object every time you want to use a function of the interface.
using UnityEngine;
using Bipolar;
public class MyBehaviour : MonoBehaviour
{
[SerializeField, RequireInterface(typeof(IMyInterface))]
private Object mySerializedInterface;
private void CallMyMethod()
{
((IMyInterface)mySerializedInterface).MyMethod();
}
}
Regardless of the field declaration method, the correctly configured field of interface will be displayed as shown in the picture.
You can drag and drop into the field all objects implementing your interface, including:
- ScriptableObjects implementing the interface
- Components implementing the interface
- GameObjects containing such Component
- prefabs whose root GameObject contains such Component
You can also find the available objects in custom Object Selector window, which can be opened by pressing the default Object Selector Button. The Object Selector displays prefabs and ScriptableObjects under 'Assets' tab and Components in the scene under the 'Scene' tab.
To make creation of new objects implementing the interface more streamlined side interface buttons were introduced. They allow to quickly create new objects without having to search through "Assets/Create" and "Add Component" menus, and automatically add object reference to the field.
To show side buttons with the interface field you need to apply InterfaceButton
attribute to the field. The field must either inherit from Serialized<,>
or have RequiredInterface
attribute applied. The InterfaceButton
attribute constructor requires specifying which buttons should be shown with InterfaceButtonType
. Any combination of following types can be chosen:
AddComponent
- creates a button which adds a Component implementing the interface to the GameObject and assigns it to the interface fieldCreateAsset
- creates a button that creates a new instance of ScriptableObject implementing the interface and assigns it to the interface field
Chosing multiple types can be achieved by joining them with bitwise OR operator (|
).
For using both types of buttons Both
type can be also used.
[SerializeField, InterfaceButton(InterfaceButtonType.AddComponent)]
private Serialized<IMyInterface> mySerializedInterface;
Alternatively if you are using RequireInterface
attribute you can specify the buttons in the second argument of the attribute constructor.
[SerializeField, RequireInterface(typeof(IMyInterface), InterfaceButtonType.CreateAsset)]
private Object mySerializedInterface;
After applying the InterfaceButton
attribute to the field side buttons will be shown beside the field in the Inspector, as displayed in the picture below.
Upon pressing any of the side buttons objects browser will show up, from which you can choose Component type you want to add or ScriptableObject type you want to create. Pressing the type name will result in addition/creation of the specified object. Types in the browser are also hierarchically grouped according to the AddComponentMenu
attrbute for Components and CreateAssetMenu
attribute in case of ScriptableObjects.