Upgrade to Pro — share decks privately, control downloads, hide ads and more …

UIElements 最高! 一番好きなUIフレームワークです!

UIElements 最高! 一番好きなUIフレームワークです!

ねおりん

August 21, 2020
Tweet

More Decks by ねおりん

Other Decks in Programming

Transcript

  1. public class SerializationBehaviour : MonoBehaviour { [SerializeField] Serialization serialization; }

    [Serializable] public class Serialization { [SerializeField] EnumTest enumTest; } public enum EnumTest { A, B, C } 6
  2. 11

  3. [CustomEditor(typeof(SerializationBehaviour))] public sealed class SerializationBehaviourEditor : Editor { public override

    VisualElement CreateInspectorGUI() { var container = new VisualElement(); var serializationProperty = serializedObject.FindProperty("serialization"); var enumProperty = serializationProperty.FindPropertyRelative("enumTest"); var enumField = new EnumField(enumProperty.displayName, (EnumTest)enumProperty.enumValueIndex) { bindingPath = enumProperty.propertyPath }; container.Add(enumField); return container; } } 14
  4. public override VisualElement CreateInspectorGUI() { var container = new VisualElement();

    var serializationProperty = serializedObject.FindProperty("serialization"); var enumProperty = serializationProperty.FindPropertyRelative("enumTest"); var enumField = new EnumField(enumProperty.displayName, (EnumTest)enumProperty.enumValueIndex) { bindingPath = enumProperty.propertyPath }; container.Add(enumField); return container; } 16
  5. 17

  6. 18

  7. [CustomPropertyDrawer(typeof(Serialization))] public class EnumFieldPropertyDrawer : PropertyDrawer { public override VisualElement

    CreatePropertyGUI(SerializedProperty property) { var container = new VisualElement(); var enumProperty = property.FindPropertyRelative("enumTest"); // 2019.3 or newer var enumField = new EnumField(enumProperty.displayName, (EnumTest)enumProperty.enumValueIndex) { bindingPath = enumProperty.propertyPath }; container.Add(enumField); return container; } } 20
  8. public override VisualElement CreatePropertyGUI(SerializedProperty property) { var container = new

    VisualElement(); var enumProperty = property.FindPropertyRelative("enumTest"); // 2019.3 or newer var enumField = new EnumField(enumProperty.displayName, (EnumTest)enumProperty.enumValueIndex) { bindingPath = enumProperty.propertyPath }; container.Add(enumField); return container; } 22
  9. 23

  10. [CustomEditor(typeof(SerializationBehaviour))] public sealed class SerializationBehaviourEditor : Editor { public override

    VisualElement CreateInspectorGUI() { var container = new VisualElement(); var serializationProperty = serializedObject.FindProperty("serialization"); var serializationField = new PropertyField(serializationProperty); container.Add(serializationField); return container; } } 26
  11. public override VisualElement CreateInspectorGUI() { var container = new VisualElement();

    var serializationProperty = serializedObject.FindProperty("serialization"); var serializationField = new PropertyField(serializationProperty); container.Add(serializationField); return container; } 27
  12. 28

  13. public class VisualElementEditor : Editor { public override VisualElement CreateInspectorGUI()

    { var container = new VisualElement(); var iterator = serializedObject.GetIterator(); if (!iterator.NextVisible(true)) return container; while (iterator.NextVisible(false)) { var propertyField = new PropertyField(iterator.Copy()) { name = "PropertyField:" + iterator.propertyPath }; if (iterator.propertyPath == "m_Script" && serializedObject.targetObject != null) propertyField.SetEnabled(false); container.Add(propertyField); } return container; } } 31
  14. [Serializable] public class Serialization { [SerializeField] EnumTest enumTest; [SerializeField] Recursion

    recursion; } [Serializable] public class Recursion { [SerializeField] Serialization serialization; } 36
  15. 38

  16. 39

  17. [CustomPropertyDrawer(typeof(Serialization))] public class EnumFieldPropertyDrawer : PropertyDrawer { public override VisualElement

    CreatePropertyGUI(SerializedProperty property) { var container = new VisualElement(); var enumProperty = property.FindPropertyRelative("enumTest"); var enumField = new EnumField(enumProperty.displayName, (EnumTest)enumProperty.enumValueIndex) { bindingPath = enumProperty.propertyPath }; container.Add(enumField); var recursionProperty = property.FindPropertyRelative("recursion"); var recursionField = new PropertyField(recursionProperty); container.Add(recursionField); return container; } } 42
  18. 44

  19. 46

  20. 48

  21. static VisualElement CreateEnumFieldAsInteger(SerializedProperty property) { var container = new VisualElement();

    Assert.AreEqual(property.propertyType, SerializedPropertyType.Integer); var integerField = new IntegerField { bindingPath = property.propertyPath, style = { display = new StyleEnum<DisplayStyle>(DisplayStyle.None) } }; container.Add(integerField); var popupField = new PopupField<EnumTest>(property.displayName, Enum.GetValues(typeof(EnumTest)).Cast<EnumTest>().ToList(), property.intValue); popupField.RegisterValueChangedCallback(e => { var newValue = (int)(object) e.newValue; if (property.intValue == newValue) return; property.intValue = newValue; property.serializedObject.ApplyModifiedProperties(); }); container.Add(popupField); integerField.RegisterValueChangedCallback(e => { var newValue = (EnumTest)(object) e.newValue; popupField.SetValueWithoutNotify(newValue); }); return container; } 51
  22. var integerField = new IntegerField { bindingPath = property.propertyPath, style

    = { display = new StyleEnum<DisplayStyle>(DisplayStyle.None) } }; container.Add(integerField); 52
  23. var popupField = new PopupField<EnumTest>(property.displayName, Enum.GetValues(typeof(EnumTest)).Cast<EnumTest>().ToList(), property.intValue); popupField.RegisterValueChangedCallback(e => {

    var newValue = (int)(object) e.newValue; if (property.intValue == newValue) return; property.intValue = newValue; property.serializedObject.ApplyModifiedProperties(); }); container.Add(popupField); 53
  24. // Undo ରԠ integerField.RegisterValueChangedCallback(e => { var newValue = (EnumTest)(object)

    e.newValue; popupField.SetValueWithoutNotify(newValue); }); 54
  25. static VisualElement CreateEnumFieldAsEnum(SerializedProperty property) { Assert.AreEqual(property.propertyType, SerializedPropertyType.Enum); // 2019.3 or

    newer var field = new EnumField(property.displayName, (EnumTest)property.enumValueIndex) { bindingPath = property.propertyPath }; field.Bind(property.serializedObject); return field; } 56
  26. static VisualElement CreateEnumField(SerializedProperty property) { switch (property.propertyType) { case SerializedPropertyType.Enum:

    { return CreateEnumFieldAsEnum(property); } case SerializedPropertyType.Integer: { return CreateEnumFieldAsInteger(property); } default: throw new ArgumentOutOfRangeException(); } } 58
  27. public override VisualElement CreatePropertyGUI(SerializedProperty property) { var container = new

    VisualElement(); var enumProperty = property.FindPropertyRelative("enumTest"); var enumField = CreateEnumField(enumProperty); container.Add(enumField); var recursionProperty = property.FindPropertyRelative("recursion"); var recursionField = new PropertyField(recursionProperty); container.Add(recursionField); return container; } 60
  28. 61

  29. static VisualElement CreateSerializationField(SerializedProperty property) { Assert.AreEqual(property.type, nameof(Serialization)); var container =

    new VisualElement(); var enumProperty = property.FindPropertyRelative("enumTest"); var enumField = CreateEnumField(enumProperty); container.Add(enumField); var recursionProperty = property.FindPropertyRelative("recursion.serialization"); var recursionField = CreateSerializationField(recursionProperty); container.Add(recursionField); return container; } 64
  30. 66

  31. ճ਺੍ݶ͸ࣗ෼Ͱ؅ཧ static VisualElement CreateSerializationField(SerializedProperty property, int depth = 0) {

    if (depth > 3) return new VisualElement(); Assert.AreEqual(property.type, nameof(Serialization)); var container = new VisualElement(); var enumProperty = property.FindPropertyRelative("enumTest"); var enumField = CreateEnumField(enumProperty); container.Add(enumField); var recursionProperty = property.FindPropertyRelative("recursion.serialization"); var recursionField = CreateSerializationField(recursionProperty, depth + 1); container.Add(recursionField); return container; } 68
  32. 69

  33. static VisualElement CreateEnumFieldAsString(SerializedProperty property) { Assert.AreEqual(property.propertyType, SerializedPropertyType.Enum); var enumField =

    new PopupField<string>(property.displayName) { bindingPath = property.propertyPath }; return enumField; } https:/ /forum.unity.com/threads/cant-create-bindings-for-an-enum- not-compatible.728111/#post-4873661 73