using System;
using System.Collections.Generic;
namespace UnityEngine.Rendering.PostProcessing
{
///
/// A factory for easy creation and destruction of
/// and .
///
///
public sealed class PropertySheetFactory
{
readonly Dictionary m_Sheets;
///
/// Creates a new factory.
///
public PropertySheetFactory()
{
m_Sheets = new Dictionary();
}
///
/// Gets a for a given shader identifier. Sheets are recycled
/// so you can safely call this method on every frame.
///
/// The name of the shader to retrieve a sheet for
/// A sheet for the given shader
///
/// This method will not work when loading post-processing from an asset bundle. For this
/// reason it is recommended to use instead.
///
/// Thrown if the shader is invalid
[Obsolete("Use PropertySheet.Get(Shader) with a direct reference to the Shader instead.")]
public PropertySheet Get(string shaderName)
{
var shader = Shader.Find(shaderName);
if (shader == null)
throw new ArgumentException(string.Format("Invalid shader ({0})", shaderName));
return Get(shader);
}
///
/// Gets a for a given shader instance. Sheets are recycled so
/// you can safely call this method on every frame.
///
/// A shader instance to retrieve a sheet for
/// A sheet for the given shader
/// Thrown if the shader is invalid
public PropertySheet Get(Shader shader)
{
PropertySheet sheet;
if (shader == null)
throw new ArgumentException(string.Format("Invalid shader ({0})", shader));
if (m_Sheets.TryGetValue(shader, out sheet))
return sheet;
var shaderName = shader.name;
var material = new Material(shader)
{
name = string.Format("PostProcess - {0}", shaderName.Substring(shaderName.LastIndexOf('/') + 1)),
hideFlags = HideFlags.DontSave
};
sheet = new PropertySheet(material);
m_Sheets.Add(shader, sheet);
return sheet;
}
///
/// Releases all resources used by this factory.
///
///
/// You don't need to call this method when using the builtin factory from
/// .
///
public void Release()
{
foreach (var sheet in m_Sheets.Values)
sheet.Release();
m_Sheets.Clear();
}
}
}