Unity3d 统一给结构体赋值
1、结构体内的所有的属性一致
效果:
原本:
改变后:
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using Newtonsoft.Json;
using System;
using System.Reflection;
public class Test : MonoBehaviour
{
public struct TestGob
{
public GameObject gob1;
public GameObject gob2;
}
public TestGob testGob;
void Init()
{
Type type = typeof(TestGob);
FieldInfo[] fields = type.GetFields();
object ob = testGob;
foreach (FieldInfo tgob in fields)
{
if (tgob != null)
{
Debug.Log(tgob.Name);
if (tgob.Name.Contains("1"))
tgob.SetValue(ob, GameObject.Find("Canvas"));//GameObject.Find("Canvas") 为要赋值的物体
}
}
testGob = (TestGob)ob;
}
}