2025-12-02 15:26:41 +08:00
|
|
|
|
using Convention;
|
2025-12-12 15:19:10 +08:00
|
|
|
|
using Demo.Attr;
|
2025-09-25 19:04:05 +08:00
|
|
|
|
using System.Collections;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using UnityEngine;
|
2025-12-02 15:26:41 +08:00
|
|
|
|
using UnityEngine.Rendering.LookDev;
|
2025-09-25 19:04:05 +08:00
|
|
|
|
|
|
|
|
|
|
namespace Demo.Game
|
|
|
|
|
|
{
|
2025-12-12 15:19:10 +08:00
|
|
|
|
[Scriptable]
|
2025-12-02 15:26:41 +08:00
|
|
|
|
public class MaterialUpdatement : Updatement<int>, IAssetBundleLoader
|
2025-09-25 19:04:05 +08:00
|
|
|
|
{
|
|
|
|
|
|
public static MaterialUpdatement Make()
|
|
|
|
|
|
{
|
|
|
|
|
|
return new GameObject().AddComponent<MaterialUpdatement>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-02 15:26:41 +08:00
|
|
|
|
private readonly Dictionary<string, int> NameCache = new();
|
|
|
|
|
|
private readonly Dictionary<int, Material> MaterialCache = new();
|
|
|
|
|
|
|
2025-11-24 18:02:57 +08:00
|
|
|
|
public string MaterialAssetBundlePath = null;
|
|
|
|
|
|
public AssetBundle MaterialAssetBundle = null;
|
2025-09-25 19:04:05 +08:00
|
|
|
|
|
2025-12-02 15:26:41 +08:00
|
|
|
|
protected override int Lerp(int begin, int end, float t)
|
2025-09-25 19:04:05 +08:00
|
|
|
|
{
|
|
|
|
|
|
return begin;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-02 15:26:41 +08:00
|
|
|
|
[Content, SerializeField] private int Cache;
|
2025-09-25 19:04:05 +08:00
|
|
|
|
|
2025-12-02 15:26:41 +08:00
|
|
|
|
protected override void UpdateData(int data)
|
2025-09-25 19:04:05 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrEmpty(MaterialAssetBundlePath))
|
|
|
|
|
|
return;
|
2025-12-02 15:26:41 +08:00
|
|
|
|
if (Cache < 0)
|
|
|
|
|
|
return;
|
2025-09-25 19:04:05 +08:00
|
|
|
|
if (Cache != data && Parent.TryGetComponent<MeshRenderer>(out var meshRenderer))
|
|
|
|
|
|
{
|
2025-12-02 15:26:41 +08:00
|
|
|
|
meshRenderer.material = MaterialCache[data];
|
2025-09-25 19:04:05 +08:00
|
|
|
|
Cache = data;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override IEnumerator UnloadScript()
|
|
|
|
|
|
{
|
2025-12-02 15:26:41 +08:00
|
|
|
|
Cache = -1;
|
2025-09-25 19:04:05 +08:00
|
|
|
|
if (string.IsNullOrEmpty(MaterialAssetBundlePath) == false)
|
|
|
|
|
|
yield return this.UnloadAssetBundle(MaterialAssetBundlePath);
|
2025-11-24 18:02:57 +08:00
|
|
|
|
MaterialAssetBundlePath = null;
|
2025-09-25 19:04:05 +08:00
|
|
|
|
yield return base.UnloadScript();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 对应ab包名称,自动匹配对应平台
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="ab"></param>
|
2025-10-27 20:24:15 +08:00
|
|
|
|
[Convention.RScript.Variable.Attr.Method]
|
2025-10-06 16:09:52 +08:00
|
|
|
|
public IEnumerator Load(string ab)
|
2025-09-25 19:04:05 +08:00
|
|
|
|
{
|
2025-10-06 16:09:52 +08:00
|
|
|
|
yield return this.LoadAssetBundle(ab, x =>
|
2025-09-25 19:04:05 +08:00
|
|
|
|
{
|
|
|
|
|
|
MaterialAssetBundlePath = ab;
|
|
|
|
|
|
MaterialAssetBundle = x;
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 在指定时刻切换父物体上的MeshRenderer.material
|
|
|
|
|
|
/// </summary>
|
2025-10-27 20:24:15 +08:00
|
|
|
|
[Convention.RScript.Variable.Attr.Method]
|
2025-11-24 18:02:57 +08:00
|
|
|
|
public IEnumerator Add(float time, string material)
|
2025-09-25 19:04:05 +08:00
|
|
|
|
{
|
2025-11-24 18:02:57 +08:00
|
|
|
|
var ir = MaterialAssetBundle.LoadAssetAsync<Material>(material);
|
|
|
|
|
|
ir.completed += x =>
|
|
|
|
|
|
{
|
|
|
|
|
|
var mat = ir.asset as Material;
|
2025-12-02 15:26:41 +08:00
|
|
|
|
if (NameCache.TryGetValue(material, out int id) == false)
|
|
|
|
|
|
{
|
|
|
|
|
|
id = NameCache.Count;
|
|
|
|
|
|
NameCache[material] = id;
|
|
|
|
|
|
}
|
|
|
|
|
|
MaterialCache[id] = mat;
|
|
|
|
|
|
ManualAddEntry(time, id, default);
|
2025-11-24 18:02:57 +08:00
|
|
|
|
};
|
|
|
|
|
|
yield return ir;
|
2025-09-25 19:04:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|