private VisualElement GetField(SerializedProperty so, string path, string label = null) { var prop = so.FindPropertyRelative(path); var valueType = prop.propertyType; // prop.GetValueType(); if (valueType == SerializedPropertyType.ObjectReference) // valueType.IsSubclassOf(typeof(UnityEngine.Object)) { var objectField = new ObjectField(label); if (label == null) { objectField.AddToClassList("hideLabel"); } objectField.objectType = prop.GetValueType(); objectField.allowSceneObjects = false; objectField.bindingPath = prop.propertyPath; objectField.Bind(prop.serializedObject); objectField.AddToClassList("node-field-input"); objectField.binding = new BBB(); return(objectField); } else { PropertyField p = new PropertyField(prop, label); if (label == null) { p.AddToClassList("hideLabel"); } p.Bind(so.serializedObject); p.AddToClassList("node-field-input"); return(p); } }
// ---------------------------------------------------- Build Object Objects // -- Build Object Objects ------------------------------------------------- private VisualElement MakePropertyItem(SerializedProperty property, string propertyPath, int index, bool hideSelector = true) { var container = new VisualElement(); container.AddToClassList("makePropertyItemContainer"); var pf = new PropertyField(property.GetArrayElementAtIndex(index)) { name = $"{propertyPath}PropertyField", }; // @formatter:off pf.AddToClassList("makePropertyItem"); pf.AddToClassList("unity-base-field--no-label"); pf.bindingPath = propertyPath; pf.style.flexGrow = new StyleFloat(1.0f); BuildContainers(container, index); container.Add(pf); return(container); } // @formatter:on
protected static VisualElement CreateStandardField(string gamePath) { var container = new VisualElement(); var label = ObjectNames.NicifyVariableName(gamePath); var field = new PropertyField { bindingPath = gamePath, label = label }; container.Add(field); container.AddToClassList("thunderkit-field"); field.AddToClassList("thunderkit-field-input"); return(container); }
public ManagedReferenceField(SerializedProperty property, string label) { AddToClassList(ussClassName); pickingMode = PickingMode.Ignore; if (property.propertyType != SerializedPropertyType.ManagedReference) { Debug.LogError("property must be a ManagedReference"); return; } var tracker = new ManagedReferenceTypeTracker(property); tracker.style.display = DisplayStyle.None; tracker.AddToClassList(trackerUssClassName); Add(tracker); var propertyField = new PropertyField(property, label); propertyField.AddToClassList(propertyFieldUssClassName); Add(propertyField); }
public override VisualElement CreatePropertyGUI(SerializedProperty property) { var value = property.FindPropertyRelative("m_Value"); var root = new VisualElement { style = { flexDirection = FlexDirection.Row } }; root.Add(new Label(ManagedReferenceUtility.GetDisplayName(property.managedReferenceFullTypename).text) { style = { width = 50, alignSelf = Align.Center } }); var valueField = new PropertyField(value) { style = { flexGrow = 1 } }; // Workaround to handle the fact that PropertyField will always have a label. valueField.AddToClassList("unity-property-field--no-label"); root.Add(valueField); return(root); }
public override VisualElement CreatePropertyGUI(SerializedProperty property) { container = new VisualElement(); if (styleSheet is null) { styleSheet = typeof(SerializedDictionary <object, object>).GetStyleSheet($"InstanceId{nameof(SerializedDictionary<object, object>)}"); if (!(styleSheet is null)) { container.styleSheets.Add(styleSheet); } } propertyKeyField = property.FindPropertyRelative(SerializedDictionary <object, object> .KeyProperty); propertyValueField = property.FindPropertyRelative(SerializedDictionary <object, object> .ValueProperty); propertyKeyField.serializedObject.ApplyModifiedProperties(); propertyValueField.serializedObject.ApplyModifiedProperties(); var serializedDictionaryFoldout = new Foldout { text = property.displayName, value = false }; serializedDictionaryFoldout.NameAsUSS(nameof(serializedDictionaryFoldout)); var box = new Box(); var scroller = new ScrollView { name = "serialDictScroller" }; for (int i = 0; i < propertyKeyField.arraySize; i++) { var keyTypeString = propertyKeyField.GetArrayElementAtIndex(i).type; var keyType = propertyKeyField.GetArrayElementAtIndex(i).GetType(); var valueTypeString = propertyValueField.GetArrayElementAtIndex(i).type; var valueType = propertyValueField.GetArrayElementAtIndex(i).GetType(); // ----------------------------------------------------------- Dictionary Container // -- Dictionary Container -------------------------------------------------------- var layerEntry = new VisualElement { focusable = true, name = $"Entry: {i}" }; layerEntry.AddToClassList("serialDictionaryContainer"); // ----------------------------------------------------------------- Dictionary Key // -- Dictionary Key -------------------------------------------------------------- VisualElement listKey; listKey = new PropertyField(propertyKeyField.GetArrayElementAtIndex(i)) { name = "keyTextField" }; var keyTextField = new TextField { bindingPath = propertyKeyField.propertyPath, label = null, name = "keyTextField" }; keyTextField.tooltip = keyTextField.text; switch (valueTypeString) { case { } a when a.Contains("GuidReference"): case { } b when b.Contains("GuidComponent"): keyTextField.AddToClassList("serialDictionaryKeyGuid"); keyTextField.AddToClassList("unity-base-field--no-label"); break; default: keyTextField.AddToClassList("serialDictionaryKey"); keyTextField.AddToClassList("unity-base-field--no-label"); break; } keyTextField.SetValueWithoutNotify(propertyKeyField.GetArrayElementAtIndex(i).stringValue); keyTextField.SetEnabled(false); // keyTextField.Q(null, "unity-base-text-field__input").RemoveFromClassList("unity-base-text-field__input"); keyTextField.Q(null, "unity-disabled") .RemoveFromClassList("unity-disabled"); keyTextField.AddToClassList("serialDictionaryKeyLocator"); keyTextField.SetEnabled(false); listKey = keyTextField; layerEntry.Add(listKey); // --------------------------------------------------------------- Dictionary Value // -- Dictionary Value ------------------------------------------------------------ VisualElement listValue; listValue = new PropertyField(propertyValueField.GetArrayElementAtIndex(i)) { name = "valueObjectField" }; listValue.SetEnabled(true); switch (valueType) { case { } a when objectTypes.Contains(a.Name): case { } t1 when t1 == typeof(Object): case { } t2 when typeof(Object).IsSubclassOf(t2): case { } t3 when typeof(Object).IsAssignableFrom(t3): case { } t4 when Convert.GetTypeCode(t4) == TypeCode.Object: case { } t5 when typeof(Object).IsSubclassOf(t5): case { } t6 when typeof(Object).IsAssignableFrom(t6): var objectField = new ObjectField { bindingPath = propertyValueField.propertyPath, label = null, name = "valueObjectField" }; objectField.SetValueWithoutNotify(propertyValueField.GetArrayElementAtIndex(i).objectReferenceValue); objectField.AddToClassList("serialDictionaryValue"); objectField.AddToClassList("unity-base-field--no-label"); objectField.Q(null, "unity-object-field__selector") .RemoveFromClassList("unity-object-field__selector"); // objectField.Q(null, "unity-object-field__input").RemoveFromClassList("unity-object-field__input"); listValue = objectField; break; case { } b when guidTypes.Contains(b.Name): var guidObjectField = new ObjectField { bindingPath = propertyValueField.propertyPath, label = null, name = "valueObjectField" }; guidObjectField.SetValueWithoutNotify(propertyValueField.GetArrayElementAtIndex(i).objectReferenceValue); guidObjectField.AddToClassList("serialDictionaryGuidValue"); guidObjectField.AddToClassList("unity-base-field--no-label"); guidObjectField.Q(null, "unity-object-field__selector") .RemoveFromClassList("unity-object-field__selector"); // objectField.Q(null, "unity-object-field__input").RemoveFromClassList("unity-object-field__input"); listValue = guidObjectField; break; case { } d when d == (typeof(int)): var valueTextField = new TextField { bindingPath = propertyValueField.propertyPath, label = null }; valueTextField.SetValueWithoutNotify(propertyValueField.GetArrayElementAtIndex(i).intValue.ToString()); valueTextField.SetEnabled(false); listValue = valueTextField; listValue.AddToClassList("serialDictionaryValue"); listValue.RemoveFromClassList("unity-base-text-field__input"); break; case { } d when d == typeof(Type): var valueTypeTextField = new TextField { bindingPath = propertyValueField.propertyPath, label = null }; valueTypeTextField.SetValueWithoutNotify(propertyValueField.GetArrayElementAtIndex(i).stringValue); valueTypeTextField.SetEnabled(false); listValue = valueTypeTextField; listValue.AddToClassList("serialDictionaryValue"); listValue.RemoveFromClassList("unity-base-text-field__input"); break; default: if (property.IsReallyArray()) { listValue = new PropertyField(propertyValueField); } listValue.AddToClassList("serialDictionaryValue"); listValue.AddToClassList("unity-base-field--no-label"); break; } listValue.AddToClassList("serialDictionaryValueLocator"); layerEntry.Add(listValue); scroller.Add(layerEntry); } box.Add(scroller); serializedDictionaryFoldout.Add(box); container.Add(serializedDictionaryFoldout); return(container); }
public override VisualElement CreatePropertyGUI(SerializedProperty property) { container = new VisualElement(); if (styleSheet is null) { styleSheet = idConfig.GetStyleSheet("AAICustomProperties"); if (!(styleSheet is null)) { container.styleSheets.Add(styleSheet); } } propertyKeyField = property.FindPropertyRelative(SerializedDictionary <object, object> .KeyProperty); propertyValueField = property.FindPropertyRelative(SerializedDictionary <object, object> .ValueProperty); propertyKeyField.serializedObject.ApplyModifiedProperties(); propertyValueField.serializedObject.ApplyModifiedProperties(); var box = new Box(); var scroller = new ScrollView { name = "serialDictScroller" }; for (int i = 0; i < propertyKeyField.arraySize; i++) { // ReSharper disable once UnusedVariable var keyType = propertyKeyField.GetArrayElementAtIndex(i).type; var valueType = propertyValueField.GetArrayElementAtIndex(i).type; // ----------------------------------------------------------- Dictionary Container // -- Dictionary Container -------------------------------------------------------- var layerEntry = new VisualElement { focusable = true, name = $"Entry: {i}" }; layerEntry.AddToClassList("serialDictionaryContainer"); // ----------------------------------------------------------------- Dictionary Key // -- Dictionary Key -------------------------------------------------------------- var keyTextField = new TextField { bindingPath = propertyKeyField.propertyPath, label = null, name = "keyTextField" }; switch (valueType) { case string a when a.Contains("GuidReference"): case string b when b.Contains("GuidComponent"): keyTextField.AddToClassList("serialDictionaryKeyGuid"); keyTextField.AddToClassList("unity-base-field--no-label"); break; default: keyTextField.AddToClassList("serialDictionaryKey"); keyTextField.AddToClassList("unity-base-field--no-label"); break; } keyTextField.SetValueWithoutNotify(propertyKeyField.GetArrayElementAtIndex(i).stringValue); keyTextField.SetEnabled(false); // keyTextField.Q(null, "unity-base-text-field__input").RemoveFromClassList("unity-base-text-field__input"); keyTextField.Q(null, "unity-disabled") .RemoveFromClassList("unity-disabled"); keyTextField.AddToClassList("serialDictionaryKeyLocator"); keyTextField.SetEnabled(false); VisualElement listKey = keyTextField; layerEntry.Add(listKey); // --------------------------------------------------------------- Dictionary Value // -- Dictionary Value ------------------------------------------------------------ VisualElement listValue = new PropertyField(propertyValueField.GetArrayElementAtIndex(i)) { name = "valueObjectField" }; listValue.SetEnabled(true); switch (valueType) { case string a when objectTypes.Contains(a): var objectField = new ObjectField { bindingPath = propertyValueField.propertyPath, label = null, name = "valueObjectField" }; objectField.SetValueWithoutNotify(propertyValueField.GetArrayElementAtIndex(i).objectReferenceValue); objectField.AddToClassList("serialDictionaryValue"); objectField.AddToClassList("unity-base-field--no-label"); objectField.Q(null, "unity-object-field__selector") .RemoveFromClassList("unity-object-field__selector"); // objectField.Q(null, "unity-object-field__input").RemoveFromClassList("unity-object-field__input"); listValue = objectField; break; case string b when guidTypes.Contains(b): var guidObjectField = new ObjectField { bindingPath = propertyValueField.propertyPath, label = null, name = "valueObjectField" }; guidObjectField.SetValueWithoutNotify(propertyValueField.GetArrayElementAtIndex(i).objectReferenceValue); guidObjectField.AddToClassList("serialDictionaryGuidValue"); guidObjectField.AddToClassList("unity-base-field--no-label"); guidObjectField.Q(null, "unity-object-field__selector") .RemoveFromClassList("unity-object-field__selector"); // objectField.Q(null, "unity-object-field__input").RemoveFromClassList("unity-object-field__input"); listValue = guidObjectField; break; case string d when d.Contains("int"): var valueTextField = new TextField { bindingPath = propertyValueField.propertyPath, label = null }; valueTextField.SetValueWithoutNotify(propertyValueField.GetArrayElementAtIndex(i).intValue.ToString()); valueTextField.SetEnabled(false); listValue = valueTextField; listValue.AddToClassList("serialDictionaryValue"); listValue.RemoveFromClassList("unity-base-text-field__input"); break; case string d when d.Contains("Type"): var valueTypeTextField = new TextField { bindingPath = propertyValueField.propertyPath, label = null }; valueTypeTextField.SetValueWithoutNotify(propertyValueField.GetArrayElementAtIndex(i).stringValue); valueTypeTextField.SetEnabled(false); listValue = valueTypeTextField; listValue.AddToClassList("serialDictionaryValue"); listValue.RemoveFromClassList("unity-base-text-field__input"); break; default: listValue.AddToClassList("serialDictionaryValue"); listValue.AddToClassList("unity-base-field--no-label"); break; } listValue.AddToClassList("serialDictionaryValueLocator"); layerEntry.Add(listValue); scroller.Add(layerEntry); } box.Add(scroller); container.Add(box); return(container); }
public static void BuildInspectorPropertiesElement(string elementPath, IEditorContainer editor, System.Collections.Generic.HashSet <System.Type> usedComponents, SerializedProperty obj, UnityEngine.UIElements.VisualElement container, bool noFields, System.Action <int, PropertyField> onBuild = null) { obj = obj.Copy(); container.Clear(); var source = obj.Copy(); SerializedProperty iterator = obj; if (iterator.NextVisible(true) == false) { return; } if (iterator.NextVisible(true) == false) { return; } var depth = iterator.depth; var i = 0; var iteratorNext = iterator.Copy(); do { if (string.IsNullOrEmpty(elementPath) == false) { iterator = iteratorNext.FindPropertyRelative(elementPath); } else { iterator = iteratorNext; } if (iterator.propertyType != SerializedPropertyType.ManagedReference) { continue; } var element = new VisualElement(); element.AddToClassList("element"); var itCopy = iterator.Copy(); GetTypeFromManagedReferenceFullTypeName(iterator.managedReferenceFullTypename, out var type); element.AddToClassList(i % 2 == 0 ? "even" : "odd"); element.RegisterCallback <UnityEngine.UIElements.ContextClickEvent, int>((evt, idx) => { var menu = new GenericMenu(); if (usedComponents != null) { menu.AddItem(new GUIContent("Delete"), false, () => { RemoveComponent((DataConfigEditor)editor, usedComponents, source, type, noFields); editor.Save(); BuildInspectorProperties(editor, usedComponents, source, container, noFields); }); menu.AddItem(new GUIContent("Copy JSON"), false, () => { var instance = itCopy.GetValue(); var json = JsonUtility.ToJson(instance, true); EditorGUIUtility.systemCopyBuffer = json; }); } editor.OnComponentMenu(menu, idx); menu.ShowAsContext(); }, i); if (type != null && usedComponents?.Contains(type) == false) { usedComponents?.Add(type); } if (type == null) { var label = new UnityEngine.UIElements.Label("MISSING: " + iterator.managedReferenceFullTypename); element.name = "missing"; label.AddToClassList("inner-element"); label.AddToClassList("missing-label"); element.Add(label); } else if (iterator.hasVisibleChildren == false || noFields == true) { var horizontal = new UnityEngine.UIElements.VisualElement(); horizontal.AddToClassList("inner-element"); horizontal.AddToClassList("no-fields-container"); element.name = type.Name; var toggle = new UnityEngine.UIElements.Toggle(); toggle.AddToClassList("no-fields-toggle"); toggle.SetEnabled(false); toggle.SetValueWithoutNotify(true); horizontal.Add(toggle); var label = new UnityEngine.UIElements.Label(GUILayoutExt.GetStringCamelCaseSpace(type.Name)); label.AddToClassList("no-fields-label"); horizontal.Add(label); element.Add(horizontal); } else { var label = GUILayoutExt.GetStringCamelCaseSpace(type.Name); if (iterator.hasVisibleChildren == true) { var childs = iterator.Copy(); //var height = EditorUtilities.GetPropertyHeight(childs, true, new GUIContent(label)); var cnt = EditorUtilities.GetPropertyChildCount(childs); if (cnt == 1 /*&& height <= 22f*/) { iterator.NextVisible(true); } } var propertyField = new PropertyField(iterator.Copy(), label); propertyField.BindProperty(iterator); onBuild?.Invoke(i, propertyField); propertyField.AddToClassList("property-field"); propertyField.AddToClassList("inner-element"); element.name = type.Name; element.Add(propertyField); } if (type != null) { var helps = type.GetCustomAttributes(typeof(ComponentHelpAttribute), false); if (helps.Length > 0) { var label = new UnityEngine.UIElements.Label(((ComponentHelpAttribute)helps[0]).comment); label.AddToClassList("comment"); element.Add(label); } if (typeof(IComponentStatic).IsAssignableFrom(type) == true) { var label = new UnityEngine.UIElements.Label("Static"); label.AddToClassList("static-component"); element.AddToClassList("has-static-component"); element.Add(label); } if (typeof(IComponentShared).IsAssignableFrom(type) == true) { var label = new UnityEngine.UIElements.Label("Shared"); label.AddToClassList("shared-component"); element.AddToClassList("has-shared-component"); element.Add(label); } } container.Add(element); ++i; } while (iteratorNext.NextVisible(false) == true && depth <= iteratorNext.depth); }