2020-05-11 15:34:22 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace Coffee.UISoftMask
|
|
|
|
|
{
|
2020-06-04 12:43:17 +08:00
|
|
|
|
internal class MaterialEntry
|
2020-05-11 15:34:22 +08:00
|
|
|
|
{
|
2020-06-04 12:43:17 +08:00
|
|
|
|
public Material material;
|
|
|
|
|
public int referenceCount;
|
2020-05-11 15:34:22 +08:00
|
|
|
|
|
2020-06-04 12:43:17 +08:00
|
|
|
|
public void Release()
|
2020-05-11 15:34:22 +08:00
|
|
|
|
{
|
2020-06-04 12:43:17 +08:00
|
|
|
|
if (material)
|
2020-05-11 15:34:22 +08:00
|
|
|
|
{
|
2020-06-04 12:43:17 +08:00
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
if (!Application.isPlaying)
|
2020-05-11 15:34:22 +08:00
|
|
|
|
UnityEngine.Object.DestroyImmediate(material, false);
|
2020-06-04 12:43:17 +08:00
|
|
|
|
else
|
|
|
|
|
#endif
|
|
|
|
|
UnityEngine.Object.Destroy(material);
|
2020-05-11 15:34:22 +08:00
|
|
|
|
}
|
2020-06-04 12:43:17 +08:00
|
|
|
|
|
|
|
|
|
material = null;
|
2020-05-11 15:34:22 +08:00
|
|
|
|
}
|
2020-06-04 12:43:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static class MaterialCache
|
|
|
|
|
{
|
|
|
|
|
static readonly Dictionary<Hash128, MaterialEntry> s_MaterialMap = new Dictionary<Hash128, MaterialEntry>();
|
2020-05-11 15:34:22 +08:00
|
|
|
|
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
[UnityEditor.InitializeOnLoadMethod]
|
|
|
|
|
private static void ClearCache()
|
|
|
|
|
{
|
2020-06-04 12:43:17 +08:00
|
|
|
|
foreach (var entry in s_MaterialMap.Values)
|
2020-05-11 15:34:22 +08:00
|
|
|
|
{
|
|
|
|
|
entry.Release();
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-04 12:43:17 +08:00
|
|
|
|
s_MaterialMap.Clear();
|
2020-05-11 15:34:22 +08:00
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
public static Material Register(Material material, Hash128 hash, Action<Material> onModify)
|
|
|
|
|
{
|
|
|
|
|
if (!hash.isValid) return null;
|
|
|
|
|
|
|
|
|
|
MaterialEntry entry;
|
2020-06-04 12:43:17 +08:00
|
|
|
|
if (!s_MaterialMap.TryGetValue(hash, out entry))
|
2020-05-11 15:34:22 +08:00
|
|
|
|
{
|
|
|
|
|
entry = new MaterialEntry()
|
|
|
|
|
{
|
|
|
|
|
material = new Material(material)
|
|
|
|
|
{
|
|
|
|
|
hideFlags = HideFlags.HideAndDontSave,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
onModify(entry.material);
|
2020-06-04 12:43:17 +08:00
|
|
|
|
s_MaterialMap.Add(hash, entry);
|
2020-05-11 15:34:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
entry.referenceCount++;
|
|
|
|
|
//Debug.LogFormat("Register: {0}, {1} (Total: {2})", hash, entry.referenceCount, materialMap.Count);
|
|
|
|
|
return entry.material;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Unregister(Hash128 hash)
|
|
|
|
|
{
|
|
|
|
|
MaterialEntry entry;
|
2020-06-04 12:43:17 +08:00
|
|
|
|
if (!hash.isValid || !s_MaterialMap.TryGetValue(hash, out entry)) return;
|
2020-05-11 15:34:22 +08:00
|
|
|
|
//Debug.LogFormat("Unregister: {0}, {1}", hash, entry.referenceCount -1);
|
|
|
|
|
|
|
|
|
|
if (--entry.referenceCount > 0) return;
|
|
|
|
|
|
|
|
|
|
entry.Release();
|
2020-06-04 12:43:17 +08:00
|
|
|
|
s_MaterialMap.Remove(hash);
|
2020-05-11 15:34:22 +08:00
|
|
|
|
//Debug.LogFormat("Unregister: Release Emtry: {0}, {1} (Total: {2})", hash, entry.referenceCount, materialMap.Count);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|