private bool ShouldHide(HideIfAttribute hideAttr, SerializedProperty property) { if (hideAttr.paths.Length < 1) { return(false); } if (hideAttr.type != null) { return(ShouldHide0()); } else if (string.IsNullOrEmpty(hideAttr.relational)) { return(ShouldHide1()); } else { return(ShouldHide2()); } bool ShouldHide(string p, object v) { if (p == "typeof(this)") { return((v as System.Type).IsAssignableFrom(fieldInfo.ReflectedType)); } else if (ZetanUtility.Editor.TryGetValue(property, out _)) { bool type = p.StartsWith("typeof("); if (type) { p = p.Replace("typeof(", "").Replace(")", ""); } if (ZetanUtility.TryGetMemberValue(p, property.serializedObject.targetObject ?? property.managedReferenceValue, out var value, out _)) { if (type) { return(value.GetType().IsAssignableFrom(v as System.Type)); } else if (Equals(value, v)) { return(true); } else { return(false); } } else { Debug.LogWarning($"找不到路径:{(property.serializedObject.targetObject ?? property.managedReferenceValue).GetType().Name}.{p}"); return(false); } }
private void ShouldHide(FieldInfo fieldInfo, out bool should, out bool readOnly) { HideIf_BTAttribute hideAttr = fieldInfo.GetCustomAttribute <HideIf_BTAttribute>(); ReadOnlyAttribute roAttr = fieldInfo.GetCustomAttribute <ReadOnlyAttribute>(); should = false; readOnly = false; if (hideAttr != null) { readOnly = hideAttr.readOnly; if (ZetanUtility.TryGetMemberValue(hideAttr.path, nodeEditor.node, out var fv, out var mi) && mi != null) { if (typeof(SharedVariable).IsAssignableFrom(fv.GetType())) { if (fv is SharedVariable sv) { if ((sv.isGlobal || sv.isShared) && !string.IsNullOrEmpty(sv.name)) { if (sv.isGlobal) { sv = global.GetVariable(sv.name); } else if (sv.isShared) { sv = tree.GetVariable(sv.name); } if (sv != null && Equals(sv.GetValue(), hideAttr.value)) { should = true; } } else if (!sv.isGlobal && !sv.isShared && Equals(sv.GetValue(), hideAttr.value)) { should = true; } } } else if (Equals(fv, hideAttr.value)) { should = true; } } } else if (roAttr != null) { readOnly = !roAttr.onlyRuntime || roAttr.onlyRuntime && Application.isPlaying; } }
public void Init(BehaviourTree tree) { if (!IsInstance) { Debug.LogError("尝试初始化未实例化的结点: " + name); return; } Tree = tree; foreach (var field in GetType().GetFields(ZetanUtility.CommonBindingFlags).Where(field => field.FieldType.IsSubclassOf(typeof(SharedVariable)))) { var hideAttr = field.GetCustomAttribute <HideIf_BTAttribute>(); if (hideAttr != null && ZetanUtility.TryGetMemberValue(hideAttr.path, this, out var value, out _) && Equals(value, hideAttr.value)) { continue; } SharedVariable variable = field.GetValue(this) as SharedVariable; //if (variable.isShared) field.SetValue(this, this.owner.GetVariable(variable.name)); //else if (variable.isGlobal) field.SetValue(this, BehaviourManager.Instance.GetVariable(variable.name)); if (variable.isShared) { variable.Link(Tree.GetVariable(variable.name)); } else if (variable.isGlobal) { variable.Link(BehaviourManager.Instance.GetVariable(variable.name)); } } Shortcut = new NodeShortcut(Tree.Executor); OnAwake(); #if false void TryLinkSharedVariable(object onwer, FieldInfo field) { if (field.FieldType.IsSubclassOf(typeof(SharedVariable))) { SharedVariable variable = field.GetValue(onwer) as SharedVariable; field.SetValue(this, this.owner.GetVariable(variable.name)); } else if (field.FieldType.IsArray)//因为Unity只能显示数组和列表,只对这两种特殊情况特殊处理 { var array = field.GetValue(owner) as SharedVariable[]; if (array.Length < 1) { return; } bool typeMatch = false; var eType = array[0].GetType(); var fieldInfos = eType.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); if (eType.IsSubclassOf(typeof(SharedVariable))) { typeMatch = true; } for (int i = 0; i < array.Length; i++) { if (typeMatch) { array[i] = this.owner.GetVariable(array[i].name); } else { foreach (var _field in fieldInfos) { TryLinkSharedVariable(array[i], _field); } } } } else if (typeof(IList).IsAssignableFrom(field.FieldType)) { var list = field.GetValue(owner) as IList; if (list.Count < 1) { return; } bool typeMatch = false; var eType = list[0].GetType(); var fieldInfos = eType.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); if (eType.IsSubclassOf(typeof(SharedVariable))) { typeMatch = true; } for (int i = 0; i < list.Count; i++) { if (typeMatch) { list[i] = this.owner.GetVariable((list[i] as SharedVariable).name); } else { foreach (var _field in fieldInfos) { TryLinkSharedVariable(list[i], _field); } } } } } #endif }
private void OnEnable() { if (ZetanUtility.TryGetMemberValue("attributeTypes", target, out var value, out _)) { dict = value as Dictionary <string, RoleAttributeValueType>; } names = serializedObject.FindProperty("names"); types = serializedObject.FindProperty("types"); list = new ReorderableList(serializedObject, names, false, true, true, true) { drawElementCallback = (rect, index, isActive, isFocused) => { SerializedProperty name = names.GetArrayElementAtIndex(index); SerializedProperty type = types.GetArrayElementAtIndex(index); EditorGUI.LabelField(new Rect(rect.x, rect.y, 30, EditorGUIUtility.singleLineHeight), $"[{index}]"); string nameBef = name.stringValue; EditorGUI.PropertyField(new Rect(rect.x + 30, rect.y, (rect.width - 30) / 2 - 2, EditorGUIUtility.singleLineHeight), name, new GUIContent(string.Empty)); if (nameBef != name.stringValue && dict.ContainsKey(name.stringValue)) { if (EditorUtility.DisplayDialog("错误", $"已存在属性名 [{name.stringValue}]", "确定")) { name.stringValue = nameBef; } } EditorGUI.PropertyField(new Rect(rect.x + +30 + (rect.width - 30) / 2, rect.y, (rect.width - 30) / 2 - 2, EditorGUIUtility.singleLineHeight), type, new GUIContent(string.Empty)); }, onAddCallback = (list) => { string name = "属性0"; int i = 1; while (dict.ContainsKey(name)) { name = $"属性{i}"; i++; } names.arraySize++; names.GetArrayElementAtIndex(names.arraySize - 1).stringValue = name; types.arraySize++; types.GetArrayElementAtIndex(types.arraySize - 1).enumValueIndex = (int)RoleAttributeValueType.Integer; list.Select(names.arraySize - 1); }, onRemoveCallback = (list) => { if (list.index >= 0 && list.index < names.arraySize) { if (EditorUtility.DisplayDialog("警告", $"确定删除属性 [{names.GetArrayElementAtIndex(list.index).stringValue}] 吗?", "删除", "取消")) { names.DeleteArrayElementAtIndex(list.index); types.DeleteArrayElementAtIndex(list.index); } } }, onCanRemoveCallback = (list) => { return(list.selectedIndices.Contains(list.index)); }, elementHeightCallback = (index) => { return(EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing); }, drawHeaderCallback = (rect) => { EditorGUI.LabelField(rect, "基础属性类型列表"); } }; }