namespace Zinnia.Utility { using System; using System.Text.RegularExpressions; using UnityEngine; #if UNITY_2019_1_OR_NEWER using UnityEngine.Rendering; #endif [ExecuteInEditMode] /// /// Applies the relevant collection to the specified based on the render pipeline being used by the project. /// /// /// This is automatically run in the Unity editor when the script is awoken in the editor window. It does not run automatically during play mode. /// public class PipelineMaterialApplier : MonoBehaviour { [Serializable] public class PipelineMaterials { [Tooltip("The name of the pipeline to match the Material collection to.")] [SerializeField] private string pipelineName = ""; /// /// The name of the pipeline to match the collection to. /// public string PipelineName { get { return pipelineName; } set { pipelineName = value; } } [Tooltip("The name of the pipeline shader to match the Material collection to.")] [SerializeField] private string pipelineShaderName = ""; /// /// The name of the pipeline shader to match the collection to. /// public string PipelineShaderName { get { return pipelineShaderName; } set { pipelineShaderName = value; } } [Tooltip("The Material collection to set the Renderer materials to if using this render pipeline.")] [SerializeField] private Material[] materials = new Material[0]; /// /// The collection to set the materials to if using this render pipeline. /// public Material[] Materials { get { return materials; } set { materials = value; } } } [Tooltip("The Renderer to update the materials on.")] [SerializeField] private Renderer target; /// /// The to update the materials on. /// public Renderer Target { get { return target; } set { target = value; } } [Tooltip("The collection of PipelineMaterials to apply if the current element Pipeline Name matches the currently used render pipeline.")] [SerializeField] private PipelineMaterials[] pipelines = new PipelineMaterials[0]; /// /// The collection of to apply if the current element Pipeline Name matches the currently used render pipeline. /// public PipelineMaterials[] Pipelines { get { return pipelines; } set { pipelines = value; } } /// /// Applies the relevant pipeline collection to the . /// public virtual void ApplyMaterialsToRenderer() { if (Target == null) { return; } #if UNITY_2019_1_OR_NEWER && !ZINNIA_IGNORE_PIPELINE_MATERIALS foreach (PipelineMaterials pipeline in Pipelines) { string nameMatch = "default"; string shaderMatch = "default"; if (GraphicsSettings.currentRenderPipeline != null) { nameMatch = GraphicsSettings.currentRenderPipeline.name; shaderMatch = GraphicsSettings.currentRenderPipeline.defaultShader.ToString(); } bool nameCheck = !string.IsNullOrEmpty(pipeline.PipelineName) && Regex.IsMatch(nameMatch, pipeline.PipelineName); bool shaderCheck = !string.IsNullOrEmpty(pipeline.PipelineShaderName) && Regex.IsMatch(shaderMatch, pipeline.PipelineShaderName); if (nameCheck || shaderCheck) { Target.sharedMaterials = pipeline.Materials; break; } } #endif } protected virtual void Awake() { #if UNITY_EDITOR ApplyMaterialsToRenderer(); #endif } } }