|
| 1 | +using System; |
| 2 | +using UnityEditor; |
| 3 | +using UnityEditor.UIElements; |
| 4 | +using UnityEngine.UIElements; |
| 5 | + |
| 6 | +public static class UIElementsHelper { |
| 7 | + /// <summary> |
| 8 | + /// Create a PropertyField with a callback. |
| 9 | + /// </summary> |
| 10 | + /// <param name="property">SerializedProperty that should be used to create the PropertyField.</param> |
| 11 | + /// <param name="changeCallback">Callback that gets triggered if a value changed.</param> |
| 12 | + /// <returns></returns> |
| 13 | + public static PropertyField CreatePropertyFieldWithCallback(SerializedProperty property, EventCallback<SerializedPropertyChangeEvent> changeCallback = null) { |
| 14 | + PropertyField propertyField = new PropertyField(property); |
| 15 | + if (changeCallback != null) { |
| 16 | + propertyField.RegisterCallback(changeCallback); |
| 17 | + } |
| 18 | + return propertyField; |
| 19 | + } |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// This is an alternative to InspectorElement.FillDefaultInspector that works without having to provide an editor object. |
| 23 | + /// </summary> |
| 24 | + /// <param name="serializedObject"></param> |
| 25 | + /// <param name="root">The UI root object.</param> |
| 26 | + /// <param name="changeCallback">Optional: Callback fired when a property changed.</param> |
| 27 | + /// <param name="CreateAdditionalUI">Optional: A method executed to create additional UI for every row.</param> |
| 28 | + /// <param name="rootPropertyPath">Optional: A relative property path that can be provided to start with a different property inside the serializedObject.</param> |
| 29 | + public static void CreateGenericUI(SerializedObject serializedObject, VisualElement root, EventCallback<SerializedPropertyChangeEvent> changeCallback = null, System.Action<SerializedProperty, VisualElement> CreateAdditionalUI = null, string rootPropertyPath = null) { |
| 30 | + |
| 31 | + SerializedProperty rootProperty = null; |
| 32 | + if (rootPropertyPath != null) { |
| 33 | + rootProperty = serializedObject.FindProperty(rootPropertyPath); |
| 34 | + } |
| 35 | + |
| 36 | + Action<SerializedProperty, ScrollView> creationLogic; |
| 37 | + if (CreateAdditionalUI != null) { |
| 38 | + creationLogic = (prop, scrollView) => { |
| 39 | + VisualElement container = new VisualElement(); |
| 40 | + container.AddToClassList(nameof(container) + nameof(PropertyField)); |
| 41 | + container.Add(CreatePropertyFieldWithCallback(prop, changeCallback)); |
| 42 | + CreateAdditionalUI(prop, container); |
| 43 | + scrollView.Add(container); |
| 44 | + }; |
| 45 | + } else { |
| 46 | + creationLogic = (prop, scrollView) => { |
| 47 | + scrollView.Add(CreatePropertyFieldWithCallback(prop, changeCallback)); |
| 48 | + }; |
| 49 | + } |
| 50 | + |
| 51 | + void ForEachProperty(ref ScrollView scrollView) { |
| 52 | + SerializedProperty prop = rootProperty == null ? serializedObject.GetIterator() : rootProperty; |
| 53 | + if (prop.NextVisible(true)) { |
| 54 | + do { |
| 55 | + if (prop.name != "m_Script") { |
| 56 | + creationLogic(prop.Copy(), scrollView); |
| 57 | + } |
| 58 | + } |
| 59 | + while (prop.NextVisible(false)); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + ScrollView scrollView = new ScrollView(); |
| 64 | + scrollView.AddToClassList("propertyList" + nameof(ScrollView)); |
| 65 | + root.Add(scrollView); |
| 66 | + ForEachProperty(ref scrollView); |
| 67 | + |
| 68 | + } |
| 69 | +} |
| 70 | + |
0 commit comments