2020-10-28 01:07:23 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace Coffee.UIParticleExtensions
|
|
|
|
|
{
|
|
|
|
|
internal class ModifiedMaterial
|
|
|
|
|
{
|
|
|
|
|
private static readonly List<MatEntry> s_Entries = new List<MatEntry>();
|
|
|
|
|
|
|
|
|
|
public static Material Add(Material baseMat, Texture texture, int id)
|
|
|
|
|
{
|
|
|
|
|
MatEntry e;
|
2023-08-17 08:43:02 +08:00
|
|
|
|
for (var i = 0; i < s_Entries.Count; i++)
|
2020-10-28 01:07:23 +08:00
|
|
|
|
{
|
|
|
|
|
e = s_Entries[i];
|
|
|
|
|
if (e.baseMat != baseMat || e.texture != texture || e.id != id) continue;
|
|
|
|
|
++e.count;
|
|
|
|
|
return e.customMat;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-17 08:43:02 +08:00
|
|
|
|
e = new MatEntry
|
|
|
|
|
{
|
|
|
|
|
count = 1,
|
|
|
|
|
baseMat = baseMat,
|
|
|
|
|
texture = texture,
|
|
|
|
|
id = id,
|
|
|
|
|
customMat = new Material(baseMat)
|
|
|
|
|
{
|
|
|
|
|
name = $"{baseMat.name}_{id}",
|
|
|
|
|
hideFlags = HideFlags.HideAndDontSave,
|
|
|
|
|
mainTexture = texture ? texture : null
|
|
|
|
|
}
|
|
|
|
|
};
|
2020-10-28 01:07:23 +08:00
|
|
|
|
s_Entries.Add(e);
|
2022-06-08 11:54:11 +08:00
|
|
|
|
//Debug.LogFormat(">>>> ModifiedMaterial.Add -> count = count:{0}, mat:{1}, tex:{2}, id:{3}", s_Entries.Count, baseMat, texture, id);
|
2020-10-28 01:07:23 +08:00
|
|
|
|
return e.customMat;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Remove(Material customMat)
|
|
|
|
|
{
|
|
|
|
|
if (!customMat) return;
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < s_Entries.Count; ++i)
|
|
|
|
|
{
|
|
|
|
|
var e = s_Entries[i];
|
|
|
|
|
if (e.customMat != customMat) continue;
|
|
|
|
|
if (--e.count == 0)
|
|
|
|
|
{
|
2022-06-08 11:54:11 +08:00
|
|
|
|
//Debug.LogFormat(">>>> ModifiedMaterial.Remove -> count:{0}, mat:{1}, tex:{2}, id:{3}", s_Entries.Count - 1, e.customMat, e.texture, e.id);
|
2023-08-17 08:43:02 +08:00
|
|
|
|
Misc.DestroyImmediate(e.customMat);
|
|
|
|
|
e.customMat = null;
|
2020-10-28 01:07:23 +08:00
|
|
|
|
e.baseMat = null;
|
|
|
|
|
e.texture = null;
|
|
|
|
|
s_Entries.RemoveAt(i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class MatEntry
|
|
|
|
|
{
|
|
|
|
|
public Material baseMat;
|
|
|
|
|
public int count;
|
2023-08-17 08:43:02 +08:00
|
|
|
|
public Material customMat;
|
2020-10-28 01:07:23 +08:00
|
|
|
|
public int id;
|
2023-08-17 08:43:02 +08:00
|
|
|
|
public Texture texture;
|
2020-10-28 01:07:23 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|