SW 중심대학 OSS GIT 서버 박건태, 이승준, 고기완, 이준호 새로운 배포
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.

86 lines
3.4 KiB

4 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEditor.Build;
  5. using UnityEngine;
  6. using Object = UnityEngine.Object;
  7. namespace UnityEditor.XR.ARSubsystems
  8. {
  9. /// <summary>
  10. /// Class with helper methods for interacting with the build process.
  11. /// </summary>
  12. public static class BuildHelper
  13. {
  14. /// <summary>
  15. /// Adds a background shader with the given name to the project as a preloaded asset.
  16. /// </summary>
  17. /// <param name="shaderName">The name of a shader to add to the project.</param>
  18. /// <exception cref="UnityEditor.Build.BuildFailedException">Thrown if a shader with the given name cannot be
  19. /// found.</exception>
  20. public static void AddBackgroundShaderToProject(string shaderName)
  21. {
  22. if (string.IsNullOrEmpty(shaderName))
  23. {
  24. Debug.LogWarning("Incompatible render pipeline in GraphicsSettings.renderPipelineAsset. Background "
  25. + "rendering may not operate properly.");
  26. }
  27. else
  28. {
  29. Shader shader = FindShaderOrFailBuild(shaderName);
  30. Object[] preloadedAssets = PlayerSettings.GetPreloadedAssets();
  31. var shaderAssets = (from preloadedAsset in preloadedAssets where shader.Equals(preloadedAsset)
  32. select preloadedAsset);
  33. if ((shaderAssets == null) || !shaderAssets.Any())
  34. {
  35. List<Object> preloadedAssetsList = preloadedAssets.ToList();
  36. preloadedAssetsList.Add(shader);
  37. PlayerSettings.SetPreloadedAssets(preloadedAssetsList.ToArray());
  38. }
  39. }
  40. }
  41. /// <summary>
  42. /// Removes a shader with the given name from the preloaded assets of the project.
  43. /// </summary>
  44. /// <param name="shaderName">The name of a shader to remove from the project.</param>
  45. /// <exception cref="UnityEditor.Build.BuildFailedException">Thrown if a shader with the given name cannot be
  46. /// found.</exception>
  47. public static void RemoveShaderFromProject(string shaderName)
  48. {
  49. if (!string.IsNullOrEmpty(shaderName))
  50. {
  51. Shader shader = FindShaderOrFailBuild(shaderName);
  52. Object[] preloadedAssets = PlayerSettings.GetPreloadedAssets();
  53. var nonShaderAssets = (from preloadedAsset in preloadedAssets where !shader.Equals(preloadedAsset)
  54. select preloadedAsset);
  55. PlayerSettings.SetPreloadedAssets(nonShaderAssets.ToArray());
  56. }
  57. }
  58. /// <summary>
  59. /// Finds a shader with the given name, or fail the build, if no shader is found.
  60. /// </summary>
  61. /// <param name="shaderName">The name of a shader to find.</param>
  62. /// <returns>
  63. /// The shader with the given name.
  64. /// </returns>
  65. /// <exception cref="UnityEditor.Build.BuildFailedException">Thrown if a shader with the given name cannot be
  66. /// found.</exception>
  67. static Shader FindShaderOrFailBuild(string shaderName)
  68. {
  69. var shader = Shader.Find(shaderName);
  70. if (shader == null)
  71. {
  72. throw new BuildFailedException($"Cannot find shader '{shaderName}'");
  73. }
  74. return shader;
  75. }
  76. }
  77. }