public override void OnInspectorGUI() { serializedObject.Update(); //获取脚本对象 UINodeBase script = target as UINodeBase; SerializedProperty nodeToken = serializedObject.FindProperty("nodeToken"); //输入 nodeToken.stringValue = EditorGUILayout.TextField("NodeToken", nodeToken.stringValue); showComponentToken = EditorGUILayout.Foldout(showComponentToken, "Components"); if (showComponentToken) { List <string> nowComponents = new List <string>(script.GetComponents <UIBehaviour>().Select(p => p.GetType().Name)); List <string> oldComponents = new List <string>(script.actionComponents.Keys); if (!oldComponents.All(nowComponents.Contains) || oldComponents.Count != nowComponents.Count) { foreach (string b in oldComponents.Where(q => !nowComponents.Contains(q))) { script.actionComponents.Remove(b); } foreach (string b in nowComponents.Where(q => !oldComponents.Contains(q))) { script.actionComponents.Add(b, false); } } foreach (string s in nowComponents) { script.actionComponents[s] = EditorGUILayout.ToggleLeft(s, script.GetComponents <UIBindingComponent>().Where(p => p.targetComponent == s).Count() != 0); } } serializedObject.ApplyModifiedProperties(); }
private void OnEnable() { UINodeBase uiNode = target as UINodeBase; if (uiNode.actionComponents == null) { uiNode.actionComponents = new ObservableDictionary <string, bool>(""); uiNode.actionComponents.CollectionChanged += (e) => { //KeyValuePair<string, bool> oldItem = ((KeyValuePair<string, bool>)e.OldItem); //KeyValuePair<string, bool> newItem = ((KeyValuePair<string, bool>)e.NewItem); switch (e.Action) { case CollectionChangedAction.Add: if (((KeyValuePair <string, bool>)e.NewItem).Value == true && uiNode.GetComponents <UIBindingComponent>().Where(x => x.targetComponent == ((KeyValuePair <string, bool>)e.NewItem).Key).Count() == 0) { UIBindingComponent attributes = uiNode.gameObject.AddComponent <UIBindingComponent>(); attributes.targetComponent = ((KeyValuePair <string, bool>)e.NewItem).Key; } break; case CollectionChangedAction.Remove: foreach (UIBindingComponent attributes in uiNode.GetComponents <UIBindingComponent>().Where(x => x.targetComponent == ((KeyValuePair <string, bool>)e.OldItem).Key)) { Destroy(attributes); } break; case CollectionChangedAction.Replace: if (((KeyValuePair <string, bool>)e.NewItem).Value == true) { if (uiNode.GetComponents <UIBindingComponent>().Where(x => x.targetComponent == ((KeyValuePair <string, bool>)e.NewItem).Key).Count() == 0) { UIBindingComponent attributes = uiNode.gameObject.AddComponent <UIBindingComponent>(); attributes.targetComponent = ((KeyValuePair <string, bool>)e.NewItem).Key; } } else { List <UIBindingComponent> attributes = uiNode.GetComponents <UIBindingComponent>().Where(x => x.targetComponent == ((KeyValuePair <string, bool>)e.NewItem).Key).ToList(); while (attributes.Count > 0) { DestroyImmediate(attributes[0]); attributes.RemoveAt(0); } } break; } }; } }