Files
Convention-Unity-Demo/Assets/Scripts/MaterialUpdatement.cs

75 lines
2.2 KiB
C#
Raw Normal View History

2025-09-25 19:04:05 +08:00
using System.Collections;
using System.Collections.Generic;
using Convention;
using UnityEngine;
namespace Demo.Game
{
2025-11-24 18:02:57 +08:00
public class MaterialUpdatement : Updatement<Material>, IAssetBundleLoader
2025-09-25 19:04:05 +08:00
{
public static MaterialUpdatement Make()
{
return new GameObject().AddComponent<MaterialUpdatement>();
}
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-11-24 18:02:57 +08:00
protected override Material Lerp(Material begin, Material end, float t)
2025-09-25 19:04:05 +08:00
{
return begin;
}
2025-11-24 18:02:57 +08:00
[Content, SerializeField] private Material Cache;
2025-09-25 19:04:05 +08:00
2025-11-24 18:02:57 +08:00
protected override void UpdateData(Material data)
2025-09-25 19:04:05 +08:00
{
if (string.IsNullOrEmpty(MaterialAssetBundlePath))
return;
if (Cache != data && Parent.TryGetComponent<MeshRenderer>(out var meshRenderer))
{
2025-11-24 18:02:57 +08:00
meshRenderer.material = data;
2025-09-25 19:04:05 +08:00
Cache = data;
}
}
public override IEnumerator UnloadScript()
{
Cache = null;
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>
[Convention.RScript.Variable.Attr.Method]
public IEnumerator Load(string ab)
2025-09-25 19:04:05 +08:00
{
yield return this.LoadAssetBundle(ab, x =>
2025-09-25 19:04:05 +08:00
{
MaterialAssetBundlePath = ab;
MaterialAssetBundle = x;
});
}
/// <summary>
/// 在指定时刻切换父物体上的MeshRenderer.material
/// </summary>
[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;
ManualAddEntry(time, mat, default);
};
yield return ir;
2025-09-25 19:04:05 +08:00
}
}
}