InputSystemUIInputModuleEditor.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #if UNITY_INPUT_SYSTEM_ENABLE_UI && UNITY_EDITOR
  2. using System;
  3. using System.Linq;
  4. using UnityEditor;
  5. ////TODO: add button to automatically set up gamepad mouse cursor support
  6. namespace UnityEngine.InputSystem.UI.Editor
  7. {
  8. [CustomEditor(typeof(InputSystemUIInputModule))]
  9. internal class InputSystemUIInputModuleEditor : UnityEditor.Editor
  10. {
  11. private static InputActionReference GetActionReferenceFromAssets(InputActionReference[] actions, params string[] actionNames)
  12. {
  13. foreach (var actionName in actionNames)
  14. {
  15. foreach (var action in actions)
  16. {
  17. if (string.Compare(action.action.name, actionName, StringComparison.InvariantCultureIgnoreCase) == 0)
  18. return action;
  19. }
  20. }
  21. return null;
  22. }
  23. private static InputActionReference[] GetAllActionsFromAsset(InputActionAsset actions)
  24. {
  25. if (actions != null)
  26. {
  27. var path = AssetDatabase.GetAssetPath(actions);
  28. var assets = AssetDatabase.LoadAllAssetsAtPath(path);
  29. return assets.Where(asset => asset is InputActionReference).Cast<InputActionReference>().OrderBy(x => x.name).ToArray();
  30. }
  31. return null;
  32. }
  33. private static readonly string[] s_ActionNames =
  34. {
  35. "Point",
  36. "LeftClick",
  37. "MiddleClick",
  38. "RightClick",
  39. "ScrollWheel",
  40. "Move",
  41. "Submit",
  42. "Cancel",
  43. "TrackedDevicePosition",
  44. "TrackedDeviceOrientation"
  45. };
  46. private static readonly string[] s_ActionNiceNames =
  47. {
  48. "Point",
  49. "Left Click",
  50. "Middle Click",
  51. "Right Click",
  52. "Scroll Wheel",
  53. "Move",
  54. "Submit",
  55. "Cancel",
  56. "Tracked Position",
  57. "Tracked Orientation"
  58. };
  59. private SerializedProperty[] m_ReferenceProperties;
  60. private SerializedProperty m_ActionsAsset;
  61. private InputActionReference[] m_AvailableActionsInAsset;
  62. private string[] m_AvailableActionsInAssetNames;
  63. public void OnEnable()
  64. {
  65. var numActions = s_ActionNames.Length;
  66. m_ReferenceProperties = new SerializedProperty[numActions];
  67. for (var i = 0; i < numActions; i++)
  68. m_ReferenceProperties[i] = serializedObject.FindProperty($"m_{s_ActionNames[i]}Action");
  69. m_ActionsAsset = serializedObject.FindProperty("m_ActionsAsset");
  70. m_AvailableActionsInAsset = GetAllActionsFromAsset(m_ActionsAsset.objectReferenceValue as InputActionAsset);
  71. // Ugly hack: GenericMenu interprets "/" as a submenu path. But luckily, "/" is not the only slash we have in Unicode.
  72. m_AvailableActionsInAssetNames = new[] { "None" }.Concat(m_AvailableActionsInAsset?.Select(x => x.name.Replace("/", "\uFF0F")) ?? new string[0]).ToArray();
  73. }
  74. public static void ReassignActions(InputSystemUIInputModule module, InputActionAsset action)
  75. {
  76. module.actionsAsset = action;
  77. var assets = GetAllActionsFromAsset(action);
  78. if (assets != null)
  79. {
  80. module.point = GetActionReferenceFromAssets(assets, module.point?.action?.name, "Point", "MousePosition", "Mouse Position");
  81. module.leftClick = GetActionReferenceFromAssets(assets, module.leftClick?.action?.name, "Click", "LeftClick", "Left Click");
  82. module.rightClick = GetActionReferenceFromAssets(assets, module.rightClick?.action?.name, "RightClick", "Right Click", "ContextClick", "Context Click", "ContextMenu", "Context Menu");
  83. module.middleClick = GetActionReferenceFromAssets(assets, module.middleClick?.action?.name, "MiddleClick", "Middle Click");
  84. module.scrollWheel = GetActionReferenceFromAssets(assets, module.scrollWheel?.action?.name, "ScrollWheel", "Scroll Wheel", "Scroll", "Wheel");
  85. module.move = GetActionReferenceFromAssets(assets, module.move?.action?.name, "Navigate", "Move");
  86. module.submit = GetActionReferenceFromAssets(assets, module.submit?.action?.name, "Submit");
  87. module.cancel = GetActionReferenceFromAssets(assets, module.cancel?.action?.name, "Cancel", "Esc", "Escape");
  88. module.trackedDevicePosition = GetActionReferenceFromAssets(assets, module.trackedDevicePosition?.action?.name, "TrackedDevicePosition", "Position");
  89. module.trackedDeviceOrientation = GetActionReferenceFromAssets(assets, module.trackedDeviceOrientation?.action?.name, "TrackedDeviceOrientation", "Orientation");
  90. }
  91. }
  92. public override void OnInspectorGUI()
  93. {
  94. base.OnInspectorGUI();
  95. EditorGUI.BeginChangeCheck();
  96. EditorGUILayout.PropertyField(m_ActionsAsset);
  97. if (EditorGUI.EndChangeCheck())
  98. {
  99. var actions = m_ActionsAsset.objectReferenceValue as InputActionAsset;
  100. if (actions != null)
  101. {
  102. serializedObject.ApplyModifiedProperties();
  103. ReassignActions(target as InputSystemUIInputModule, actions);
  104. serializedObject.Update();
  105. }
  106. // reinitialize action types
  107. OnEnable();
  108. }
  109. var numActions = s_ActionNames.Length;
  110. for (var i = 0; i < numActions; i++)
  111. {
  112. if (m_AvailableActionsInAsset == null)
  113. continue;
  114. var index = Array.IndexOf(m_AvailableActionsInAsset, m_ReferenceProperties[i].objectReferenceValue) + 1;
  115. EditorGUI.BeginChangeCheck();
  116. index = EditorGUILayout.Popup(s_ActionNiceNames[i], index, m_AvailableActionsInAssetNames);
  117. if (EditorGUI.EndChangeCheck())
  118. m_ReferenceProperties[i].objectReferenceValue = index > 0 ? m_AvailableActionsInAsset[index - 1] : null;
  119. }
  120. if (GUI.changed)
  121. serializedObject.ApplyModifiedProperties();
  122. }
  123. }
  124. }
  125. #endif