由于考虑到unity预编辑不用讲效率以及封闭原则,为了在不改动原来写好的脚本而添加新内容的情况下,我设计了一个反射来完成需求
即selectMum=DrawViewSelect<ViewEditor>(selectMum);
1.代码分析如下:
public int DrawViewSelect<T>(int selectIndex)
{
List<string> nameList = new List<string>();
List<T> TList = new List<T>();
T thisView=default(T);
//获取T脚本下的子类脚本名字
for (int i = 0; i < editorTypeList.Count; ++i)
{
if (Utility.IsChildsType(editorTypeList[i], typeof(T)))
{
TList.Add(Utility<T>.CreateEditer(editorTypeList[i]));
nameList.Add(editorTypeList[i].Name);
}
}
if (TList.Count > 0)
{
if (selectIndex >= TList.Count)
{
selectIndex = 0;
}
selectIndex = EditorGUILayout.Popup(selectIndex, nameList.ToArray());//讲获取的脚本名字绘制在窗口中 (即那种下拉框)
//EditorGUILayout.Space();
//if (index != selectIndex)
//{
// selectIndex = index;
//}
thisView = TList[selectIndex];
if (thisView != null)
{
Utility.CallNotParameMethod(thisView, "OnGUI");//执行选中脚本下的OnGUI方法
}
}
return selectIndex;
}
其中在我得工具类utility如下:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
/// <summary>
/// 工具类
/// </summary>
public class Utility : Utility<object>{}
public class Utility<T>
{
/// <summary>
/// 反射调用没有参数的方法
/// </summary>
/// <param name="obj">类</param>
/// <param name="methodName">方法名</param>
public static void CallNotParameMethod(Object obj, String methodName)
{
if (obj != null && !string.IsNullOrEmpty(methodName))
{
MethodInfo method = obj.GetType().GetMethod(methodName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
if (method != null)
{
method.Invoke(obj, new Object[] { });
}
}
}
/// <summary>
/// 判断是否为子类
/// </summary>
/// <param name="type"></param>
/// <param name="baseType"></param>
/// <returns></returns>
public static bool IsChildsType(Type type, Type baseType,bool isFindGrandson=false)
{
if (!isFindGrandson)
return type.BaseType == baseType;
else {
bool result = false;
Type tObje = typeof(System.Object);
for (; ; )
{
if (type == null)
{
break;
}
if (type == tObje)
{
break;
}
if (type == baseType)
{
result = true;
break;
}
type = type.BaseType;//先取得父类
}
return result;
}
}
/// <summary>
/// 返回该类下的子类
/// </summary>
/// <param name="assembly">Assembly.GetExecutingAssembly()</param>
/// <param name="baseType"></param>
/// <param name="isFindGroudson">是否找孙子类</param>
public static List<Type> FindChildType(Assembly assembly ,Type baseType,bool isFindGroudson=false)
{
Type[] types = assembly.GetExportedTypes();
List<Type> needList=new List<Type>();
for (int i = 0; i < types.Length; i++)
{
if (Utility.IsChildsType(types[i], baseType,isFindGroudson))
{
needList.Add(types[i]);
}
}
return needList;
}
/// <summary>
/// type数组转换界面类的数组
/// </summary>
/// <param name="isFindGroudson">是否找孙子类</param>
public static List<T> CreateEditer(List<Type> editorTypeList)
{
//List<Type> editorTypeList = Utility.FindChildType(assembly, typeof(T),isFindGroudson);
List<T> viewList = new List<T>();
for (int i = 0; i < editorTypeList.Count; i++)
{
viewList.Add(CreateEditer(editorTypeList[i]));
}
return viewList;
}
/// <summary>
/// type转换界面类
/// </summary>
public static T CreateEditer(Type editorTypeList)
{
T editor = default(T);
ConstructorInfo constructorInfo = editorTypeList.GetConstructor(new Type[] { });
if (constructorInfo != null)
{
editor = (T)constructorInfo.Invoke(new System.Object[] { });
CallNotParameMethod(editor, "Start");
}
return editor;
}
}