Thursday, July 28, 2022
HomeGame Developmentunity - Exposing record of interfaces to inspector utilizing Odin

unity – Exposing record of interfaces to inspector utilizing Odin


Use case: scriptable objects that comprise completely different algorithms however generate suitable information, that are used randomly in a procedural system. They implement an interface in order that the system may give them inputs and get outputs with out understanding the concrete kind, permitting a listing of kind MyInterface to comprise varied completely different algorithms.

I would like an uncovered record within the inspector {that a} designer can drag configured scriptable objects into, and the system shall be influenced by whichever objects are there.

It really works if I’ve an uncovered record of any scriptableObjects and manually guarantee all of them implement MyInterface, and loop by way of that record internally including them to aChecklist<MyInterFace> that the system can iterate by way of. Nonetheless Checklist<MyInterface> will not present up within the inspector.

I have been instructed Odin will enable me to serialize the Checklist<MyInterface> and entry immediately within the inspector.

Is that this potential? Full disclosure I do not perceive serialization properly and really presumably simply did not convey what I am making an attempt to do correctly.

The next code will work however the contents of the record won’t carry by way of to runtime, and each time I’ve to initialize the record within the inspector by deciding on Checklist<MyInterface> from a dropdown:

public class Odintest : MonoBehaviour
{
    [ShowInInspector] public Checklist<IMyInterface> InterfaceList;

    [Button("AccessAlgorithms")]
    public void AccessAlgorithms()
    {
        foreach (var v in InterfaceList)
        {
            print(v.MyMethod());
        }
    }
    
}

public interface IMyInterface
{
    public float MyVar { get; set; }
    public float MyMethod();
}

[CreateAssetMenu]
public partial class MySrciptableObject1 : ScriptableObject, IMyInterface
{
    public float param1;
    public float MyVar { get; set; }
    public float MyMethod()
    {
        // do one thing
        float result1 = 1;
        return result1;
    }
}

[CreateAssetMenu]
public class MySrciptableObject2 : ScriptableObject, IMyInterface
{
    public float param2;

    public float MyVar { get; set; }
    public float MyMethod()
    {
        // do somethign else
        float result1 = 1;
        return result1;
    }
}

I’ve learn by way of the docs on implementing the serializer however I can not work out apply that to an interface.

Thanks for any assist

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments