using System; using System.Collections.Generic; using System.Linq; using UnityEditor.Build; using UnityEngine; using Object = UnityEngine.Object; namespace UnityEditor.XR.ARSubsystems { /// /// Class with helper methods for interacting with the build process. /// public static class BuildHelper { /// /// Adds a background shader with the given name to the project as a preloaded asset. /// /// The name of a shader to add to the project. /// Thrown if a shader with the given name cannot be /// found. public static void AddBackgroundShaderToProject(string shaderName) { if (string.IsNullOrEmpty(shaderName)) { Debug.LogWarning("Incompatible render pipeline in GraphicsSettings.currentRenderPipeline. Background " + "rendering may not operate properly."); } else { Shader shader = FindShaderOrFailBuild(shaderName); Object[] preloadedAssets = PlayerSettings.GetPreloadedAssets(); var shaderAssets = (from preloadedAsset in preloadedAssets where shader.Equals(preloadedAsset) select preloadedAsset); if ((shaderAssets == null) || !shaderAssets.Any()) { List preloadedAssetsList = preloadedAssets.ToList(); preloadedAssetsList.Add(shader); PlayerSettings.SetPreloadedAssets(preloadedAssetsList.ToArray()); } } } /// /// Removes a shader with the given name from the preloaded assets of the project. /// /// The name of a shader to remove from the project. /// Thrown if a shader with the given name cannot be /// found. public static void RemoveShaderFromProject(string shaderName) { if (!string.IsNullOrEmpty(shaderName)) { Shader shader = FindShaderOrFailBuild(shaderName); Object[] preloadedAssets = PlayerSettings.GetPreloadedAssets(); var nonShaderAssets = (from preloadedAsset in preloadedAssets where !shader.Equals(preloadedAsset) select preloadedAsset); PlayerSettings.SetPreloadedAssets(nonShaderAssets.ToArray()); } } /// /// Finds a shader with the given name. If no shader with that name is found, the build fails. /// /// The name of a shader to find. /// /// The shader with the given name. /// /// Thrown if a shader with the given name cannot be /// found. static Shader FindShaderOrFailBuild(string shaderName) { var shader = Shader.Find(shaderName); if (shader == null) { throw new BuildFailedException($"Cannot find shader '{shaderName}'"); } return shader; } } }