赞
踩
1 创建一个基类Demo ,然后在 子类,创建不同的变量类型 ,然后在同一个物体下可以赋值两个共有的变量,效果如下
,
using System;
using UnityEngine;
using System.Reflection;
public class CharacterEditor : MonoBehaviour {
public int health;
public string name;
public GameObject shirt;
public GameObject pants;
private static CharacterEditor characterCopyFrom;
private static FieldInfo[] fileToCopy;
[ContextMenu("Copy With Reflection")]
public void CopyWithReflection()
{
characterCopyFrom = this;
Type characterType = typeof(CharacterEditor);
FieldInfo[] charcterFileds = characterType.GetFields(BindingFlags.Public | BindingFlags.Instance);
fileToCopy = charcterFileds;
}
[ContextMenu("Paste With Reflection")]
public void PasteWithReflection()
{
foreach (FieldInfo field in fileToCopy)
{
object value = field.GetValue(characterCopyFrom);
field.SetValue(this,value);
}
}
}
using UnityEngine;
using System.Collections;
public class FirstCharacter : CharacterEditor {
public GameObject[] other;
}
using UnityEngine;
using System.Collections;
public class SecondCharacter : CharacterEditor {
//不同的属性
public int[] Origin;
}
放在同一个物体上点击同一个物体的”Copy With Reflection” 以及:”Paste With Reflection” 就可以直接从copy赋值过去,而不用一个一个赋值,但是不是同一个父类的物体不会被赋值。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。