Unity3D反射实现状态返回

  最近做项目需要状态返回,感觉用反射的话会比较轻松。于是用反射获取Gameobject身上所有组件的属性与字段,下面放代码。

获取一个类内部的所有属性与字段

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

/// 
/// 类对象的信息
/// 
public class ObjInfoClass
{
   private Dictionary<_PropertyInfo, object> _PropertyInfoDic = new Dictionary<_PropertyInfo, object>();//所有属性以及值
   private Dictionary<_FieldInfo, object> _FieldInfoDic = new Dictionary<_FieldInfo, object>();//所有字段以及值

    private object mainObj;//主要对象

    public ObjInfoClass(object obj)
    {
        this.mainObj = obj;
    }

    public void GetAllPropertyAndField() //获取所有属性字段
    {
        PropertyInfo[] propertyArray = mainObj.GetType().GetProperties();
        foreach (var item in propertyArray)
        {
            try
            {
                var value = item.GetValue(mainObj, null);
                _PropertyInfoDic.Add(item, value);
            }
            catch (Exception)
            {

            }
         
        }
        FieldInfo[] fieldArray = mainObj.GetType().GetFields();
        foreach (var item in fieldArray)
        {
            try
            {
                var value = item.GetValue(mainObj);
                _FieldInfoDic.Add(item, value);
            }
            catch (Exception)
            {

            }
         
        }

    }

    public void SetAllMember()//设置所有的成员
    {
        foreach (KeyValuePair<_PropertyInfo, object> item in _PropertyInfoDic)
        {
            try
            {
            item.Key.SetValue(mainObj, item.Value, null);

            }
            catch (Exception)
            {

            }
        }
        foreach (KeyValuePair<_FieldInfo, object> item in _FieldInfoDic)
        {
            try
            {
            item.Key.SetValue(mainObj, item.Value);

            }
            catch (Exception)
            {

            }
        }
    }

}

获取Gameobject脚本信息,以及自身active属性

/// 
/// Game object对象的所有组件的信息
/// 
public class GameobjectInfoClass
{
    private GameObject MainObj;//主要Gameobject

    public GameobjectInfoClass(GameObject go)
    {
        MainObj = go;
    }

    ObjInfoClass[] objInfoArray;//所有组件信息数组
    Component[] componentArray;//所有组件

    /// 
    /// 获取所有组件
    /// 
    public void GetAllComponent()
    {
        componentArray = MainObj.GetComponents();
    }
    /// 
    /// 获取所有组件信息
    /// 
    public void GetAllInfo()
    {
        if (componentArray.Length==0)
        {
            GetAllComponent();
        }
        objInfoArray = new ObjInfoClass[componentArray.Length+1];
        for (int i = 0; i < componentArray.Length; i++)
        {
            try
            {
            objInfoArray[i] = new ObjInfoClass(componentArray[i]);
                objInfoArray[i].GetAllPropertyAndField();

            }
            catch (Exception)
            {

            }
        }
        objInfoArray[componentArray.Length] = new ObjInfoClass(MainObj);//gameobject自身的信息
        objInfoArray[componentArray.Length].GetAllPropertyAndField();
    }
    /// 
    /// 设置所有组件信息
    /// 
    public void SetAllInfo()
    {
        foreach (var item in objInfoArray)
        {
            try
            {

            item.SetAllMember();
            }
            catch (Exception)
            {

            }
        }
    }
}
应用脚本

public class test : MonoBehaviour {

        public GameObject obj;
        private GameobjectInfoClass gameInfo ;
        [ContextMenu("获取信息")]
        public void GetInfo()
        {
            gameInfo = new GameobjectInfoClass(obj);
            gameInfo.GetAllComponent();
            gameInfo.GetAllInfo();

        }

        [ContextMenu("设置信息")]
        public void SetInfo()
        {
            gameInfo.SetAllInfo();
        }
    }


Unity3D反射实现状态返回_第1张图片

拿这个摄像机做例子,先存储信息。

Unity3D反射实现状态返回_第2张图片

对面板上的参数随意更改

Unity3D反射实现状态返回_第3张图片

再设置信息


再看这次的面板

Unity3D反射实现状态返回_第4张图片

是不是又一样了,复制的?不存在的

你可能感兴趣的:(Unity3d)