MaterialFactory.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System;
  2. using System.Collections.Generic;
  3. namespace UnityEngine.PostProcessing
  4. {
  5. public sealed class MaterialFactory : IDisposable
  6. {
  7. public MaterialFactory()
  8. {
  9. this.m_Materials = new Dictionary<string, Material>();
  10. }
  11. public Material Get(string shaderName)
  12. {
  13. Material material;
  14. if (!this.m_Materials.TryGetValue(shaderName, out material))
  15. {
  16. Shader shader = Shader.Find(shaderName);
  17. if (shader == null)
  18. {
  19. throw new ArgumentException(string.Format("Shader not found ({0})", shaderName));
  20. }
  21. material = new Material(shader)
  22. {
  23. name = string.Format("PostFX - {0}", shaderName.Substring(shaderName.LastIndexOf("/") + 1)),
  24. hideFlags = HideFlags.DontSave
  25. };
  26. this.m_Materials.Add(shaderName, material);
  27. }
  28. return material;
  29. }
  30. public void Dispose()
  31. {
  32. foreach (KeyValuePair<string, Material> keyValuePair in this.m_Materials)
  33. {
  34. Material value = keyValuePair.Value;
  35. GraphicsUtils.Destroy(value);
  36. }
  37. this.m_Materials.Clear();
  38. }
  39. private Dictionary<string, Material> m_Materials;
  40. }
  41. }