説明なし
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

NavigationDrawer.cs 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System;
  2. using UnityEditor.UIElements;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.UIElements;
  6. namespace UnityEditor.UI
  7. {
  8. [CustomPropertyDrawer(typeof(Navigation), true)]
  9. /// <summary>
  10. /// This is a PropertyDrawer for Navigation. It is implemented using the standard Unity PropertyDrawer framework.
  11. /// </summary>
  12. public class NavigationDrawer : PropertyDrawer
  13. {
  14. const string kNavigation = "Navigation";
  15. const string kModeProp = "m_Mode";
  16. const string kWrapAroundProp = "m_WrapAround";
  17. const string kSelectOnUpProp = "m_SelectOnUp";
  18. const string kSelectOnDownProp = "m_SelectOnDown";
  19. const string kSelectOnLeftProp = "m_SelectOnLeft";
  20. const string kSelectOnRightProp = "m_SelectOnRight";
  21. const string kHiddenClass = "unity-ui-navigation-hidden";
  22. private class Styles
  23. {
  24. readonly public GUIContent navigationContent;
  25. public Styles()
  26. {
  27. navigationContent = EditorGUIUtility.TrTextContent(kNavigation);
  28. }
  29. }
  30. private static Styles s_Styles = null;
  31. public override void OnGUI(Rect pos, SerializedProperty prop, GUIContent label)
  32. {
  33. if (s_Styles == null)
  34. s_Styles = new Styles();
  35. Rect drawRect = pos;
  36. drawRect.height = EditorGUIUtility.singleLineHeight;
  37. SerializedProperty navigation = prop.FindPropertyRelative(kModeProp);
  38. SerializedProperty wrapAround = prop.FindPropertyRelative(kWrapAroundProp);
  39. Navigation.Mode navMode = GetNavigationMode(navigation);
  40. EditorGUI.PropertyField(drawRect, navigation, s_Styles.navigationContent);
  41. ++EditorGUI.indentLevel;
  42. drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  43. switch (navMode)
  44. {
  45. case Navigation.Mode.Horizontal:
  46. case Navigation.Mode.Vertical:
  47. {
  48. EditorGUI.PropertyField(drawRect, wrapAround);
  49. drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  50. }
  51. break;
  52. case Navigation.Mode.Explicit:
  53. {
  54. SerializedProperty selectOnUp = prop.FindPropertyRelative(kSelectOnUpProp);
  55. SerializedProperty selectOnDown = prop.FindPropertyRelative(kSelectOnDownProp);
  56. SerializedProperty selectOnLeft = prop.FindPropertyRelative(kSelectOnLeftProp);
  57. SerializedProperty selectOnRight = prop.FindPropertyRelative(kSelectOnRightProp);
  58. EditorGUI.PropertyField(drawRect, selectOnUp);
  59. drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  60. EditorGUI.PropertyField(drawRect, selectOnDown);
  61. drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  62. EditorGUI.PropertyField(drawRect, selectOnLeft);
  63. drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  64. EditorGUI.PropertyField(drawRect, selectOnRight);
  65. drawRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  66. }
  67. break;
  68. }
  69. --EditorGUI.indentLevel;
  70. }
  71. static Navigation.Mode GetNavigationMode(SerializedProperty navigation)
  72. {
  73. return (Navigation.Mode)navigation.enumValueIndex;
  74. }
  75. public override float GetPropertyHeight(SerializedProperty prop, GUIContent label)
  76. {
  77. SerializedProperty navigation = prop.FindPropertyRelative(kModeProp);
  78. if (navigation == null)
  79. return EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  80. Navigation.Mode navMode = GetNavigationMode(navigation);
  81. switch (navMode)
  82. {
  83. case Navigation.Mode.None:
  84. return EditorGUIUtility.singleLineHeight;
  85. case Navigation.Mode.Horizontal:
  86. case Navigation.Mode.Vertical:
  87. return 2 * EditorGUIUtility.singleLineHeight + 2 * EditorGUIUtility.standardVerticalSpacing;
  88. case Navigation.Mode.Explicit:
  89. return 5 * EditorGUIUtility.singleLineHeight + 5 * EditorGUIUtility.standardVerticalSpacing;
  90. default:
  91. return EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  92. }
  93. }
  94. PropertyField PrepareField(VisualElement parent, string propertyPath, bool hideable = true, string label = null)
  95. {
  96. var field = new PropertyField(null, label) { bindingPath = propertyPath };
  97. if (hideable) field.AddToClassList(kHiddenClass);
  98. parent.Add(field);
  99. return field;
  100. }
  101. public override VisualElement CreatePropertyGUI(SerializedProperty property)
  102. {
  103. var container = new VisualElement() { name = kNavigation };
  104. var indented = new VisualElement() { name = "Indent" };
  105. indented.AddToClassList("unity-ui-navigation-indent");
  106. var navigation = PrepareField(container, kModeProp, false, kNavigation);
  107. var wrapAround = PrepareField(indented, kWrapAroundProp);
  108. var selectOnUp = PrepareField(indented, kSelectOnUpProp);
  109. var selectOnDown = PrepareField(indented, kSelectOnDownProp);
  110. var selectOnLeft = PrepareField(indented, kSelectOnLeftProp);
  111. var selectOnRight = PrepareField(indented, kSelectOnRightProp);
  112. Action<Navigation.Mode> callback = (value) =>
  113. {
  114. wrapAround.EnableInClassList(kHiddenClass, value != Navigation.Mode.Vertical && value != Navigation.Mode.Horizontal);
  115. selectOnUp.EnableInClassList(kHiddenClass, value != Navigation.Mode.Explicit);
  116. selectOnDown.EnableInClassList(kHiddenClass, value != Navigation.Mode.Explicit);
  117. selectOnLeft.EnableInClassList(kHiddenClass, value != Navigation.Mode.Explicit);
  118. selectOnRight.EnableInClassList(kHiddenClass, value != Navigation.Mode.Explicit);
  119. };
  120. navigation.RegisterValueChangeCallback((e) => callback.Invoke((Navigation.Mode)e.changedProperty.enumValueIndex));
  121. callback.Invoke((Navigation.Mode)property.FindPropertyRelative(kModeProp).enumValueFlag);
  122. container.Add(indented);
  123. return container;
  124. }
  125. }
  126. }