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.

183 lines
8.1 KiB

5 years ago
  1. #if UNITY_EDITOR
  2. using UnityEngine;
  3. using UnityEditor;
  4. using System.Reflection;
  5. public class FogVolumeCreator : Editor
  6. {
  7. [MenuItem("GameObject/Create Other/Fog Volume")]
  8. [MenuItem("Fog Volume/Create Fog Volume")]
  9. public static void CreateFogVolume()
  10. {
  11. var FogVolume = new GameObject();
  12. //Icon stuff
  13. var Icon = Resources.Load("FogVolumeIcon") as Texture;
  14. //Icon.hideFlags = HideFlags.HideAndDontSave;
  15. var editorGUI = typeof(EditorGUIUtility);
  16. var bindingFlags = BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.NonPublic;
  17. var args = new object[]
  18. {
  19. FogVolume,
  20. Icon
  21. };
  22. editorGUI.InvokeMember("SetIconForObject", bindingFlags, null, null, args);
  23. FogVolume.name = "Fog Volume";
  24. FogVolume.AddComponent<MeshRenderer>();
  25. FogVolume.AddComponent<FogVolume>();
  26. FogVolume.GetComponent<Renderer>().shadowCastingMode =
  27. UnityEngine.Rendering.ShadowCastingMode.Off;
  28. FogVolume.GetComponent<Renderer>().receiveShadows = false;
  29. FogVolume.GetComponent<Renderer>().reflectionProbeUsage =
  30. UnityEngine.Rendering.ReflectionProbeUsage.Off;
  31. FogVolume.GetComponent<Renderer>().lightProbeUsage =
  32. UnityEngine.Rendering.LightProbeUsage.Off;
  33. Selection.activeObject = FogVolume;
  34. if (SceneView.currentDrawingSceneView)
  35. {
  36. SceneView.currentDrawingSceneView.MoveToView(FogVolume.transform);
  37. }
  38. }
  39. //=============================================================================================
  40. // L I G H T S
  41. //=============================================================================================
  42. [MenuItem("GameObject/Create Other/Fog Volume Light/Fog Volume Point Light")]
  43. [MenuItem("Fog Volume/Create Light/Fog Volume Point Light")]
  44. public static void CreateFogVolumePointLight()
  45. {
  46. var fogVolumeLight = new GameObject("FogVolumePointLight");
  47. var light = fogVolumeLight.AddComponent<FogVolumeLight>();
  48. light.IsPointLight = true;
  49. light.IsAddedToNormalLight = false;
  50. }
  51. [MenuItem("GameObject/Create Other/Fog Volume Light/Fog Volume Spot Light")]
  52. [MenuItem("Fog Volume/Create Light/Fog Volume Spot Light")]
  53. public static void CreateFogVolumeSpotLight()
  54. {
  55. var fogVolumeLight = new GameObject("FogVolumeSpotLight");
  56. var light = fogVolumeLight.AddComponent<FogVolumeLight>();
  57. light.IsPointLight = false;
  58. light.IsAddedToNormalLight = false;
  59. }
  60. [MenuItem("Fog Volume/Create Light/Fog Volume Directional Light")]
  61. public static void AutoCreateFogVolumeDirectionalLight()
  62. {
  63. var lights = FindObjectsOfType<Light>();
  64. for (var i = 0; i < lights.Length; i++)
  65. {
  66. var light = lights[i];
  67. if (light.type == LightType.Directional)
  68. {
  69. if (light.GetComponent<FogVolumeDirectionalLight>() == null)
  70. {
  71. var dirLight = light.gameObject.AddComponent<FogVolumeDirectionalLight>();
  72. var fogVolumes = FindObjectsOfType<FogVolume>();
  73. dirLight._TargetFogVolumes = new FogVolume[fogVolumes.Length];
  74. for (var k = 0; k < fogVolumes.Length; k++)
  75. {
  76. dirLight._TargetFogVolumes[k] = fogVolumes[k];
  77. }
  78. }
  79. }
  80. }
  81. }
  82. [MenuItem("Fog Volume/Create Light/Fog Volume Directional Light", true)]
  83. public static bool EnableCreateFogVolumeDirectionalLight()
  84. {
  85. return FindObjectOfType<FogVolumeDirectionalLight>() == null &&
  86. FindObjectOfType<FogVolume>() != null;
  87. }
  88. [MenuItem("GameObject/Create Other/Fog Volume Light/Normal Point Light")]
  89. [MenuItem("Fog Volume/Create Light/Normal Point Light")]
  90. public static void CreateNormalPointLight()
  91. {
  92. var normalPointLight = new GameObject("Point light");
  93. var light = normalPointLight.AddComponent<Light>();
  94. light.type = LightType.Point;
  95. var fvLight = normalPointLight.AddComponent<FogVolumeLight>();
  96. fvLight.IsPointLight = true;
  97. fvLight.IsAddedToNormalLight = true;
  98. }
  99. [MenuItem("GameObject/Create Other/Fog Volume Light/Normal Spot Light")]
  100. [MenuItem("Fog Volume/Create Light/Normal Spot Light")]
  101. public static void CreateNormalSpotLight()
  102. {
  103. var normalPointLight = new GameObject("Spot light");
  104. var light = normalPointLight.AddComponent<Light>();
  105. light.type = LightType.Spot;
  106. var fvLight = normalPointLight.AddComponent<FogVolumeLight>();
  107. fvLight.IsPointLight = false;
  108. fvLight.IsAddedToNormalLight = true;
  109. }
  110. //=============================================================================================
  111. // P R I M I T I V E S
  112. //=============================================================================================
  113. [MenuItem("Fog Volume/Create Primitive/Fog Volume Primitive Box")]
  114. [MenuItem("GameObject/Create Other/Fog Volume Primitive/Box")]
  115. static public void CreateFogVolumePrimitiveBox()
  116. {
  117. GameObject FogVolumePrimitive = GameObject.CreatePrimitive(PrimitiveType.Cube);
  118. //Icon stuff
  119. //Texture Icon = Resources.Load("FogVolumePrimitiveIcon") as Texture;
  120. //Icon.hideFlags = HideFlags.HideAndDontSave;
  121. // var editorGUI = typeof(EditorGUIUtility);
  122. // var bindingFlags = BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.NonPublic;
  123. //var args = new object[] { FogVolumePrimitive, Icon };
  124. //editorGUI.InvokeMember("SetIconForObject", bindingFlags, null, null, args);
  125. FogVolumePrimitive.name = "Fog Volume Primitive Box";
  126. FogVolumePrimitive.GetComponent<BoxCollider>().isTrigger = true;
  127. FogVolumePrimitive.GetComponent<Renderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
  128. FogVolumePrimitive.GetComponent<Renderer>().receiveShadows = false;
  129. FogVolumePrimitive.GetComponent<Renderer>().reflectionProbeUsage = UnityEngine.Rendering.ReflectionProbeUsage.Off;
  130. FogVolumePrimitive.GetComponent<Renderer>().lightProbeUsage = UnityEngine.Rendering.LightProbeUsage.Off;
  131. Selection.activeObject = FogVolumePrimitive;
  132. if (UnityEditor.SceneView.currentDrawingSceneView) UnityEditor.SceneView.currentDrawingSceneView.MoveToView(FogVolumePrimitive.transform);
  133. FogVolumePrimitive.AddComponent<FogVolumePrimitive>();
  134. }
  135. [MenuItem("Fog Volume/Create Primitive/Fog Volume Primitive Sphere")]
  136. [MenuItem("GameObject/Create Other/Fog Volume Primitive/Sphere")]
  137. static public void CreateFogVolumePrimitiveSphere()
  138. {
  139. GameObject FogVolumePrimitive = GameObject.CreatePrimitive(PrimitiveType.Sphere);
  140. //Icon stuff
  141. //Texture Icon = Resources.Load("FogVolumePrimitiveIcon") as Texture;
  142. //Icon.hideFlags = HideFlags.HideAndDontSave;
  143. // var editorGUI = typeof(EditorGUIUtility);
  144. // var bindingFlags = BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.NonPublic;
  145. //var args = new object[] { FogVolumePrimitive, Icon };
  146. //editorGUI.InvokeMember("SetIconForObject", bindingFlags, null, null, args);
  147. FogVolumePrimitive.name = "Fog Volume Primitive Sphere";
  148. FogVolumePrimitive.GetComponent<SphereCollider>().isTrigger = true;
  149. FogVolumePrimitive.GetComponent<Renderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
  150. FogVolumePrimitive.GetComponent<Renderer>().receiveShadows = false;
  151. FogVolumePrimitive.GetComponent<Renderer>().reflectionProbeUsage = UnityEngine.Rendering.ReflectionProbeUsage.Off;
  152. FogVolumePrimitive.GetComponent<Renderer>().lightProbeUsage = UnityEngine.Rendering.LightProbeUsage.Off;
  153. Selection.activeObject = FogVolumePrimitive;
  154. if (UnityEditor.SceneView.currentDrawingSceneView) UnityEditor.SceneView.currentDrawingSceneView.MoveToView(FogVolumePrimitive.transform);
  155. FogVolumePrimitive.AddComponent<FogVolumePrimitive>();
  156. }
  157. }
  158. #endif