RenderingPipelineDefines.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using UnityEditor;
  4. using UnityEngine;
  5. using UnityEngine.Rendering;
  6. [InitializeOnLoad]
  7. public class RenderingPipelineDefines
  8. {
  9. enum PipelineType
  10. {
  11. Unsupported,
  12. BuiltInPipeline,
  13. UniversalPipeline,
  14. HDPipeline
  15. }
  16. static RenderingPipelineDefines()
  17. {
  18. UpdateDefines();
  19. }
  20. /// <summary>
  21. /// Update the unity pipeline defines for URP
  22. /// </summary>
  23. static void UpdateDefines()
  24. {
  25. var pipeline = GetPipeline();
  26. if (pipeline == PipelineType.UniversalPipeline)
  27. {
  28. AddDefine("ZED_URP");
  29. }
  30. else
  31. {
  32. RemoveDefine("ZED_URP");
  33. }
  34. if (pipeline == PipelineType.HDPipeline)
  35. {
  36. AddDefine("ZED_HDRP");
  37. }
  38. else
  39. {
  40. RemoveDefine("ZED_HDRP");
  41. }
  42. }
  43. /// <summary>
  44. /// Returns the type of renderpipeline that is currently running
  45. /// </summary>
  46. /// <returns></returns>
  47. static PipelineType GetPipeline()
  48. {
  49. #if UNITY_2019_1_OR_NEWER
  50. if (GraphicsSettings.renderPipelineAsset != null)
  51. {
  52. // SRP
  53. var srpType = GraphicsSettings.renderPipelineAsset.GetType().ToString();
  54. if (srpType.Contains("HDRenderPipelineAsset"))
  55. {
  56. return PipelineType.HDPipeline;
  57. }
  58. else if (srpType.Contains("UniversalRenderPipelineAsset"))
  59. {
  60. return PipelineType.UniversalPipeline;
  61. }
  62. else return PipelineType.Unsupported;
  63. }
  64. #elif UNITY_2017_1_OR_NEWER
  65. if (GraphicsSettings.renderPipelineAsset != null) {
  66. // SRP not supported before 2019
  67. return PipelineType.Unsupported;
  68. }
  69. #endif
  70. // no SRP
  71. return PipelineType.BuiltInPipeline;
  72. }
  73. /// <summary>
  74. /// Add a custom define
  75. /// </summary>
  76. /// <param name="define"></param>
  77. /// <param name="buildTargetGroup"></param>
  78. public static void AddDefine(string define)
  79. {
  80. var definesList = GetDefines();
  81. if (!definesList.Contains(define))
  82. {
  83. definesList.Add(define);
  84. SetDefines(definesList);
  85. }
  86. }
  87. /// <summary>
  88. /// Remove a custom define
  89. /// </summary>
  90. /// <param name="_define"></param>
  91. /// <param name="_buildTargetGroup"></param>
  92. public static void RemoveDefine(string define)
  93. {
  94. var definesList = GetDefines();
  95. if (definesList.Contains(define))
  96. {
  97. definesList.Remove(define);
  98. SetDefines(definesList);
  99. }
  100. }
  101. public static List<string> GetDefines()
  102. {
  103. var target = EditorUserBuildSettings.activeBuildTarget;
  104. var buildTargetGroup = BuildPipeline.GetBuildTargetGroup(target);
  105. var defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(buildTargetGroup);
  106. return defines.Split(';').ToList();
  107. }
  108. public static void SetDefines(List<string> definesList)
  109. {
  110. var target = EditorUserBuildSettings.activeBuildTarget;
  111. var buildTargetGroup = BuildPipeline.GetBuildTargetGroup(target);
  112. var defines = string.Join(";", definesList.ToArray());
  113. PlayerSettings.SetScriptingDefineSymbolsForGroup(buildTargetGroup, defines);
  114. }
  115. }