A game about forced loneliness, made by TACStudios
at master 435 lines 19 kB view raw
1using System; 2using UnityEditor.SceneManagement; 3using UnityEditor.EventSystems; 4using UnityEngine; 5using UnityEngine.EventSystems; 6using UnityEngine.UI; 7 8namespace UnityEditor.UI 9{ 10 /// <summary> 11 /// This script adds the UI menu options to the Unity Editor. 12 /// </summary> 13 14 static internal class MenuOptions 15 { 16 enum MenuOptionsPriorityOrder { 17 // 2000 - Text (TMP) 18 Image = 2001, 19 RawImage = 2002, 20 Panel = 2003, 21 // 2020 - Button (TMP) 22 Toggle = 2021, 23 // 2022 - Dropdown (TMP) 24 // 2023 - Input Field (TMP) 25 Slider = 2024, 26 Scrollbar = 2025, 27 ScrollView = 2026, 28 Canvas = 2060, 29 EventSystem = 2061, 30 Text = 2080, 31 Button = 2081, 32 Dropdown = 2082, 33 InputField = 2083, 34 }; 35 36 private const string kUILayerName = "UI"; 37 38 private const string kStandardSpritePath = "UI/Skin/UISprite.psd"; 39 private const string kBackgroundSpritePath = "UI/Skin/Background.psd"; 40 private const string kInputFieldBackgroundPath = "UI/Skin/InputFieldBackground.psd"; 41 private const string kKnobPath = "UI/Skin/Knob.psd"; 42 private const string kCheckmarkPath = "UI/Skin/Checkmark.psd"; 43 private const string kDropdownArrowPath = "UI/Skin/DropdownArrow.psd"; 44 private const string kMaskPath = "UI/Skin/UIMask.psd"; 45 46 static private DefaultControls.Resources s_StandardResources; 47 48 static private DefaultControls.Resources GetStandardResources() 49 { 50 if (s_StandardResources.standard == null) 51 { 52 s_StandardResources.standard = AssetDatabase.GetBuiltinExtraResource<Sprite>(kStandardSpritePath); 53 s_StandardResources.background = AssetDatabase.GetBuiltinExtraResource<Sprite>(kBackgroundSpritePath); 54 s_StandardResources.inputField = AssetDatabase.GetBuiltinExtraResource<Sprite>(kInputFieldBackgroundPath); 55 s_StandardResources.knob = AssetDatabase.GetBuiltinExtraResource<Sprite>(kKnobPath); 56 s_StandardResources.checkmark = AssetDatabase.GetBuiltinExtraResource<Sprite>(kCheckmarkPath); 57 s_StandardResources.dropdown = AssetDatabase.GetBuiltinExtraResource<Sprite>(kDropdownArrowPath); 58 s_StandardResources.mask = AssetDatabase.GetBuiltinExtraResource<Sprite>(kMaskPath); 59 } 60 return s_StandardResources; 61 } 62 63 private class DefaultEditorFactory : DefaultControls.IFactoryControls 64 { 65 public static DefaultEditorFactory Default = new DefaultEditorFactory(); 66 67 public GameObject CreateGameObject(string name, params Type[] components) 68 { 69 return ObjectFactory.CreateGameObject(name, components); 70 } 71 } 72 73 private class FactorySwapToEditor : IDisposable 74 { 75 DefaultControls.IFactoryControls factory; 76 77 public FactorySwapToEditor() 78 { 79 factory = DefaultControls.factory; 80 DefaultControls.factory = DefaultEditorFactory.Default; 81 } 82 83 public void Dispose() 84 { 85 DefaultControls.factory = factory; 86 } 87 } 88 89 private static void SetPositionVisibleinSceneView(RectTransform canvasRTransform, RectTransform itemTransform) 90 { 91 SceneView sceneView = SceneView.lastActiveSceneView; 92 93 // Couldn't find a SceneView. Don't set position. 94 if (sceneView == null || sceneView.camera == null) 95 return; 96 97 // Create world space Plane from canvas position. 98 Vector2 localPlanePosition; 99 Camera camera = sceneView.camera; 100 Vector3 position = Vector3.zero; 101 if (RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRTransform, new Vector2(camera.pixelWidth / 2, camera.pixelHeight / 2), camera, out localPlanePosition)) 102 { 103 // Adjust for canvas pivot 104 localPlanePosition.x = localPlanePosition.x + canvasRTransform.sizeDelta.x * canvasRTransform.pivot.x; 105 localPlanePosition.y = localPlanePosition.y + canvasRTransform.sizeDelta.y * canvasRTransform.pivot.y; 106 107 localPlanePosition.x = Mathf.Clamp(localPlanePosition.x, 0, canvasRTransform.sizeDelta.x); 108 localPlanePosition.y = Mathf.Clamp(localPlanePosition.y, 0, canvasRTransform.sizeDelta.y); 109 110 // Adjust for anchoring 111 position.x = localPlanePosition.x - canvasRTransform.sizeDelta.x * itemTransform.anchorMin.x; 112 position.y = localPlanePosition.y - canvasRTransform.sizeDelta.y * itemTransform.anchorMin.y; 113 114 Vector3 minLocalPosition; 115 minLocalPosition.x = canvasRTransform.sizeDelta.x * (0 - canvasRTransform.pivot.x) + itemTransform.sizeDelta.x * itemTransform.pivot.x; 116 minLocalPosition.y = canvasRTransform.sizeDelta.y * (0 - canvasRTransform.pivot.y) + itemTransform.sizeDelta.y * itemTransform.pivot.y; 117 118 Vector3 maxLocalPosition; 119 maxLocalPosition.x = canvasRTransform.sizeDelta.x * (1 - canvasRTransform.pivot.x) - itemTransform.sizeDelta.x * itemTransform.pivot.x; 120 maxLocalPosition.y = canvasRTransform.sizeDelta.y * (1 - canvasRTransform.pivot.y) - itemTransform.sizeDelta.y * itemTransform.pivot.y; 121 122 position.x = Mathf.Clamp(position.x, minLocalPosition.x, maxLocalPosition.x); 123 position.y = Mathf.Clamp(position.y, minLocalPosition.y, maxLocalPosition.y); 124 } 125 126 itemTransform.anchoredPosition = position; 127 itemTransform.localRotation = Quaternion.identity; 128 itemTransform.localScale = Vector3.one; 129 } 130 131 private static void PlaceUIElementRoot(GameObject element, MenuCommand menuCommand) 132 { 133 GameObject parent = menuCommand.context as GameObject; 134 bool explicitParentChoice = true; 135 if (parent == null) 136 { 137 parent = GetOrCreateCanvasGameObject(); 138 explicitParentChoice = false; 139 140 // If in Prefab Mode, Canvas has to be part of Prefab contents, 141 // otherwise use Prefab root instead. 142 PrefabStage prefabStage = PrefabStageUtility.GetCurrentPrefabStage(); 143 if (prefabStage != null && !prefabStage.IsPartOfPrefabContents(parent)) 144 parent = prefabStage.prefabContentsRoot; 145 } 146 if (parent.GetComponentsInParent<Canvas>(true).Length == 0) 147 { 148 // Create canvas under context GameObject, 149 // and make that be the parent which UI element is added under. 150 GameObject canvas = MenuOptions.CreateNewUI(); 151 Undo.SetTransformParent(canvas.transform, parent.transform, ""); 152 parent = canvas; 153 } 154 155 GameObjectUtility.EnsureUniqueNameForSibling(element); 156 157 SetParentAndAlign(element, parent); 158 if (!explicitParentChoice) // not a context click, so center in sceneview 159 SetPositionVisibleinSceneView(parent.GetComponent<RectTransform>(), element.GetComponent<RectTransform>()); 160 161 // This call ensure any change made to created Objects after they where registered will be part of the Undo. 162 Undo.RegisterFullObjectHierarchyUndo(parent == null ? element : parent, ""); 163 164 // We have to fix up the undo name since the name of the object was only known after reparenting it. 165 Undo.SetCurrentGroupName("Create " + element.name); 166 167 Selection.activeGameObject = element; 168 } 169 170 private static void SetParentAndAlign(GameObject child, GameObject parent) 171 { 172 if (parent == null) 173 return; 174 175 Undo.SetTransformParent(child.transform, parent.transform, ""); 176 177 RectTransform rectTransform = child.transform as RectTransform; 178 if (rectTransform) 179 { 180 rectTransform.anchoredPosition = Vector2.zero; 181 Vector3 localPosition = rectTransform.localPosition; 182 localPosition.z = 0; 183 rectTransform.localPosition = localPosition; 184 } 185 else 186 { 187 child.transform.localPosition = Vector3.zero; 188 } 189 child.transform.localRotation = Quaternion.identity; 190 child.transform.localScale = Vector3.one; 191 192 SetLayerRecursively(child, parent.layer); 193 } 194 195 private static void SetLayerRecursively(GameObject go, int layer) 196 { 197 go.layer = layer; 198 Transform t = go.transform; 199 for (int i = 0; i < t.childCount; i++) 200 SetLayerRecursively(t.GetChild(i).gameObject, layer); 201 } 202 203 // Graphic elements 204 205 [MenuItem("GameObject/UI/Image", false, (int)MenuOptionsPriorityOrder.Image)] 206 static public void AddImage(MenuCommand menuCommand) 207 { 208 GameObject go; 209 using (new FactorySwapToEditor()) 210 go = DefaultControls.CreateImage(GetStandardResources()); 211 PlaceUIElementRoot(go, menuCommand); 212 } 213 214 [MenuItem("GameObject/UI/Raw Image", false, (int)MenuOptionsPriorityOrder.RawImage)] 215 static public void AddRawImage(MenuCommand menuCommand) 216 { 217 GameObject go; 218 using (new FactorySwapToEditor()) 219 go = DefaultControls.CreateRawImage(GetStandardResources()); 220 PlaceUIElementRoot(go, menuCommand); 221 } 222 223 [MenuItem("GameObject/UI/Panel", false, (int)MenuOptionsPriorityOrder.Panel)] 224 static public void AddPanel(MenuCommand menuCommand) 225 { 226 GameObject go; 227 using (new FactorySwapToEditor()) 228 go = DefaultControls.CreatePanel(GetStandardResources()); 229 PlaceUIElementRoot(go, menuCommand); 230 231 // Panel is special, we need to ensure there's no padding after repositioning. 232 RectTransform rect = go.GetComponent<RectTransform>(); 233 rect.anchoredPosition = Vector2.zero; 234 rect.sizeDelta = Vector2.zero; 235 } 236 237 // Controls 238 239 // Toggle is a control you just click on. 240 241 [MenuItem("GameObject/UI/Toggle", false, (int)MenuOptionsPriorityOrder.Toggle)] 242 static public void AddToggle(MenuCommand menuCommand) 243 { 244 GameObject go; 245 using (new FactorySwapToEditor()) 246 go = DefaultControls.CreateToggle(GetStandardResources()); 247 PlaceUIElementRoot(go, menuCommand); 248 } 249 250 // Slider and Scrollbar modify a number 251 252 [MenuItem("GameObject/UI/Slider", false, (int)MenuOptionsPriorityOrder.Slider)] 253 static public void AddSlider(MenuCommand menuCommand) 254 { 255 GameObject go; 256 using (new FactorySwapToEditor()) 257 go = DefaultControls.CreateSlider(GetStandardResources()); 258 PlaceUIElementRoot(go, menuCommand); 259 } 260 261 [MenuItem("GameObject/UI/Scrollbar", false, (int)MenuOptionsPriorityOrder.Scrollbar)] 262 static public void AddScrollbar(MenuCommand menuCommand) 263 { 264 GameObject go; 265 using (new FactorySwapToEditor()) 266 go = DefaultControls.CreateScrollbar(GetStandardResources()); 267 PlaceUIElementRoot(go, menuCommand); 268 } 269 270 [MenuItem("GameObject/UI/Scroll View", false, (int)MenuOptionsPriorityOrder.ScrollView)] 271 static public void AddScrollView(MenuCommand menuCommand) 272 { 273 GameObject go; 274 using (new FactorySwapToEditor()) 275 go = DefaultControls.CreateScrollView(GetStandardResources()); 276 PlaceUIElementRoot(go, menuCommand); 277 } 278 279 // Containers 280 281 [MenuItem("GameObject/UI/Canvas", false, (int)MenuOptionsPriorityOrder.Canvas)] 282 static public void AddCanvas(MenuCommand menuCommand) 283 { 284 var go = CreateNewUI(); 285 SetParentAndAlign(go, menuCommand.context as GameObject); 286 if (go.transform.parent as RectTransform) 287 { 288 RectTransform rect = go.transform as RectTransform; 289 rect.anchorMin = Vector2.zero; 290 rect.anchorMax = Vector2.one; 291 rect.anchoredPosition = Vector2.zero; 292 rect.sizeDelta = Vector2.zero; 293 } 294 Selection.activeGameObject = go; 295 } 296 297 // Legacy Elements 298 299 [MenuItem("GameObject/UI/Legacy/Text", false, (int)MenuOptionsPriorityOrder.Text)] 300 static public void AddText(MenuCommand menuCommand) 301 { 302 GameObject go; 303 using (new FactorySwapToEditor()) 304 go = DefaultControls.CreateText(GetStandardResources()); 305 PlaceUIElementRoot(go, menuCommand); 306 } 307 308 [MenuItem("GameObject/UI/Legacy/Button", false, (int)MenuOptionsPriorityOrder.Button)] 309 static public void AddButton(MenuCommand menuCommand) 310 { 311 GameObject go; 312 using (new FactorySwapToEditor()) 313 go = DefaultControls.CreateButton(GetStandardResources()); 314 PlaceUIElementRoot(go, menuCommand); 315 } 316 317 [MenuItem("GameObject/UI/Legacy/Dropdown", false, (int)MenuOptionsPriorityOrder.Dropdown)] 318 static public void AddDropdown(MenuCommand menuCommand) 319 { 320 GameObject go; 321 using (new FactorySwapToEditor()) 322 go = DefaultControls.CreateDropdown(GetStandardResources()); 323 PlaceUIElementRoot(go, menuCommand); 324 } 325 326 [MenuItem("GameObject/UI/Legacy/Input Field", false, (int)MenuOptionsPriorityOrder.InputField)] 327 public static void AddInputField(MenuCommand menuCommand) 328 { 329 GameObject go; 330 using (new FactorySwapToEditor()) 331 go = DefaultControls.CreateInputField(GetStandardResources()); 332 PlaceUIElementRoot(go, menuCommand); 333 } 334 335 // Helper methods 336 337 static public GameObject CreateNewUI() 338 { 339 // Root for the UI 340 var root = ObjectFactory.CreateGameObject("Canvas", typeof(Canvas), typeof(CanvasScaler), typeof(GraphicRaycaster)); 341 root.layer = LayerMask.NameToLayer(kUILayerName); 342 Canvas canvas = root.GetComponent<Canvas>(); 343 canvas.renderMode = RenderMode.ScreenSpaceOverlay; 344 345 // Works for all stages. 346 StageUtility.PlaceGameObjectInCurrentStage(root); 347 bool customScene = false; 348 PrefabStage prefabStage = PrefabStageUtility.GetCurrentPrefabStage(); 349 if (prefabStage != null) 350 { 351 Undo.SetTransformParent(root.transform, prefabStage.prefabContentsRoot.transform, ""); 352 customScene = true; 353 } 354 355 Undo.SetCurrentGroupName("Create " + root.name); 356 357 // If there is no event system add one... 358 // No need to place event system in custom scene as these are temporary anyway. 359 // It can be argued for or against placing it in the user scenes, 360 // but let's not modify scene user is not currently looking at. 361 if (!customScene) 362 CreateEventSystem(false); 363 return root; 364 } 365 366 [MenuItem("GameObject/UI/Event System", false, (int)MenuOptionsPriorityOrder.EventSystem)] 367 public static void CreateEventSystem(MenuCommand menuCommand) 368 { 369 GameObject parent = menuCommand.context as GameObject; 370 CreateEventSystem(true, parent); 371 } 372 373 private static void CreateEventSystem(bool select) 374 { 375 CreateEventSystem(select, null); 376 } 377 378 private static void CreateEventSystem(bool select, GameObject parent) 379 { 380 StageHandle stage = parent == null ? StageUtility.GetCurrentStageHandle() : StageUtility.GetStageHandle(parent); 381 var esys = stage.FindComponentOfType<EventSystem>(); 382 if (esys == null) 383 { 384 var eventSystem = ObjectFactory.CreateGameObject("EventSystem"); 385 if (parent == null) 386 StageUtility.PlaceGameObjectInCurrentStage(eventSystem); 387 else 388 SetParentAndAlign(eventSystem, parent); 389 esys = ObjectFactory.AddComponent<EventSystem>(eventSystem); 390 InputModuleComponentFactory.AddInputModule(eventSystem); 391 392 Undo.RegisterCreatedObjectUndo(eventSystem, "Create " + eventSystem.name); 393 } 394 395 if (select && esys != null) 396 { 397 Selection.activeGameObject = esys.gameObject; 398 } 399 } 400 401 // Helper function that returns a Canvas GameObject; preferably a parent of the selection, or other existing Canvas. 402 static public GameObject GetOrCreateCanvasGameObject() 403 { 404 GameObject selectedGo = Selection.activeGameObject; 405 406 // Try to find a gameobject that is the selected GO or one if its parents. 407 Canvas canvas = (selectedGo != null) ? selectedGo.GetComponentInParent<Canvas>() : null; 408 if (IsValidCanvas(canvas)) 409 return canvas.gameObject; 410 411 // No canvas in selection or its parents? Then use any valid canvas. 412 // We have to find all loaded Canvases, not just the ones in main scenes. 413 Canvas[] canvasArray = StageUtility.GetCurrentStageHandle().FindComponentsOfType<Canvas>(); 414 for (int i = 0; i < canvasArray.Length; i++) 415 if (IsValidCanvas(canvasArray[i])) 416 return canvasArray[i].gameObject; 417 418 // No canvas in the scene at all? Then create a new one. 419 return MenuOptions.CreateNewUI(); 420 } 421 422 static bool IsValidCanvas(Canvas canvas) 423 { 424 if (canvas == null || !canvas.gameObject.activeInHierarchy) 425 return false; 426 427 // It's important that the non-editable canvas from a prefab scene won't be rejected, 428 // but canvases not visible in the Hierarchy at all do. Don't check for HideAndDontSave. 429 if (EditorUtility.IsPersistent(canvas) || (canvas.hideFlags & HideFlags.HideInHierarchy) != 0) 430 return false; 431 432 return StageUtility.GetStageHandle(canvas.gameObject) == StageUtility.GetCurrentStageHandle(); 433 } 434 } 435}