白泽图

  • 文章
    • Unity渲染
    • Unity项目开发
    • 工具
    • 数学
    • 算法
    • 网站搭建
    • 网络&操作系统
蒋程个人博客
互联网技术经验总结&分享
  1. 首页
  2. Unity项目开发
  3. 正文

在Unity中通过反射获取SerializedProperty的值

2024-04-10 738点热度 10人点赞 0条评论

写编辑器遇到需要通过SerializedObject的SerializedProperty获取其对应的对象(比如需要调用其方法),以下封装一个统一方法,便于以后直接使用

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using System;
using System.Reflection;


public class ReflectPropertyPath : MonoBehaviour
{

    [Serializable]
    public class TestObject
    {
        [Serializable]
        public class ElementLayer1
        {
            [Serializable]
            public class ElementLayer2
            {
                [SerializeField]
                int intData;
            }

            [SerializeField]
            List<ElementLayer2> listData;


            [SerializeField]
            ElementLayer2[] arrayData;

            [SerializeField]
            string stringData;
        }

        [SerializeField]
        ElementLayer1 layer1Data;
    }


    [SerializeField]
    TestObject m_TestObject;


    [ContextMenu("Run")]
    void Run()
    {
        SerializedObject serializObject = new SerializedObject(this);
        var testObjectProp = serializObject.FindProperty("m_TestObject");
        var layer1DataProp = testObjectProp.FindPropertyRelative("layer1Data");

        var stringDataProp = layer1DataProp.FindPropertyRelative("stringData");
        Debug.Log(stringDataProp.propertyPath);
        Debug.Log(GetSerializedPropertyValue(stringDataProp));

        var listDataElementProp0 = layer1DataProp.FindPropertyRelative("listData").GetArrayElementAtIndex(0).FindPropertyRelative("intData");
        Debug.Log(listDataElementProp0.propertyPath);
        Debug.Log(GetSerializedPropertyValue(listDataElementProp0));

        var arrayDataElementProp0 = layer1DataProp.FindPropertyRelative("arrayData").GetArrayElementAtIndex(1).FindPropertyRelative("intData");
        Debug.Log(arrayDataElementProp0.propertyPath);
        Debug.Log(GetSerializedPropertyValue(arrayDataElementProp0));
    }



    System.Object GetSerializedPropertyValue(SerializedProperty property)
    { 
        if(property == null)
            return null;
        return GetSerializedPropertyValue(property.serializedObject, property.propertyPath);
    }


    System.Object GetSerializedPropertyValue(SerializedObject serializedObject, string propertyPath)
    {
        System.Object tempObject = serializedObject.targetObject;
        var splitPaths = propertyPath.Split('.');
        Array array = null;
        for (int i = 0; i < splitPaths.Length; i++)
        {
            var splitPath = splitPaths[i];
            if (splitPath == "Array")
            {
                var arrayType = tempObject.GetType();
                if (arrayType.IsArray)
                {
                    array = (Array)tempObject;
                }
                else
                {
                    if (arrayType.Name.StartsWith("List"))
                    {
                        var _itemsField = arrayType.GetField("_items", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                        tempObject = _itemsField.GetValue(tempObject);
                        array = (Array)tempObject;
                    }
                }
            }
            else if (i > 0 && splitPaths[i - 1] == "Array" && splitPath.StartsWith("data["))
            {
                var arrayIndex = int.Parse(splitPath.Replace("data[", "").Replace("]", ""));
                tempObject = array.GetValue(arrayIndex);
            }
            else
            {
                var propField = tempObject.GetType().GetField(splitPath, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                tempObject = propField.GetValue(tempObject);
            }
        }
        return tempObject;
    }




}

效果如下

标签: 暂无
最后更新:2024-04-10

蒋程

这个人很懒,什么都没留下

点赞
< 上一篇

文章评论

您需要 登录 之后才可以评论

COPYRIGHT © 2023 白泽图. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

登录
注册|忘记密码?