A game about forced loneliness, made by TACStudios
at master 111 lines 4.9 kB view raw
1using JetBrains.Annotations; 2using System; 3using System.Collections.Generic; 4using System.Diagnostics.CodeAnalysis; 5using UnityEditor.Build; 6using UnityEngine; 7using UnityEngine.Rendering; 8 9namespace UnityEditor.Rendering 10{ 11 /// <summary> 12 /// Extensions for <see cref="BuildTarget"/> 13 /// </summary> 14 public static class BuildTargetExtensions 15 { 16 static bool NeedsToBeIncludedInBuildBylabel(RenderPipelineAsset asset, string label) 17 { 18 var labelList = AssetDatabase.GetLabels(asset); 19 foreach (string item in labelList) 20 { 21 if (item == label) 22 return true; 23 } 24 return false; 25 } 26 27 static void AddAdditionalRenderPipelineAssetsIncludedForBuild<T>(HashSet<T> assetsList) 28 where T : RenderPipelineAsset 29 { 30 var includer = GraphicsSettings.GetRenderPipelineSettings<IncludeAdditionalRPAssets>(); 31 if (includer == null) 32 return; 33 34 bool includeSceneDependencies = includer.includeReferencedInScenes; 35 bool includeAssetsWithLabel = includer.includeAssetsByLabel; 36 string labelToInclude = includer.labelToInclude; 37 38 if (!includeSceneDependencies && !includeAssetsWithLabel) 39 return; 40 41 using (ListPool<string>.Get(out var assetsPaths)) 42 { 43 assetsPaths.AddRange(AssetDatabaseHelper.FindAssetPaths<T>(".asset")); 44 45 if (includeSceneDependencies) 46 { 47 using (ListPool<string>.Get(out var scenesPaths)) 48 { 49 foreach (var scene in EditorBuildSettings.scenes) 50 if (scene.enabled) 51 scenesPaths.Add(scene.path); 52 53 // Get all enabled scenes path in the build settings. 54 HashSet<string> depsHash = new HashSet<string>(AssetDatabase.GetDependencies(scenesPaths.ToArray())); 55 for (int i = 0; i < assetsPaths.Count; ++i) 56 { 57 var assetPath = assetsPaths[i]; 58 if (depsHash.Contains(assetPath)) 59 assetsList.Add(AssetDatabase.LoadAssetAtPath<T>(assetPath)); 60 } 61 } 62 } 63 64 if (includeAssetsWithLabel) 65 { 66 for (int i = 0; i < assetsPaths.Count; ++i) 67 { 68 // Add the assets that are labeled to be included 69 var asset = AssetDatabase.LoadAssetAtPath<T>(assetsPaths[i]); 70 if (NeedsToBeIncludedInBuildBylabel(asset, labelToInclude)) 71 assetsList.Add(asset); 72 } 73 } 74 } 75 } 76 77 /// <summary> 78 /// Obtains a list of the <see cref="RenderPipelineAsset"/> that are references into the settings either on <see cref="QualitySettings"/> or in <see cref="GraphicsSettings"/> 79 /// </summary> 80 /// <typeparam name="T">The type of <see cref="RenderPipelineAsset"/></typeparam> 81 /// <param name="buildTarget">The <see cref="BuildTarget"/> to obtain the assets.</param> 82 /// <param name="srpAssets">The output list of <see cref="RenderPipelineAsset"/> that are referenced by the platform.</param> 83 /// <returns>false if there was an error fetching the <see cref="RenderPipelineAsset"/> for this <see cref="BuildTarget"/></returns> 84 [MustUseReturnValue] 85 public static bool TryGetRenderPipelineAssets<T>([DisallowNull] this BuildTarget buildTarget, List<T> srpAssets) 86 where T : RenderPipelineAsset 87 { 88 if (srpAssets == null) 89 return false; 90 91 var activeBuildTargetGroup = BuildPipeline.GetBuildTargetGroup(buildTarget); 92 var namedBuildTarget = NamedBuildTarget.FromBuildTargetGroup(activeBuildTargetGroup); 93 94 QualitySettings.GetRenderPipelineAssetsForPlatform<T>(namedBuildTarget.TargetName, out var buildPipelineAssets, out var allQualityLevelsAreOverriden); 95 96 bool noQualityLevels = QualitySettings.GetActiveQualityLevelsForPlatformCount(namedBuildTarget.TargetName) == 0; 97 if (noQualityLevels || !allQualityLevelsAreOverriden) 98 { 99 // We need to check the fallback cases 100 if (GraphicsSettings.defaultRenderPipeline is T srpAsset) 101 buildPipelineAssets.Add(srpAsset); 102 } 103 104 if (buildPipelineAssets.Count != 0) 105 AddAdditionalRenderPipelineAssetsIncludedForBuild(buildPipelineAssets); 106 107 srpAssets.AddRange(buildPipelineAssets); 108 return srpAssets.Count != 0; 109 } 110 } 111}