TylerTemp / SaintsField

A Unity Inspector extension tool focusing on script fields inspector enhancement
MIT License
180 stars 10 forks source link

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

Closed laurentopia closed 2 weeks ago

laurentopia commented 2 weeks ago

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?

laurentopia commented 2 weeks ago

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

TylerTemp commented 2 weeks ago

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

laurentopia commented 2 weeks ago

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.