using System; using UnityEngine; using UnityEngine.Profiling; namespace Coffee.UIParticleInternal { /// /// Provides functionality to manage materials. /// internal static class MaterialRepository { private static readonly ObjectRepository s_Repository = new ObjectRepository(); public static int count => s_Repository.count; #if UNITY_EDITOR [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)] private static void Clear() { s_Repository.Clear(); } #endif /// /// Retrieves a cached material based on the hash. /// public static bool Valid(Hash128 hash, Material material) { Profiler.BeginSample("(COF)[MaterialRegistry] Valid"); var ret = s_Repository.Valid(hash, material); Profiler.EndSample(); return ret; } /// /// Adds or retrieves a cached material based on the hash. /// public static void Get(Hash128 hash, ref Material material, Func onCreate) { Profiler.BeginSample("(COF)[MaterialRepository] Get"); s_Repository.Get(hash, ref material, onCreate); Profiler.EndSample(); } /// /// Adds or retrieves a cached material based on the hash. /// public static void Get(Hash128 hash, ref Material material, string shaderName) { Profiler.BeginSample("(COF)[MaterialRepository] Get"); s_Repository.Get(hash, ref material, x => new Material(Shader.Find(x)) { hideFlags = HideFlags.DontSave | HideFlags.NotEditable }, shaderName); Profiler.EndSample(); } /// /// Adds or retrieves a cached material based on the hash. /// public static void Get(Hash128 hash, ref Material material, string shaderName, string[] keywords) { Profiler.BeginSample("(COF)[MaterialRepository] Get"); s_Repository.Get(hash, ref material, x => new Material(Shader.Find(x.shaderName)) { hideFlags = HideFlags.DontSave | HideFlags.NotEditable, shaderKeywords = x.keywords }, (shaderName, keywords)); Profiler.EndSample(); } /// /// Adds or retrieves a cached material based on the hash. /// public static void Get(Hash128 hash, ref Material material, Func onCreate, T source) { Profiler.BeginSample("(COF)[MaterialRepository] Get"); s_Repository.Get(hash, ref material, onCreate, source); Profiler.EndSample(); } /// /// Removes a soft mask material from the cache. /// public static void Release(ref Material material) { Profiler.BeginSample("(COF)[MaterialRepository] Release"); s_Repository.Release(ref material); Profiler.EndSample(); } } }