public List <SharedVariable> GetVariables(Type type) { if (!behaviour) { return(null); } return(behaviour.GetVariables(type)); }
private void DrawProperty(SerializedProperty property, FieldInfo fieldInfo) { if (fieldInfo == null) { return; } ShouldHide(fieldInfo, out var shouldHide, out var readOnly); if (shouldHide && !readOnly) { return; } string displayName = property.displayName; DisplayNameAttribute nameAttr = fieldInfo.GetCustomAttribute <DisplayNameAttribute>(); if (nameAttr != null) { displayName = nameAttr.name; } string tooltip = property.tooltip; TooltipAttribute tipAttr = fieldInfo.GetCustomAttribute <TooltipAttribute>(); if (tipAttr != null) { tooltip = tipAttr.tooltip; } EditorGUI.BeginDisabledGroup(readOnly); var type = fieldInfo.FieldType; if (type.IsSubclassOf(typeof(SharedVariable))) { SerializedProperty name = property.FindPropertyRelative("_name"); SerializedProperty value = property.FindPropertyRelative("value"); //使用HideInInspector标签后仍可Find出来 SerializedProperty isShared = property.FindPropertyRelative("isShared"); SerializedProperty isGlobal = property.FindPropertyRelative("isGlobal"); int typeIndex = isGlobal.boolValue ? 2 : (isShared.boolValue ? 1 : 0); int oldTypeIndex = typeIndex; Rect rect = EditorGUILayout.GetControlRect(); //EditorGUI.BeginDisabledGroup(nodeEditor.node.IsInstance); typeIndex = EditorGUI.Popup(new Rect(rect.x + rect.width - 32, rect.y, 32, EditorGUIUtility.singleLineHeight), typeIndex, varType); if (oldTypeIndex != typeIndex) { switch (typeIndex) { case 1: isShared.boolValue = true; isGlobal.boolValue = false; break; case 2: isShared.boolValue = false; isGlobal.boolValue = true; break; default: isShared.boolValue = false; isGlobal.boolValue = false; break; } } //EditorGUI.EndDisabledGroup(); switch (typeIndex) { case 1: Rect valueRect = new Rect(rect.x, rect.y, rect.width - 34, EditorGUIUtility.singleLineHeight); DrawSharedField(valueRect, tree); break; case 2: valueRect = new Rect(rect.x, rect.y, rect.width - 34, EditorGUIUtility.singleLineHeight); DrawSharedField(valueRect, global); break; default: valueRect = new Rect(rect.x, rect.y, rect.width - 34, EditorGUI.GetPropertyHeight(value, true)); if (type == typeof(SharedString) && fieldInfo.GetCustomAttribute <Tag_BTAttribute>() != null) { value.stringValue = EditorGUI.TagField(valueRect, new GUIContent(displayName, tooltip), string.IsNullOrEmpty(value.stringValue) ? UnityEditorInternal.InternalEditorUtility.tags[0] : value.stringValue); } else { EditorGUI.PropertyField(valueRect, value, new GUIContent(displayName, tooltip), true); } EditorGUILayout.Space(EditorGUI.GetPropertyHeight(value, true) - EditorGUIUtility.singleLineHeight); break; } void DrawSharedField(Rect valueRect, ISharedVariableHandler variableHandler) { var variables = variableHandler.GetVariables(type); string[] varNames = variables.Select(x => x.name).Prepend("未选择").ToArray(); GUIContent[] contents = new GUIContent[varNames.Length]; for (int i = 0; i < varNames.Length; i++) { contents[i] = new GUIContent(varNames[i]); } if (ZetanUtility.Editor.TryGetValue(property, out var value)) { var link = type.GetField("linkedVariable", ZetanUtility.CommonBindingFlags).GetValue(value); if (link != null) { name.stringValue = (link as SharedVariable).name; } } int nameIndex = Array.IndexOf(varNames, name.stringValue); if (nameIndex < 0) { nameIndex = 0; } nameIndex = EditorGUI.Popup(valueRect, new GUIContent(displayName, tooltip), nameIndex, contents); string nameStr = string.Empty; if (nameIndex > 0 && nameIndex <= variables.Count) { nameStr = varNames[nameIndex]; } //if (!nodeEditor.node.IsInstance) name.stringValue = nameStr; //else if (nameStr != name.stringValue) //{ // var val = variableHandler.GetVariable(varNames[nameIndex]); // if (val == null) // { // val = Activator.CreateInstance(type) as SharedVariable; // val.isShared = variableHandler is BehaviourTree; // val.isGlobal = variableHandler is GlobalVariables; // type.GetField("_name", ZetanUtility.CommonBindingFlags).SetValue(val, nameStr); // } // ZetanUtility.Editor.TrySetValue(property, val); //} if (!nodeEditor.node.IsInstance) { name.stringValue = nameStr; } if (!string.IsNullOrEmpty(nameStr)) { var val = variableHandler.GetVariable(nameStr); if (val != null && value != null) { (value as SharedVariable).Link(val); } } else { (value as SharedVariable).Unlink(); } } } else if (type == typeof(string)) { if (fieldInfo.GetCustomAttribute <Tag_BTAttribute>() != null) { property.stringValue = EditorGUILayout.TagField(new GUIContent(displayName, tooltip), string.IsNullOrEmpty(property.stringValue) ? UnityEditorInternal.InternalEditorUtility.tags[0] : property.stringValue); } else { NameOfVariableAttribute varNameAttr = fieldInfo.GetCustomAttribute <NameOfVariableAttribute>(); if (varNameAttr != null) { var variables = tree.GetVariables(varNameAttr.type); string[] varNames = variables.Select(x => x.name).Prepend("未选择").ToArray(); GUIContent[] contents = new GUIContent[varNames.Length]; for (int i = 0; i < varNames.Length; i++) { contents[i] = new GUIContent(varNames[i]); } int nameIndex = Array.IndexOf(varNames, property.stringValue); if (nameIndex < 0) { nameIndex = 0; } nameIndex = EditorGUILayout.Popup(new GUIContent(displayName, tooltip), nameIndex, contents); string nameStr = string.Empty; if (nameIndex > 0 && nameIndex <= variables.Count) { nameStr = varNames[nameIndex]; } property.stringValue = nameStr; } else { EditorGUILayout.PropertyField(property, true); } } } else { EditorGUILayout.PropertyField(property, true); } EditorGUI.EndDisabledGroup(); }