using JetBrains.Annotations;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using UnityEditor.Build;
using UnityEngine;
using UnityEngine.Rendering;
namespace UnityEditor.Rendering
{
///
/// Extensions for
///
public static class BuildTargetExtensions
{
static bool NeedsToBeIncludedInBuildBylabel(RenderPipelineAsset asset, string label)
{
var labelList = AssetDatabase.GetLabels(asset);
foreach (string item in labelList)
{
if (item == label)
return true;
}
return false;
}
static void AddAdditionalRenderPipelineAssetsIncludedForBuild(HashSet assetsList)
where T : RenderPipelineAsset
{
var includer = GraphicsSettings.GetRenderPipelineSettings();
if (includer == null)
return;
bool includeSceneDependencies = includer.includeReferencedInScenes;
bool includeAssetsWithLabel = includer.includeAssetsByLabel;
string labelToInclude = includer.labelToInclude;
if (!includeSceneDependencies && !includeAssetsWithLabel)
return;
using (ListPool.Get(out var assetsPaths))
{
assetsPaths.AddRange(AssetDatabaseHelper.FindAssetPaths(".asset"));
if (includeSceneDependencies)
{
using (ListPool.Get(out var scenesPaths))
{
foreach (var scene in EditorBuildSettings.scenes)
if (scene.enabled)
scenesPaths.Add(scene.path);
// Get all enabled scenes path in the build settings.
HashSet depsHash = new HashSet(AssetDatabase.GetDependencies(scenesPaths.ToArray()));
for (int i = 0; i < assetsPaths.Count; ++i)
{
var assetPath = assetsPaths[i];
if (depsHash.Contains(assetPath))
assetsList.Add(AssetDatabase.LoadAssetAtPath(assetPath));
}
}
}
if (includeAssetsWithLabel)
{
for (int i = 0; i < assetsPaths.Count; ++i)
{
// Add the assets that are labeled to be included
var asset = AssetDatabase.LoadAssetAtPath(assetsPaths[i]);
if (NeedsToBeIncludedInBuildBylabel(asset, labelToInclude))
assetsList.Add(asset);
}
}
}
}
///
/// Obtains a list of the that are references into the settings either on or in
///
/// The type of
/// The to obtain the assets.
/// The output list of that are referenced by the platform.
/// false if there was an error fetching the for this
[MustUseReturnValue]
public static bool TryGetRenderPipelineAssets([DisallowNull] this BuildTarget buildTarget, List srpAssets)
where T : RenderPipelineAsset
{
if (srpAssets == null)
return false;
var activeBuildTargetGroup = BuildPipeline.GetBuildTargetGroup(buildTarget);
var namedBuildTarget = NamedBuildTarget.FromBuildTargetGroup(activeBuildTargetGroup);
QualitySettings.GetRenderPipelineAssetsForPlatform(namedBuildTarget.TargetName, out var buildPipelineAssets, out var allQualityLevelsAreOverriden);
bool noQualityLevels = QualitySettings.GetActiveQualityLevelsForPlatformCount(namedBuildTarget.TargetName) == 0;
if (noQualityLevels || !allQualityLevelsAreOverriden)
{
// We need to check the fallback cases
if (GraphicsSettings.defaultRenderPipeline is T srpAsset)
buildPipelineAssets.Add(srpAsset);
}
if (buildPipelineAssets.Count != 0)
AddAdditionalRenderPipelineAssetsIncludedForBuild(buildPipelineAssets);
srpAssets.AddRange(buildPipelineAssets);
return srpAssets.Count != 0;
}
}
}