BuildHelper.cs
3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor.Build;
using UnityEngine;
using Object = UnityEngine.Object;
namespace UnityEditor.XR.ARSubsystems
{
/// <summary>
/// Class with helper methods for interacting with the build process.
/// </summary>
public static class BuildHelper
{
/// <summary>
/// Adds a background shader with the given name to the project as a preloaded asset.
/// </summary>
/// <param name="shaderName">The name of a shader to add to the project.</param>
/// <exception cref="UnityEditor.Build.BuildFailedException">Thrown if a shader with the given name cannot be
/// found.</exception>
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<Object> preloadedAssetsList = preloadedAssets.ToList();
preloadedAssetsList.Add(shader);
PlayerSettings.SetPreloadedAssets(preloadedAssetsList.ToArray());
}
}
}
/// <summary>
/// Removes a shader with the given name from the preloaded assets of the project.
/// </summary>
/// <param name="shaderName">The name of a shader to remove from the project.</param>
/// <exception cref="UnityEditor.Build.BuildFailedException">Thrown if a shader with the given name cannot be
/// found.</exception>
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());
}
}
/// <summary>
/// Finds a shader with the given name, or fail the build, if no shader is found.
/// </summary>
/// <param name="shaderName">The name of a shader to find.</param>
/// <returns>
/// The shader with the given name.
/// </returns>
/// <exception cref="UnityEditor.Build.BuildFailedException">Thrown if a shader with the given name cannot be
/// found.</exception>
static Shader FindShaderOrFailBuild(string shaderName)
{
var shader = Shader.Find(shaderName);
if (shader == null)
{
throw new BuildFailedException($"Cannot find shader '{shaderName}'");
}
return shader;
}
}
}