SerializedObject遍历

# 数据类

[Serializable]
public class Test
{
    public float f;
    public Vector2 v2;
}

[CreateAssetMenu]
public class TestAsset : ScriptableObject
{
    [HideInInspector]
    [SerializeField]
    public int a;
    public Vector3 v3;
    public Test test;
}

 

1-1) NextVisible(false): enterChildren=false的情况

public class NewBehaviourScript : MonoBehaviour
{
    public TestAsset testAsset;

    void Start()
    {
        SerializedObject so = new SerializedObject(this.testAsset);
        var itr = so.GetIterator();
        itr.NextVisible(true); //第一次必须用true
        do
        {
            Debug.Log($"Property: {itr.propertyPath}, {itr.name}, depth:{itr.depth}, type:{itr.propertyType}, {itr.isExpanded}");
        }
        while (itr.NextVisible(false));
    }
}

运行结果:[HideInInspector]的不会显示

 

1-2) NextVisible(true): enterChildren=true的情况

public class NewBehaviourScript : MonoBehaviour
{
    public TestAsset testAsset;

    void Start()
    {
        SerializedObject so = new SerializedObject(this.testAsset);
        var itr = so.GetIterator();
        itr.NextVisible(true); //第一次必须用true
        do
        {
            Debug.Log($"Property: {itr.propertyPath}, {itr.name}, depth:{itr.depth}, type:{itr.propertyType}, {itr.isExpanded}");
        }
        while (itr.NextVisible(true));
    }
}

运行结果:遇到struct或class会遍历其成员

 

 2-1) Next(false): enterChildren=false

public class NewBehaviourScript : MonoBehaviour
{
    public TestAsset testAsset;

    void Start()
    {
        SerializedObject so = new SerializedObject(this.testAsset);
        var itr = so.GetIterator();
        itr.Next(true); //第一次必须用true
        do
        {
            Debug.Log($"Property: {itr.propertyPath}, {itr.name}, depth:{itr.depth}, type:{itr.propertyType}, {itr.isExpanded}");
        }
        while (itr.Next(false));
    }
}

运行结果:会把所有序列化成员显示出来

 

2-2) Next(true): enterChildren=true

public class NewBehaviourScript : MonoBehaviour
{
    public TestAsset testAsset;

    void Start()
    {
        SerializedObject so = new SerializedObject(this.testAsset);
        var itr = so.GetIterator();
        itr.Next(true); //第一次必须用true
        do
        {
            Debug.Log($"Property: {itr.propertyPath}, {itr.name}, depth:{itr.depth}, type:{itr.propertyType}, {itr.isExpanded}");
        }
        while (itr.Next(true));
    }
}

运行结果(太多了,没全部显示)

 

参考

Unity插件开发:SerializedObject/SerializedProperty——查找引用的资源 - CodeGize - 博客园 (cnblogs.com)

Unity编辑器扩展——SerializedObject_Hello Bug.的博客-CSDN博客_unity serializedobject

Unity Inspector用脚本修改不触发保存~~2018.3_刘培玉--大王的博客-CSDN博客_unity脚本修改无法保存

posted @ 2022-12-30 21:34  yanghui01  阅读(88)  评论(0编辑  收藏  举报