IExposedPropertyTable与ExposedReference的使用

近期回顾Dead of the Book demo时,看见了它们的运用。感觉主要是用于ScriptableObject资源和Scene资源解耦;

并将这类做法规范化。

 

做一个小测试,IExposedPropertyTable做容器,存放具体目标对象:

public class ExposeTableObj : MonoBehaviour, IExposedPropertyTable
{
    [System.Serializable]
    public struct Info
    {
        public PropertyName id;
        public Object value;
    }

    [SerializeField] private Info[] infos;


    public void ClearReferenceValue(PropertyName id)
    {
        infos = System.Array.Empty<Info>();
    }

    public Object GetReferenceValue(PropertyName id, out bool idValid)
    {
        idValid = false;

        for (int i = 0; i < infos.Length; i++)
        {
            var info = infos[i];
            if (info.id == id)
            {
                idValid = true;
                return info.value;
            }
        }

        return null;
    }

    public void SetReferenceValue(PropertyName id, Object value)
    {
    }
}

 

使用者:

public class ExposeTableUser : MonoBehaviour
{public ExposeTableObj exposeTable;


    private void Start()
    {
        ExposedReference<GameObject> myReference = new ExposedReference<GameObject>();
        myReference.exposedName = "myGoObj";
        GameObject go = myReference.Resolve(exposeTable);
        Debug.Log(go);
    }
}

这里直接动态创建了myReference,如果作为成员字段也可以被序列化,但是unity不会初始化匹配的exposedName。

然后在Start里,通过exposedName匹配了对应的对象。Unity用Guid作为exposedName,当然这个测试用了常规字符串也没问题。

 

面板配置:

 

posted @ 2022-01-17 21:05  HONT  阅读(545)  评论(0编辑  收藏  举报