Files
semantic-release-bot 7371e889ad 4.0.0-preview.8
# [4.0.0-preview.8](https://github.com/mob-sakai/UIEffect/compare/v4.0.0-preview.7...v4.0.0-preview.8) (2020-06-04)

### change

* change namespace ([7f4190c](https://github.com/mob-sakai/UIEffect/commit/7f4190c8c6f294e90869767a96194ec5ac4ba57f))

### Features

* modify EffectPlayer at runtime ([8483ba6](https://github.com/mob-sakai/UIEffect/commit/8483ba63cb546ca032915c166c0333fa5fd7aa76)), closes [#214](https://github.com/mob-sakai/UIEffect/issues/214) [#215](https://github.com/mob-sakai/UIEffect/issues/215)

### BREAKING CHANGES

* If your code contained the UIEffect API, it would fail to compile. Please change the namespace from  to .
2020-06-04 08:22:42 +00:00

78 lines
2.0 KiB
C#

using System.Collections.Generic;
using System.Linq;
using System;
using UnityEngine;
using System.Text;
using UnityEngine.UI;
namespace Coffee.UIEffects
{
public class MaterialCache
{
static Dictionary<Hash128, MaterialEntry> materialMap = new Dictionary<Hash128, MaterialEntry>();
private class MaterialEntry
{
public Material material;
public int referenceCount;
public void Release()
{
if (material)
{
UnityEngine.Object.DestroyImmediate(material, false);
}
material = null;
}
}
#if UNITY_EDITOR
[UnityEditor.InitializeOnLoadMethod]
private static void ClearCache()
{
foreach (var entry in materialMap.Values)
{
entry.Release();
}
materialMap.Clear();
}
#endif
public static Material Register(Material baseMaterial, Hash128 hash,
System.Action<Material, Graphic> onModifyMaterial, Graphic graphic)
{
if (!hash.isValid) return null;
MaterialEntry entry;
if (!materialMap.TryGetValue(hash, out entry))
{
entry = new MaterialEntry()
{
material = new Material(baseMaterial)
{
hideFlags = HideFlags.HideAndDontSave,
},
};
onModifyMaterial(entry.material, graphic);
materialMap.Add(hash, entry);
}
entry.referenceCount++;
return entry.material;
}
public static void Unregister(Hash128 hash)
{
MaterialEntry entry;
if (!hash.isValid || !materialMap.TryGetValue(hash, out entry)) return;
if (--entry.referenceCount > 0) return;
entry.Release();
materialMap.Remove(hash);
}
}
}