/// <summary> /// Editor 框架 的设计,1、定义接口,定义基类 2、通过给实现类添加注解(属性标签)找到所有实现类,3、通过反射拿到实现类的Type 4、实例化实现类 :Activator.CreateInstance(type)。5、操作接口和基类来实现逻辑 /// </summary> void LoadEditors() { Type _interface = typeof(IModuleEditor); //接口 Type _base_type = typeof(ModuleEditor <>); //基类(可范型) List <Type> types = new List <Type>(); //获取当前domain的所有Assembly foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()) { Type[] assemblyTypes; try { //获取所有Assembly的所有Type assemblyTypes = assembly.GetTypes(); foreach (Type type in assemblyTypes) { if (_interface.IsAssignableFrom(type) && type != _interface && type != _base_type) { types.Add(type); } } } catch (ReflectionTypeLoadException e) { Debug.LogError("反射异常 + e" + e.Message); } } //移除没有加属性注解的 MetaEditor 子类 types.RemoveAll(x => x.GetCustomAttributes(true).FirstOrDefault(y => y is OhMyFrameworkPanelTabAttribute) == null); // 按优先级排序 types.Sort((Type a, Type b) => { OhMyFrameworkPanelTabAttribute _a = (OhMyFrameworkPanelTabAttribute)a.GetCustomAttributes(true).FirstOrDefault(x => x is OhMyFrameworkPanelTabAttribute); OhMyFrameworkPanelTabAttribute _b = (OhMyFrameworkPanelTabAttribute)b.GetCustomAttributes(true).FirstOrDefault(x => x is OhMyFrameworkPanelTabAttribute); return(_a.Priority.CompareTo(_b.Priority)); }); editors.Clear(); foreach (Type editor in types) { //分组,没有加BerryPanelGroupAttribute的放在“” 中 OhMyFrameworkPanelGroupAttribute attr = (OhMyFrameworkPanelGroupAttribute)editor.GetCustomAttributes(true).FirstOrDefault(x => x is OhMyFrameworkPanelGroupAttribute); string group = attr != null ? attr.Group : ""; if (!editors.ContainsKey(group)) { editors.Add(group, new List <Type>()); } editors[group].Add(editor); } }
public void Show(IModuleEditor editor) { EditorGUI.FocusTextInControl(""); if (editor.Initialize()) { current_editor = editor; save_CurrentEditor.String = editor.GetType().FullName; OhMyFrameworkPanelTabAttribute attribute = (OhMyFrameworkPanelTabAttribute)editor.GetType().GetCustomAttributes(true).FirstOrDefault(x => x is OhMyFrameworkPanelTabAttribute); editorAttribute = attribute; editorRender = editor.OnGUI; } }
bool DrawTabButton(OhMyFrameworkPanelTabAttribute tabAttribute) { bool result = false; if (tabAttribute != null) { using (new GUIHelper.BackgroundColor(editorAttribute != null && editorAttribute.Match(tabAttribute) ? selectionColor : Color.white)) using (new GUIHelper.ContentColor(Styles.centeredMiniLabel.normal.textColor)) result = GUILayout.Button(new GUIContent(tabAttribute.Title, tabAttribute.Icon), tabButtonStyle, GUILayout.ExpandWidth(true)); if (editorAttribute != null && editorAttribute.Match(tabAttribute) && editorRender == null) { result = true; } } return(result); }
void DrawTabs(string group) { if (editors.ContainsKey(group)) { if (!string.IsNullOrEmpty(group)) { DrawTabTitle(group); } foreach (Type editor in editors[group]) { OhMyFrameworkPanelTabAttribute attr = (OhMyFrameworkPanelTabAttribute)editor.GetCustomAttributes(true).FirstOrDefault(x => x is OhMyFrameworkPanelTabAttribute); if (attr != null && DrawTabButton(attr)) { Show((IModuleEditor)Activator.CreateInstance(editor)); } } } }