TylerTemp/SaintsField

Is there a way to pick an interface in the inspector?

Closed this issue · 3 comments

if I do

public interface IPhysicsRelayReceiver
{
	 void OnTriggerEnterRelayed(Collider other);
	 void RegisterCollider(Collider col);
}
//relays attacking capsule, used by melee
public class MCPhysicsRelay : MonoBehaviour
{
	[SerializeReference]
	[RequireType(typeof(IPhysicsRelayReceiver))]
	public IPhysicsRelayReceiver receiver;
}

it doesn't display the drop bucket like other reference fields do, only a button.
image
Could you add a drag and drop area?

it's possible that it's a bug...
image

Hi,

AFAIK, an interface should either be non-UnityEngine.Object, or a UnityEngine.Object. they use two different mechanisms:

  1. The first one uses SerializeReference provided by Unity (which can not be used on a UnityEngine.Object),
  2. while the latter uses c#'s default serializetion process.

I don't know if there is a way to make the two use the same process. At this point, we can only do:

public interface IPhysicsRelayReceiver
{
    void OnTriggerEnterRelayed(Collider other);
    void RegisterCollider(Collider col);
}

public struct ExampleStructOfInterface : IPhysicsRelayReceiver
{
    public string DisplayName;

    public void OnTriggerEnterRelayed(Collider other)
    {
    }

    public void RegisterCollider(Collider col)
    {
    }
}

public class MCPhysicsRelay : MonoBehaviour, IPhysicsRelayReceiver
{

    // example of non-UnityEngine.Object
    [SerializeReference, ReferencePicker]
    public IPhysicsRelayReceiver nonUnityObject;

    // example of UnityEngine.Object
    public SaintsInterface<UnityEngine.Object, IPhysicsRelayReceiver> unityObject;

    public void OnTriggerEnterRelayed(Collider other)
    {
    }

    public void RegisterCollider(Collider col)
    {
    }
}

image

SaintsInterface seems to be the easiest to use with minor changes and more importantly it doesn't break when changing the class name like [SerializeReference] does.
I'll use that, or a few hidden vars.
Thanks again.