using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UIFrame;namespace TankGame {
public class Tank {
}
public class Bullet {}
public class Enemy {}
}public class ReflectionType : MonoBehaviour {
private void Start()
{
#region 获取类型-Type
通过类名获取类别的方式 typeof
//Type personType = typeof(Person);
//Debug.Log("personType:"+personType);
//Type tankType = typeof(TankGame.Tank);
//Type gameObjectType = typeof(GameObject);
通过对象,获取类别GetType()
//Type transformType = transform.GetType();
有一个炮弹对象
//TankGame.Bullet blt = new TankGame.Bullet();
//Type BulletType = blt.GetType();
通过类名的字符串,获取类别 static GetType()
//Type enemyType=Type.GetType("TankGame.Enemy");
//Type rigidBodyType = Type.GetType("UnityEngine.Rigidbody");
#endregion#region Type类中的属性
Type goType = typeof(GameObject);
Debug.Log(goType.Name);
Debug.Log(goType.FullName);
//程序集
Debug.Log(goType.Assembly);
//程序集+命名空间+类名
Debug.Log(goType.AssemblyQualifiedName);
//获取父类
Debug.Log(goType.BaseType);
gameObject.AddComponent(typeof(Rigidbody));
#endregion
//添加一个脚本组件,但限制该组件必须继承后面的那个类
AddComponentWithInhert("HeroObjBehaviour", "ObjBehaviourBase");
}
public bool IsInhert(string sonAssemblyQualifiedName, string parentAssemblyQualifiedName) {
Type sonType = Type.GetType(sonAssemblyQualifiedName);
Type parentType = Type.GetType(parentAssemblyQualifiedName);
//判断两个类是否为父子关系
if (sonType.BaseType == parentType) {
return true;
}
return false;
}
public void AddComponentWithInhert(string sonAssemblyQualifiedName, string parentAssemblyQualifiedName) {
bool isInhert=IsInhert(sonAssemblyQualifiedName, parentAssemblyQualifiedName);
if (isInhert) {
gameObject.AddComponent(Type.GetType(sonAssemblyQualifiedName));}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class ObjBehaviourBase : MonoBehaviour {
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class HeroObjBehaviour : ObjBehaviourBase
{}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public interface IBehaviour {
void ShowMe();
}
public class ObjBehaviourBase : MonoBehaviour,IBehaviour {
public ObjBehaviourBase() { }
private ObjBehaviourBase(string obnName) {
this.objName = objName;
}
//字段
private string objName;
public float objLiftTime=666;
//属性
public string ObjName {
get {
return objName;
}
set {
objName = value;
}
}
public void ShowMe() {
Debug.Log("ObjName:"+objName);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UIFrame;
using System.Reflection;namespace TankGame {
public class Tank {
}
public class Bullet {}
public class Enemy {}
}public class ReflectionType : MonoBehaviour {
private void Start()
{
#region 获取类型-Type
通过类名获取类别的方式 typeof
//Type personType = typeof(Person);
//Debug.Log("personType:"+personType);
//Type tankType = typeof(TankGame.Tank);
//Type gameObjectType = typeof(GameObject);
通过对象,获取类别GetType()
//Type transformType = transform.GetType();
有一个炮弹对象
//TankGame.Bullet blt = new TankGame.Bullet();
//Type BulletType = blt.GetType();
通过类名的字符串,获取类别 static GetType()
//Type enemyType=Type.GetType("TankGame.Enemy");
//Type rigidBodyType = Type.GetType("UnityEngine.Rigidbody");
#endregion#region Type类中的属性
Type goType = typeof(GameObject);
Debug.Log(goType.Name);
Debug.Log(goType.FullName);
//程序集
Debug.Log(goType.Assembly);
//程序集+命名空间+类名
Debug.Log(goType.AssemblyQualifiedName);
//获取父类
Debug.Log(goType.BaseType);
gameObject.AddComponent(typeof(Rigidbody));
#endregion
#region Type类中方法
//获取类型
Type objType = typeof(ObjBehaviourBase);
MemberInfo[] objMemberInfos = objType.GetMembers();
//for (int i=0;i<objMemberInfos.Length;i++) {
// Debug.Log(string.Format("MemberType[{0}]",i)+objMemberInfos[i].MemberType);
//}
//获取该类的所有构造函数
ConstructorInfo[] ctors = objType.GetConstructors();
for (int i = 0; i < ctors.Length; i++) {
Debug.Log(ctors[i].IsPrivate);
}
//获取公共字段
FieldInfo ltField=objType.GetField("objLiftTime");
Debug.Log("FieldType:" + ltField.FieldType);
Debug.Log("FieldType:" + ltField.IsStatic);//获取私有成员字段
FieldInfo[] fields=objType.GetFields(BindingFlags.Instance|BindingFlags.NonPublic);
Debug.Log(fields[0].Name);
//获取私有的成员字段
FieldInfo[] gameObjectFields=typeof(GameObject).GetFields(BindingFlags.Instance | BindingFlags.NonPublic);
//遍历所有私有成员
for (int i = 0; i < gameObjectFields.Length; i++) {
Debug.Log("<color=lime>"+gameObjectFields[i].Name+"</color>");}
//获取私有成员属性
PropertyInfo[] gameObjectProperty=typeof(GameObject).GetProperties(BindingFlags.Instance | BindingFlags.NonPublic);
//遍历所有私有成员
for (int i = 0; i < gameObjectProperty.Length; i++) {
Debug.Log("<color=lime>"+ gameObjectProperty[i].Name+"</color>");}
//获取方法
objType.GetMethods();
MethodInfo[] methodInfos=typeof(GameObject).GetMethods(BindingFlags.Instance | BindingFlags.NonPublic);
for (int i = 0; i < methodInfos.Length; i++)
{
Debug.Log("<color=red>" + methodInfos[i].Name + "</color>");}
//BindingFlags:表示搜索的按位组合
//Public 16 or NonPublic 32
//0001 0000 0010 0000
//Static 8 or Instance 4
//0000 1000 0000 0100
//组合:私有的且非静态NonPublic Instance
//NonPublic|Instance
// 0010 0000
//| 0000 0100
//-----------------------
// 0010 0100#endregion
//添加一个脚本组件,但限制该组件必须继承后面的那个类
AddComponentWithInhert("HeroObjBehaviour", "ObjBehaviourBase");
}
public bool IsInhert(string sonAssemblyQualifiedName, string parentAssemblyQualifiedName) {
Type sonType = Type.GetType(sonAssemblyQualifiedName);
Type parentType = Type.GetType(parentAssemblyQualifiedName);
//判断两个类是否为父子关系
if (sonType.BaseType == parentType) {
return true;
}
return false;
}
public void AddComponentWithInhert(string sonAssemblyQualifiedName, string parentAssemblyQualifiedName) {
bool isInhert=IsInhert(sonAssemblyQualifiedName, parentAssemblyQualifiedName);
if (isInhert) {
gameObject.AddComponent(Type.GetType(sonAssemblyQualifiedName));}
}
}