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

80 lines
2.8 KiB
C#

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>();
}
[Content, SerializeField] private Dictionary<IInteraction.JudgementLevel, string> AssetBundles = new();
[Content, SerializeField] private Dictionary<IInteraction.JudgementLevel, GameObject> Prefabs = new();
[Content, SerializeField] private Dictionary<IInteraction.JudgementLevel, float> Durations = new();
public override IEnumerator UnloadScript()
{
yield return base.UnloadScript();
foreach (var ab in AssetBundles)
yield return this.UnloadAssetBundle(ab.Value);
}
/// <summary>
/// 加载预制体作为子物体
/// </summary>
/// <param name="level">正整数, 0级判定为最佳判定, 判定等级对应会出现的粒子效果</param>
/// <param name="ab"></param>
/// <param name="prefab"></param>
/// <param name="duration">判定效果会现形的持续时间</param>
[ScriptableCall(@"
<summary>
加载预制体作为子物体
</summary>
<param name=""level"">正整数, 0级判定为最佳判定, 判定等级对应会出现的粒子效果</param>
<param name=""ab""></param>
<param name=""prefab""></param>
<param name=""duration"">判定效果会现形的持续时间</param>
")]
public IEnumerator Load(string level, string ab, string prefab, string duration)
{
var levelId = (IInteraction.JudgementLevel)int.Parse(level);
AssetBundle assetBundle = null;
yield return this.LoadAssetBundle(ab, x => assetBundle = x);
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));
}
private void CreateParticle(GameObject prefab)
{
prefab.SetActive(true);
}
private void DestroyParticle(GameObject prefab)
{
prefab.SetActive(false);
}
public override void OnJudgement(IInteraction.JudgementLevel level)
{
if (Prefabs.TryGetValue(level, out var effect))
{
ConventionUtility.CreateSteps()
.Next(() => CreateParticle(effect))
.Wait(Durations[level], () => DestroyParticle(effect))
.Invoke();
}
}
}
}