No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GameObjectCreation.cs 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using UnityEditor.SceneManagement;
  3. using UnityEngine;
  4. using UnityEngine.U2D;
  5. namespace UnityEditor.U2D
  6. {
  7. static class GameObjectCreation
  8. {
  9. const int k_PixelPerfectCameraGameObjectMenuPriority = 5;
  10. #if !ENABLE_URP
  11. [MenuItem("GameObject/2D Object/Pixel Perfect Camera", priority = k_PixelPerfectCameraGameObjectMenuPriority)]
  12. static void GameObjectCreatePixelPerfectCamera(MenuCommand menuCommand)
  13. {
  14. var go = CreateGameObject("Pixel Perfect Camera", menuCommand, new []{typeof(PixelPerfectCamera)});
  15. go.GetComponent<PixelPerfectCamera>().pixelSnapping = true;
  16. }
  17. #endif
  18. static public GameObject CreateGameObject(string name, MenuCommand menuCommand, params Type[] components)
  19. {
  20. var parent = menuCommand.context as GameObject;
  21. var newGO = ObjectFactory.CreateGameObject(name, components);
  22. newGO.name = name;
  23. Selection.activeObject = newGO;
  24. Place(newGO, parent);
  25. if (EditorSettings.defaultBehaviorMode == EditorBehaviorMode.Mode2D)
  26. {
  27. var position = newGO.transform.position;
  28. position.z = 0;
  29. newGO.transform.position = position;
  30. }
  31. Undo.RegisterCreatedObjectUndo(newGO, string.Format("Create {0}", name));
  32. return newGO;
  33. }
  34. internal static void Place(GameObject go, GameObject parentTransform)
  35. {
  36. if (parentTransform != null)
  37. {
  38. var transform = go.transform;
  39. Undo.SetTransformParent(transform, parentTransform.transform, "Reparenting");
  40. transform.localPosition = Vector3.zero;
  41. transform.localRotation = Quaternion.identity;
  42. transform.localScale = Vector3.one;
  43. go.layer = parentTransform.gameObject.layer;
  44. if (parentTransform.GetComponent<RectTransform>())
  45. ObjectFactory.AddComponent<RectTransform>(go);
  46. }
  47. else
  48. {
  49. PlaceGameObjectInFrontOfSceneView(go);
  50. StageUtility.PlaceGameObjectInCurrentStage(go); // may change parent
  51. }
  52. // Only at this point do we know the actual parent of the object and can modify its name accordingly.
  53. GameObjectUtility.EnsureUniqueNameForSibling(go);
  54. Undo.SetCurrentGroupName("Create " + go.name);
  55. Selection.activeGameObject = go;
  56. }
  57. internal static void PlaceGameObjectInFrontOfSceneView(GameObject go)
  58. {
  59. var view = SceneView.lastActiveSceneView;
  60. if (view != null)
  61. {
  62. view.MoveToView(go.transform);
  63. }
  64. }
  65. }
  66. }