Brak opisu
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.

RenderPipelineGlobalSettingsUtils.cs 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #if UNITY_EDITOR
  2. using System;
  3. using System.Linq;
  4. using System.Reflection;
  5. using UnityEditor;
  6. using UnityEditor.Rendering;
  7. namespace UnityEngine.Rendering
  8. {
  9. /// <summary>
  10. /// Set of utilities for <see cref="RenderPipelineGlobalSettings"/>
  11. /// </summary>
  12. public class RenderPipelineGlobalSettingsUtils
  13. {
  14. /// <summary>
  15. /// Creates a <see cref="RenderPipelineGlobalSettings"/> asset
  16. /// </summary>
  17. /// <param name="path">The path where Unity generates the asset.</param>
  18. /// <param name="dataSource">Another `RenderPipelineGlobalSettings` that Unity uses as a data source.</param>
  19. /// <typeparam name="TGlobalSetting"><see cref="RenderPipelineGlobalSettings"/> </typeparam>
  20. /// <returns>Returns the asset created.</returns>
  21. public static TGlobalSetting Create<TGlobalSetting>(string path, TGlobalSetting dataSource = null)
  22. where TGlobalSetting : RenderPipelineGlobalSettings
  23. {
  24. return Create(typeof(TGlobalSetting), path, dataSource) as TGlobalSetting;
  25. }
  26. /// <summary>
  27. /// Creates a <see cref="RenderPipelineGlobalSettings"/> asset
  28. /// </summary>
  29. /// <param name="renderPipelineGlobalSettingsType"></param>
  30. /// <param name="path">The path where Unity generates the asset.</param>
  31. /// <param name="dataSource">Another `RenderPipelineGlobalSettings` that Unity uses as a data source.</param>
  32. /// <returns>Returns the asset created.</returns>
  33. public static RenderPipelineGlobalSettings Create(Type renderPipelineGlobalSettingsType, string path, RenderPipelineGlobalSettings dataSource = null)
  34. {
  35. if (!typeof(RenderPipelineGlobalSettings).IsAssignableFrom(renderPipelineGlobalSettingsType))
  36. throw new ArgumentException(
  37. $"{nameof(renderPipelineGlobalSettingsType)} must be a valid {typeof(RenderPipelineGlobalSettings)}");
  38. // Sanitize the path
  39. if (string.IsNullOrEmpty(path))
  40. path = $"Assets/{renderPipelineGlobalSettingsType.Name}.asset";
  41. if (!path.StartsWith("assets/", StringComparison.CurrentCultureIgnoreCase))
  42. path = $"Assets/{path}";
  43. CoreUtils.EnsureFolderTreeInAssetFilePath(path);
  44. path = AssetDatabase.GenerateUniqueAssetPath(path);
  45. var assetCreated = ScriptableObject.CreateInstance(renderPipelineGlobalSettingsType) as RenderPipelineGlobalSettings;
  46. if (assetCreated != null)
  47. {
  48. AssetDatabase.CreateAsset(assetCreated, path);
  49. // copy data from provided source
  50. if (dataSource != null)
  51. EditorUtility.CopySerializedManagedFieldsOnly(dataSource, assetCreated);
  52. EditorGraphicsSettings.PopulateRenderPipelineGraphicsSettings(assetCreated);
  53. assetCreated.Initialize(dataSource);
  54. EditorUtility.SetDirty(assetCreated);
  55. AssetDatabase.SaveAssetIfDirty(assetCreated);
  56. AssetDatabase.Refresh();
  57. }
  58. return assetCreated;
  59. }
  60. /// <summary>
  61. /// Checks that a <see cref="RenderPipelineGlobalSettings"/> asset exists.
  62. /// If the asset isn't valid, Unity tries the following in order:
  63. /// 1. Loads the asset at the default path.
  64. /// 2. Finds any asset in the project with the same type.
  65. /// 3. If `canCreateNewAsset` is true, creates a new asset in the default path.
  66. /// If Unity finds or creates a valid asset, Unity updates the <see cref="GraphicsSettings"/> with it. Otherwise Unity will unregister the settings for the given pipeline.
  67. /// </summary>
  68. /// <param name="instance">The current instance of the asset.</param>
  69. /// <param name="defaultPath">The default path.</param>
  70. /// <param name="canCreateNewAsset">If set to `true`, Unity creates a new asset if it can't find an existing asset in the project.</param>
  71. /// <typeparam name="TGlobalSetting">The type of global settings asset to check.</typeparam>
  72. /// <typeparam name="TRenderPipeline">The type of `RenderPipeline` that this asset belongs to.</typeparam>
  73. /// <returns>The asset that Unity found or created, or `null` if Unity can't find or create a valid asset.</returns>
  74. public static bool TryEnsure<TGlobalSetting, TRenderPipeline>(ref TGlobalSetting instance, string defaultPath = "", bool canCreateNewAsset = true)
  75. where TGlobalSetting : RenderPipelineGlobalSettings<TGlobalSetting, TRenderPipeline>
  76. where TRenderPipeline : RenderPipeline
  77. {
  78. if (!TryEnsure<TGlobalSetting, TRenderPipeline>(ref instance, defaultPath, canCreateNewAsset, out var error))
  79. {
  80. Debug.LogError(error.Message);
  81. return false;
  82. }
  83. return true;
  84. }
  85. // This method is exposed to Unit Tests
  86. internal static bool TryEnsure<TGlobalSetting, TRenderPipeline>(ref TGlobalSetting instance, string defaultPath, bool canCreateNewAsset, out Exception error)
  87. where TGlobalSetting : RenderPipelineGlobalSettings<TGlobalSetting, TRenderPipeline>
  88. where TRenderPipeline : RenderPipeline
  89. {
  90. var globalSettingsName = typeof(TGlobalSetting).GetCustomAttribute<DisplayInfoAttribute>()?.name ?? typeof(TGlobalSetting).Name;
  91. if (instance == null || instance.Equals(null))
  92. {
  93. if (!string.IsNullOrEmpty(defaultPath))
  94. {
  95. // Look at default path, if the asset exist, is the one that we need
  96. instance = AssetDatabase.LoadAssetAtPath<TGlobalSetting>(defaultPath);
  97. }
  98. if (instance == null)
  99. {
  100. // There was not saved into the default path, fetch the asset database to see if there is one defined in the project
  101. instance = CoreUtils.LoadAllAssets<TGlobalSetting>().FirstOrDefault();
  102. if (instance == null)
  103. {
  104. // Try to create one if possible
  105. if (canCreateNewAsset)
  106. {
  107. instance = Create<TGlobalSetting>(defaultPath);
  108. if (instance != null)
  109. {
  110. Debug.LogWarning($"{globalSettingsName} has been created for you. If you want to modify it, go to Project Settings > Graphics > {globalSettingsName}");
  111. }
  112. }
  113. }
  114. }
  115. }
  116. error = instance == null || instance.Equals(null)
  117. ? new Exception(
  118. $"Unable to find or create a {globalSettingsName}. The configured Render Pipeline may not work correctly. Go to Project Settings > Graphics > {globalSettingsName} for additional help.")
  119. : null;
  120. EditorGraphicsSettings.SetRenderPipelineGlobalSettingsAsset<TRenderPipeline>(instance);
  121. return error == null;
  122. }
  123. }
  124. }
  125. #endif