|
| 1 | +// This should be in an editor only assembly. |
| 2 | + |
| 3 | +#if UNITY_EDITOR |
| 4 | + |
| 5 | +using System; |
| 6 | +using UnityEditor; |
| 7 | +using UnityEngine.InputSystem.OnScreen; |
| 8 | +using UnityEngine.UI; |
| 9 | + |
| 10 | +namespace UnityEngine.InputSystem.Samples.RebindUI |
| 11 | +{ |
| 12 | + //[CustomEditor(typeof(OnScreenControlUI))] |
| 13 | + public class OnScreenControlEditor : UnityEditor.Editor |
| 14 | + { |
| 15 | + #region Unity Editor Menu Extensions |
| 16 | + |
| 17 | + private const int Priority = 10; |
| 18 | + private const int RigPriority = 21; // Note: Diff > 10 inserts separator |
| 19 | + private const string PrimaryStickControlPath = "<Gamepad>/leftStick"; |
| 20 | + private const string SecondaryStickControlPath = "<Gamepad>/rightStick"; |
| 21 | + private const string PrimaryButtonControlPath = "<Gamepad>/buttonSouth"; |
| 22 | + private const string SecondaryButtonControlPath = "<Gamepad>/buttonEast"; |
| 23 | + private const string Menu = "GameObject/Input System/"; |
| 24 | + |
| 25 | + private enum UIIntegration |
| 26 | + { |
| 27 | + None = 0, |
| 28 | + UGUI = 1, |
| 29 | + UIElements = 2, |
| 30 | + } |
| 31 | + |
| 32 | + [MenuItem(Menu + "On-Screen Button", false, Priority)] |
| 33 | + private static void CreateOnScreenButton(MenuCommand menuCommand) |
| 34 | + { |
| 35 | + FinalizeGameObject(CreateButton(PrimaryButtonControlPath, UIIntegration.None), menuCommand.context); |
| 36 | + } |
| 37 | + |
| 38 | + [MenuItem(Menu + "On-Screen Button (UI)", false, Priority)] |
| 39 | + private static void CreateOnScreenButtonUI(MenuCommand menuCommand) |
| 40 | + { |
| 41 | + FinalizeGameObject(CreateButton(PrimaryButtonControlPath, UIIntegration.UGUI), menuCommand.context); |
| 42 | + } |
| 43 | + |
| 44 | + [MenuItem(Menu + "On-Screen Stick", false, Priority)] |
| 45 | + private static void CreateOnScreenStick(MenuCommand menuCommand) |
| 46 | + { |
| 47 | + var go = CreateStick(PrimaryStickControlPath, UIIntegration.None); |
| 48 | + FinalizeGameObject(go, menuCommand.context); |
| 49 | + } |
| 50 | + |
| 51 | + [MenuItem(Menu + "On-Screen Gamepad (1 Stick, 2 Buttons)", isValidateFunction: false, RigPriority)] |
| 52 | + private static void CreateOnScreenGamepad1Stick2Buttons(MenuCommand menuCommand) |
| 53 | + { |
| 54 | + var go = CreateGamepad1Stick2Buttons(UIIntegration.None); |
| 55 | + FinalizeGameObject(go: go, menuCommand.context); |
| 56 | + } |
| 57 | + |
| 58 | + [MenuItem(Menu + "On-Screen Gamepad (2 Sticks, 2 Buttons)", isValidateFunction: false, RigPriority)] |
| 59 | + private static void CreateOnScreenGamepad2Sticks2Buttons(MenuCommand menuCommand) |
| 60 | + { |
| 61 | + var go = CreateGamepad2Stick2Buttons(UIIntegration.None); |
| 62 | + FinalizeGameObject(go, menuCommand.context); |
| 63 | + } |
| 64 | + |
| 65 | + private static void FinalizeGameObject(GameObject go, Object parent) |
| 66 | + { |
| 67 | + // Ensure it gets parented correctly if a context object was selected |
| 68 | + GameObjectUtility.SetParentAndAlign(go, parent as GameObject); |
| 69 | + |
| 70 | + // Register the creation in the undo system |
| 71 | + Undo.RegisterCreatedObjectUndo(go, $"Create {go.name}"); |
| 72 | + |
| 73 | + // Select the newly created object |
| 74 | + Selection.activeObject = go; |
| 75 | + } |
| 76 | + |
| 77 | + private static GameObject CreateStick(string controlPath, UIIntegration uiIntegration, string name = "OnScreenStick") |
| 78 | + { |
| 79 | + // There is currently no difference between stick and button apart from path. |
| 80 | + return CreateButton(controlPath, uiIntegration, name); |
| 81 | + } |
| 82 | + |
| 83 | + private static GameObject CreateButton(string controlPath, UIIntegration uiIntegration, string name = "OnScreenButton") |
| 84 | + { |
| 85 | + var go = new GameObject(name: name); |
| 86 | + |
| 87 | + var control = go.AddComponent<CustomOnScreenControl>(); |
| 88 | + control.curve = Curve.Linear; |
| 89 | + control.stickRadiusMillimeters = 7.2f; |
| 90 | + control.bounds = new Rect(0.0f, 0.5f, 0.5f, 0.0f); |
| 91 | + control.controlPath = controlPath; |
| 92 | + |
| 93 | + switch (uiIntegration) |
| 94 | + { |
| 95 | + case UIIntegration.UGUI: |
| 96 | + { |
| 97 | + // When used with UGUI we want the on-screen control to be a canvas object |
| 98 | + go.AddComponent<RectTransform>(); |
| 99 | + |
| 100 | + var uiButtonGo = new GameObject("Button"); |
| 101 | + var rectTransform = uiButtonGo.AddComponent<RectTransform>(); |
| 102 | + var uiButton = uiButtonGo.AddComponent<RawImage>(); |
| 103 | + uiButton.color = new Color(0.5f, 0.5f, 0.5f, 0.5f); |
| 104 | + uiButton.raycastTarget = false; |
| 105 | + uiButton.transform.parent = go.transform; |
| 106 | + |
| 107 | + var controlUI = go.AddComponent<OnScreenControlUI>(); |
| 108 | + controlUI.control = control; |
| 109 | + controlUI.bounds = rectTransform; |
| 110 | + } |
| 111 | + break; |
| 112 | + case UIIntegration.UIElements: |
| 113 | + throw new NotImplementedException("UIElements support not yet implemented"); |
| 114 | + break; |
| 115 | + case UIIntegration.None: |
| 116 | + default: |
| 117 | + break; |
| 118 | + } |
| 119 | + |
| 120 | + return go; |
| 121 | + } |
| 122 | + |
| 123 | + private static GameObject CreateGamepad1Stick2Buttons(UIIntegration uiIntegration) |
| 124 | + { |
| 125 | + var gamepad = new GameObject(MakeName("Gamepad 1-Stick 2-Buttons", uiIntegration)); |
| 126 | + |
| 127 | + var stick = CreateStick(PrimaryStickControlPath, uiIntegration); |
| 128 | + stick.transform.SetParent(gamepad.transform); |
| 129 | + |
| 130 | + var primaryButton = CreateButton(PrimaryButtonControlPath, uiIntegration, "PrimaryButton"); |
| 131 | + primaryButton.transform.SetParent(gamepad.transform); |
| 132 | + |
| 133 | + var secondaryButton = CreateButton(SecondaryButtonControlPath, uiIntegration, "SecondaryButton"); |
| 134 | + secondaryButton.transform.SetParent(gamepad.transform); |
| 135 | + |
| 136 | + return gamepad; |
| 137 | + } |
| 138 | + |
| 139 | + private static GameObject CreateGamepad2Stick2Buttons(UIIntegration uiIntegration) |
| 140 | + { |
| 141 | + var gamepad = new GameObject(MakeName("Gamepad 2-Sticks 2-Buttons", uiIntegration)); |
| 142 | + |
| 143 | + var primaryStick = CreateStick(PrimaryStickControlPath, uiIntegration, "Primary Stick"); |
| 144 | + primaryStick.transform.SetParent(gamepad.transform); |
| 145 | + |
| 146 | + var secondaryStick = CreateStick(SecondaryStickControlPath, uiIntegration, "Secondary Stick"); |
| 147 | + secondaryStick.transform.SetParent(gamepad.transform); |
| 148 | + |
| 149 | + var primaryButton = CreateButton(PrimaryButtonControlPath, uiIntegration, "PrimaryButton"); |
| 150 | + primaryButton.transform.SetParent(gamepad.transform); |
| 151 | + |
| 152 | + var secondaryButton = CreateButton(SecondaryButtonControlPath, uiIntegration, "SecondaryButton"); |
| 153 | + secondaryButton.transform.SetParent(gamepad.transform); |
| 154 | + |
| 155 | + return gamepad; |
| 156 | + } |
| 157 | + |
| 158 | + private static string MakeName(string name, UIIntegration uiIntegration) |
| 159 | + { |
| 160 | + switch (uiIntegration) |
| 161 | + { |
| 162 | + case UIIntegration.UGUI: return name + " (UI)"; |
| 163 | + case UIIntegration.None: return name + " (UI Elements)"; |
| 164 | + default: return name; |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + #endregion |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +#endif // UNITY_EDITOR |
0 commit comments