chenxin
2020-11-19 1213446becf0fd501829ed8e6532b64cf904b2f6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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();
        }
    }
}