namespace Zinnia.Utility
{
using UnityEngine;
///
/// A container for an that implements an interface that can be utilized within a Unity Inspector.
///
public abstract class InterfaceContainer
{
///
/// The contained object.
///
[Tooltip("The contained object.")]
[SerializeField]
protected Object field;
///
/// The contained object.
///
protected Object Field
{
get
{
return field;
}
set
{
field = value;
if (Application.isPlaying)
{
OnAfterFieldChange();
}
}
}
///
/// Called after has been changed.
///
protected abstract void OnAfterFieldChange();
}
///
/// A container for a given interface type.
///
/// The interface type to contain.
public abstract class InterfaceContainer : InterfaceContainer, ISerializationCallbackReceiver
{
///
/// The contained interface.
///
public TInterface Interface
{
get
{
return _interface;
}
set
{
Object @object = value as Object;
if (@object == null)
{
_interface = value;
Field = null;
}
else
{
_interface = default;
Field = @object;
}
}
}
private TInterface _interface;
///
public void OnBeforeSerialize() { }
///
public void OnAfterDeserialize()
{
if (Field is TInterface @interface)
{
_interface = @interface;
}
else
{
field = null;
}
}
///
/// Called after has been changed.
///
protected override void OnAfterFieldChange()
{
if (Field is TInterface @interface)
{
_interface = @interface;
}
else
{
field = null;
}
}
}
}