2025-12-12 15:19:10 +08:00
|
|
|
|
using Convention;
|
|
|
|
|
|
using Demo.Attr;
|
2025-09-25 19:04:05 +08:00
|
|
|
|
using System.Collections;
|
2025-12-02 15:26:41 +08:00
|
|
|
|
using System.Collections.Generic;
|
2025-09-25 19:04:05 +08:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Demo.Game
|
|
|
|
|
|
{
|
2025-12-12 15:19:10 +08:00
|
|
|
|
[Scriptable]
|
2025-12-02 15:26:41 +08:00
|
|
|
|
public class SkyUpdatement : Updatement<int>, IAssetBundleLoader
|
2025-09-25 19:04:05 +08:00
|
|
|
|
{
|
|
|
|
|
|
public static SkyUpdatement Make()
|
|
|
|
|
|
{
|
|
|
|
|
|
return new GameObject().AddComponent<SkyUpdatement>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-04 16:53:05 +08:00
|
|
|
|
private int SkyAssetBundleLoaderStatus = 0;
|
2025-12-02 15:26:41 +08:00
|
|
|
|
private readonly Dictionary<string, int> NameCache = new();
|
|
|
|
|
|
private readonly Dictionary<int, Material> MaterialCache = new();
|
|
|
|
|
|
|
2025-09-25 19:04:05 +08:00
|
|
|
|
public string SkyAssetBundlePath;
|
|
|
|
|
|
public AssetBundle SkyAssetBundle;
|
|
|
|
|
|
|
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-04 16:53:05 +08:00
|
|
|
|
[Content, SerializeField] private int Cache = -1;
|
|
|
|
|
|
|
|
|
|
|
|
protected override IEnumerator DoSomethingDuringApplyScript()
|
|
|
|
|
|
{
|
|
|
|
|
|
yield return base.DoSomethingDuringApplyScript();
|
|
|
|
|
|
yield return new WaitUntil(() => SkyAssetBundleLoaderStatus == 0);
|
|
|
|
|
|
}
|
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(SkyAssetBundlePath))
|
|
|
|
|
|
return;
|
|
|
|
|
|
if (Cache != data)
|
|
|
|
|
|
{
|
2025-12-04 16:53:05 +08:00
|
|
|
|
if (data < 0)
|
|
|
|
|
|
RenderSettings.skybox = null;
|
|
|
|
|
|
else
|
|
|
|
|
|
RenderSettings.skybox = 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(SkyAssetBundlePath) == false)
|
|
|
|
|
|
yield return this.UnloadAssetBundle(SkyAssetBundlePath);
|
|
|
|
|
|
SkyAssetBundlePath = "";
|
|
|
|
|
|
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-12-04 16:53:05 +08:00
|
|
|
|
public void Load(string ab)
|
2025-09-25 19:04:05 +08:00
|
|
|
|
{
|
2025-12-04 16:53:05 +08:00
|
|
|
|
SkyAssetBundleLoaderStatus++;
|
|
|
|
|
|
ConventionUtility.StartCoroutine(this.LoadAssetBundle(ab, x =>
|
|
|
|
|
|
{
|
|
|
|
|
|
SkyAssetBundlePath = ab;
|
|
|
|
|
|
SkyAssetBundle = x;
|
|
|
|
|
|
SkyAssetBundleLoaderStatus--;
|
|
|
|
|
|
}));
|
2025-09-25 19:04:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 在指定时刻切换天空
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="time"></param>
|
|
|
|
|
|
/// <param name="sky"></param>
|
2025-10-27 20:24:15 +08:00
|
|
|
|
[Convention.RScript.Variable.Attr.Method]
|
2025-12-04 16:53:05 +08:00
|
|
|
|
public void Add(float time, string sky)
|
2025-09-25 19:04:05 +08:00
|
|
|
|
{
|
2025-12-04 16:53:05 +08:00
|
|
|
|
IEnumerator Foo()
|
2025-11-25 10:52:39 +08:00
|
|
|
|
{
|
2025-12-04 16:53:05 +08:00
|
|
|
|
yield return new WaitUntil(() => SkyAssetBundle != null);
|
|
|
|
|
|
var ir = SkyAssetBundle.LoadAssetAsync<Material>(sky);
|
|
|
|
|
|
ir.completed += delegate
|
2025-12-02 15:26:41 +08:00
|
|
|
|
{
|
2025-12-04 16:53:05 +08:00
|
|
|
|
var mat = ir.asset as Material;
|
|
|
|
|
|
if (NameCache.TryGetValue(sky, out int id) == false)
|
|
|
|
|
|
{
|
|
|
|
|
|
id = NameCache.Count;
|
|
|
|
|
|
NameCache[sky] = id;
|
|
|
|
|
|
}
|
|
|
|
|
|
MaterialCache[id] = mat;
|
|
|
|
|
|
ManualAddEntry(time, id, default);
|
|
|
|
|
|
SkyAssetBundleLoaderStatus--;
|
|
|
|
|
|
};
|
|
|
|
|
|
yield return ir;
|
|
|
|
|
|
}
|
|
|
|
|
|
SkyAssetBundleLoaderStatus++;
|
|
|
|
|
|
ConventionUtility.StartCoroutine(Foo());
|
2025-11-12 10:18:50 +08:00
|
|
|
|
}
|
2025-09-25 19:04:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|