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

71 lines
2.1 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<string>, IAssetBundleLoader
{
public static MaterialUpdatement Make()
{
return new GameObject().AddComponent<MaterialUpdatement>();
}
public string MaterialAssetBundlePath;
public AssetBundle MaterialAssetBundle;
protected override string Lerp(string begin, string end, float t)
{
return begin;
}
[Content, SerializeField] private string Cache;
protected override void UpdateData(string data)
{
if (string.IsNullOrEmpty(MaterialAssetBundlePath))
return;
if (Cache != data && Parent.TryGetComponent<MeshRenderer>(out var meshRenderer))
{
meshRenderer.material = MaterialAssetBundle.LoadAsset<Material>(data);
Cache = data;
}
}
public override IEnumerator UnloadScript()
{
Cache = null;
if (string.IsNullOrEmpty(MaterialAssetBundlePath) == false)
yield return this.UnloadAssetBundle(MaterialAssetBundlePath);
MaterialAssetBundlePath = "";
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>
/// <param name="time"></param>
/// <param name="material"></param>
[Convention.RScript.Variable.Attr.Method]
public void Add(string time, string material)
{
ManualAddEntry(time, material, default);
}
}
}