Files
Convention-Unity-Demo/Assets/Scripts/Interaction/JudgementEffect/ParticleJudgement.cs

97 lines
3.3 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
{
public class ParticleJudgement : IJudgementHookObject, IAssetBundleLoader
{
public static ParticleJudgement Make()
{
return new GameObject().AddComponent<ParticleJudgement>();
}
private List<IEnumerator> AssetBundleLoadings = new();
[Content, SerializeField] private Dictionary<int, string> AssetBundles = new();
[Content, SerializeField] private Dictionary<int, GameObject> Prefabs = new();
[Content, SerializeField] private Dictionary<int, float> Durations = new();
public override IEnumerator LoadScript(string script)
{
yield return base.LoadScript(script);
foreach (var loading in AssetBundleLoadings)
yield return loading;
}
public override IEnumerator UnloadScript()
{
yield return base.UnloadScript();
foreach (var ab in AssetBundles)
yield return this.UnloadAssetBundle(ab.Value);
}
/// <summary>
/// 加载预制体作为子物体
/// </summary>
/// <param name="level">判定等级对应会出现的粒子效果,若没有对应的则向更低的值寻找</param>
/// <param name="ab"></param>
/// <param name="prefab"></param>
/// <param name="duration">判定效果的持续时间</param>
[ScriptableCall(@"
<summary>
</summary>
<param name=""level""></param>
<param name=""ab""></param>
<param name=""prefab""></param>
<param name=""duration""></param>
")]
public void Load(string level, string ab, string prefab, string duration)
{
int levelId = int.Parse(level);
IEnumerator Foo()
{
AssetBundle assetBundle = null;
var ir = this.LoadAssetBundle(ab, x => assetBundle = x);
yield return ir;
var req = assetBundle.LoadAssetAsync<GameObject>(prefab);
yield return req;
GameObject sub = Instantiate(req.asset as GameObject);
sub.SetActive(false);
Prefabs.Add(levelId, sub);
sub.transform.SetParent(transform);
AssetBundles.Add(levelId, ab);
Durations.Add(levelId, float.Parse(duration));
}
AssetBundleLoadings.Add(Foo());
}
private void CreateParticle(GameObject prefab)
{
prefab.SetActive(true);
}
private void DestroyParticle(GameObject prefab)
{
prefab.SetActive(false);
}
public override void OnJudgement(int level)
{
for (int i = level; i >= 0; i--)
{
if (Prefabs.TryGetValue(i, out var effect))
{
ConventionUtility.CreateSteps()
.Next(() => CreateParticle(effect))
.Wait(Durations[i], () => DestroyParticle(effect))
.Invoke();
}
}
}
}
}