You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.4 KiB
52 lines
1.4 KiB
using System; |
|
using System.Collections.Generic; |
|
|
|
namespace UnityEngine.PostProcessing |
|
{ |
|
using UnityObject = Object; |
|
|
|
public sealed class MaterialFactory : IDisposable |
|
{ |
|
Dictionary<string, Material> m_Materials; |
|
|
|
public MaterialFactory() |
|
{ |
|
m_Materials = new Dictionary<string, Material>(); |
|
} |
|
|
|
public Material Get(string shaderName) |
|
{ |
|
Material material; |
|
|
|
if (!m_Materials.TryGetValue(shaderName, out material)) |
|
{ |
|
var shader = Shader.Find(shaderName); |
|
|
|
if (shader == null) |
|
throw new ArgumentException(string.Format("Shader not found ({0})", shaderName)); |
|
|
|
material = new Material(shader) |
|
{ |
|
name = string.Format("PostFX - {0}", shaderName.Substring(shaderName.LastIndexOf("/") + 1)), |
|
hideFlags = HideFlags.DontSave |
|
}; |
|
|
|
m_Materials.Add(shaderName, material); |
|
} |
|
|
|
return material; |
|
} |
|
|
|
public void Dispose() |
|
{ |
|
var enumerator = m_Materials.GetEnumerator(); |
|
while (enumerator.MoveNext()) |
|
{ |
|
var material = enumerator.Current.Value; |
|
GraphicsUtils.Destroy(material); |
|
} |
|
|
|
m_Materials.Clear(); |
|
} |
|
} |
|
}
|
|
|