Renderer2DMenus.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using UnityEngine;
  2. using UnityEngine.Experimental.Rendering.Universal;
  3. using UnityEngine.Rendering.Universal;
  4. using UnityEditor;
  5. using UnityEditor.SceneManagement;
  6. using UnityEditor.ProjectWindowCallback;
  7. using UnityEngine.Rendering;
  8. namespace UnityEditor.Experimental.Rendering.Universal
  9. {
  10. static class Renderer2DMenus
  11. {
  12. internal static void PlaceGameObjectInFrontOfSceneView(GameObject go)
  13. {
  14. var sceneViews = SceneView.sceneViews;
  15. if (sceneViews.Count >= 1)
  16. {
  17. SceneView view = SceneView.lastActiveSceneView;
  18. if (!view)
  19. view = sceneViews[0] as SceneView;
  20. if (view)
  21. view.MoveToView(go.transform);
  22. }
  23. }
  24. // This is from GOCreationCommands
  25. internal static void Place(GameObject go, GameObject parent)
  26. {
  27. if (parent != null)
  28. {
  29. var transform = go.transform;
  30. Undo.SetTransformParent(transform, parent.transform, "Reparenting");
  31. transform.localPosition = Vector3.zero;
  32. transform.localRotation = Quaternion.identity;
  33. transform.localScale = Vector3.one;
  34. go.layer = parent.layer;
  35. if (parent.GetComponent<RectTransform>())
  36. ObjectFactory.AddComponent<RectTransform>(go);
  37. }
  38. else
  39. {
  40. PlaceGameObjectInFrontOfSceneView(go);
  41. StageUtility.PlaceGameObjectInCurrentStage(go); // may change parent
  42. go.transform.position = new Vector3(go.transform.position.x, go.transform.position.y, 0);
  43. }
  44. // Only at this point do we know the actual parent of the object and can modify its name accordingly.
  45. GameObjectUtility.EnsureUniqueNameForSibling(go);
  46. Undo.SetCurrentGroupName("Create " + go.name);
  47. //EditorWindow.FocusWindowIfItsOpen<SceneHierarchyWindow>();
  48. Selection.activeGameObject = go;
  49. }
  50. static void CreateLight(MenuCommand menuCommand, string name, Light2D.LightType type)
  51. {
  52. GameObject go = ObjectFactory.CreateGameObject(name, typeof(Light2D));
  53. Light2D light2D = go.GetComponent<Light2D>();
  54. light2D.lightType = type;
  55. var parent = menuCommand.context as GameObject;
  56. Place(go, parent);
  57. Analytics.Light2DData lightData = new Analytics.Light2DData();
  58. lightData.was_create_event = true;
  59. lightData.instance_id = light2D.GetInstanceID();
  60. lightData.light_type = light2D.lightType;
  61. Analytics.Renderer2DAnalytics.instance.SendData(Analytics.AnalyticsDataTypes.k_LightDataString, lightData);
  62. }
  63. static bool CreateLightValidation()
  64. {
  65. return Light2DEditorUtility.IsUsing2DRenderer();
  66. }
  67. [MenuItem("GameObject/Light/2D/Freeform Light 2D (Experimental)", false, -100)]
  68. static void CreateFreeformLight2D(MenuCommand menuCommand)
  69. {
  70. CreateLight(menuCommand, "Freeform Light 2D", Light2D.LightType.Freeform);
  71. }
  72. [MenuItem("GameObject/Light/2D/Freeform Light 2D (Experimental)", true, -100)]
  73. static bool CreateFreeformLight2DValidation()
  74. {
  75. return CreateLightValidation();
  76. }
  77. [MenuItem("GameObject/Light/2D/Sprite Light 2D (Experimental)", false, -100)]
  78. static void CreateSpriteLight2D(MenuCommand menuCommand)
  79. {
  80. CreateLight(menuCommand, "Sprite Light 2D", Light2D.LightType.Sprite);
  81. }
  82. [MenuItem("GameObject/Light/2D/Sprite Light 2D (Experimental)", true, -100)]
  83. static bool CreateSpriteLight2DValidation()
  84. {
  85. return CreateLightValidation();
  86. }
  87. [MenuItem("GameObject/Light/2D/Parametric Light 2D (Experimental)", false, -100)]
  88. static void CreateParametricLight2D(MenuCommand menuCommand)
  89. {
  90. CreateLight(menuCommand, "Parametric Light 2D", Light2D.LightType.Parametric);
  91. }
  92. [MenuItem("GameObject/Light/2D/Parametric Light 2D (Experimental)", true, -100)]
  93. static bool CreateParametricLight2DValidation()
  94. {
  95. return CreateLightValidation();
  96. }
  97. [MenuItem("GameObject/Light/2D/Point Light 2D (Experimental)", false, -100)]
  98. static void CreatePointLight2D(MenuCommand menuCommand)
  99. {
  100. CreateLight(menuCommand, "Point Light 2D", Light2D.LightType.Point);
  101. }
  102. [MenuItem("GameObject/Light/2D/Point Light 2D (Experimental)", true, -100)]
  103. static bool CreatePointLight2DValidation()
  104. {
  105. return CreateLightValidation();
  106. }
  107. [MenuItem("GameObject/Light/2D/Global Light 2D (Experimental)", false, -100)]
  108. static void CreateGlobalLight2D(MenuCommand menuCommand)
  109. {
  110. CreateLight(menuCommand, "Global Light 2D", Light2D.LightType.Global);
  111. }
  112. [MenuItem("GameObject/Light/2D/Global Light 2D (Experimental)", true, -100)]
  113. static bool CreateGlobalLight2DValidation()
  114. {
  115. return CreateLightValidation();
  116. }
  117. [MenuItem("Assets/Create/Rendering/Universal Render Pipeline/2D Renderer (Experimental)", priority = CoreUtils.assetCreateMenuPriority2 + 1)]
  118. static void Create2DRendererData()
  119. {
  120. Renderer2DData.Create2DRendererData((instance) =>
  121. {
  122. Analytics.RendererAssetData modifiedData = new Analytics.RendererAssetData();
  123. modifiedData.instance_id = instance.GetInstanceID();
  124. modifiedData.was_create_event = true;
  125. modifiedData.blending_layers_count = 1;
  126. modifiedData.blending_modes_used = 2;
  127. Analytics.Renderer2DAnalytics.instance.SendData(Analytics.AnalyticsDataTypes.k_Renderer2DDataString, modifiedData);
  128. });
  129. }
  130. }
  131. }