Files
Convention-Unity-Demo/Assets/Scripts/MaterialUpdatement.cs
2025-11-24 18:02:57 +08:00

75 lines
2.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using Convention;
using UnityEngine;
namespace Demo.Game
{
public class MaterialUpdatement : Updatement<Material>, IAssetBundleLoader
{
public static MaterialUpdatement Make()
{
return new GameObject().AddComponent<MaterialUpdatement>();
}
public string MaterialAssetBundlePath = null;
public AssetBundle MaterialAssetBundle = null;
protected override Material Lerp(Material begin, Material end, float t)
{
return begin;
}
[Content, SerializeField] private Material Cache;
protected override void UpdateData(Material data)
{
if (string.IsNullOrEmpty(MaterialAssetBundlePath))
return;
if (Cache != data && Parent.TryGetComponent<MeshRenderer>(out var meshRenderer))
{
meshRenderer.material = data;
Cache = data;
}
}
public override IEnumerator UnloadScript()
{
Cache = null;
if (string.IsNullOrEmpty(MaterialAssetBundlePath) == false)
yield return this.UnloadAssetBundle(MaterialAssetBundlePath);
MaterialAssetBundlePath = null;
yield return base.UnloadScript();
}
/// <summary>
/// 对应ab包名称自动匹配对应平台
/// </summary>
/// <param name="ab"></param>
[Convention.RScript.Variable.Attr.Method]
public IEnumerator Load(string ab)
{
yield return this.LoadAssetBundle(ab, x =>
{
MaterialAssetBundlePath = ab;
MaterialAssetBundle = x;
});
}
/// <summary>
/// 在指定时刻切换父物体上的MeshRenderer.material
/// </summary>
[Convention.RScript.Variable.Attr.Method]
public IEnumerator Add(float time, string material)
{
var ir = MaterialAssetBundle.LoadAssetAsync<Material>(material);
ir.completed += x =>
{
var mat = ir.asset as Material;
ManualAddEntry(time, mat, default);
};
yield return ir;
}
}
}