using UnityEditor;
using UnityEngine;
namespace TriLibCore.Editor
{
///
/// Represents a class with Shader Variant Collection utility methods.
///
public static class ShaderVariantCollectionUtils
{
///
/// Adds the given Shader Variant Collection to the Graphic Settings preloaded shaders.
///
/// The Shader Variant Collection to add.
public static void AddShaderVariantCollectionToGraphicSettings(ShaderVariantCollection shaderVariantCollection)
{
var graphicSettingAssets = AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/GraphicsSettings.asset");
if (graphicSettingAssets != null && graphicSettingAssets.Length > 0)
{
var graphicsSettings = new SerializedObject(graphicSettingAssets[0]);
var preloadedShaders = graphicsSettings.FindProperty("m_PreloadedShaders");
preloadedShaders.InsertArrayElementAtIndex(preloadedShaders.arraySize);
preloadedShaders.GetArrayElementAtIndex(preloadedShaders.arraySize - 1).objectReferenceValue = shaderVariantCollection;
graphicsSettings.ApplyModifiedProperties();
}
}
///
/// Returns whether the given Shader Variant Collection exists on the Graphic Settings preloaded shaders.
///
/// The Shader Variant Collection to check for.
/// Whether the Shader Variant Collection exists on the Graphic Settings preloaded shaders.
public static bool IsShaderVariantCollectionPreloaded(ShaderVariantCollection shaderVariantCollection)
{
var graphicSettingAssets = AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/GraphicsSettings.asset");
if (graphicSettingAssets != null && graphicSettingAssets.Length > 0)
{
var graphicsSettings = new SerializedObject(graphicSettingAssets[0]);
var preloadedShaders = graphicsSettings.FindProperty("m_PreloadedShaders");
for (var i = 0; i < preloadedShaders.arraySize; i++)
{
if (preloadedShaders.GetArrayElementAtIndex(i).objectReferenceValue == shaderVariantCollection)
{
return true;
}
}
return false;
}
return true;
}
}
}