86 lines
2.4 KiB
C#
86 lines
2.4 KiB
C#
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>
|
||
[ScriptableCall(@"
|
||
<summary>
|
||
对应ab包名称,自动匹配对应平台
|
||
</summary>
|
||
<param name=""ab""></param>
|
||
")]
|
||
public void Load(string ab)
|
||
{
|
||
var ir = this.LoadAssetBundle(ab, x =>
|
||
{
|
||
MaterialAssetBundlePath = ab;
|
||
MaterialAssetBundle = x;
|
||
});
|
||
if (gameObject.activeInHierarchy)
|
||
StartCoroutine(ir);
|
||
else
|
||
GetRoot().StartCoroutine(ir);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 在指定时刻切换父物体上的MeshRenderer.material
|
||
/// </summary>
|
||
/// <param name="time"></param>
|
||
/// <param name="material"></param>
|
||
[ScriptableCall(@"
|
||
<summary>
|
||
在指定时刻切换父物体上的MeshRenderer.material
|
||
</summary>
|
||
<param name=""time""></param>
|
||
<param name=""material""></param>
|
||
")]
|
||
public void Add(string time, string material)
|
||
{
|
||
ManualAddEntry(time, material, default);
|
||
}
|
||
}
|
||
}
|