Renderer2DMenus.cs 6.7 KB

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