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.