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

78 lines
2.7 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>();
}
2025-12-02 16:39:29 +08:00
[Content, SerializeField] private List<IEnumerator> TaskLoader = new();
[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();
2025-09-25 19:04:05 +08:00
2025-12-02 16:39:29 +08:00
protected override IEnumerator DoSomethingDuringApplyScript()
{
yield return base.DoSomethingDuringApplyScript();
foreach (var task in TaskLoader)
yield return task;
}
2025-09-25 19:04:05 +08:00
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>
2025-09-25 19:04:05 +08:00
/// <param name="ab"></param>
/// <param name="prefab"></param>
/// <param name="duration">判定效果会现形的持续时间</param>
[Convention.RScript.Variable.Attr.Method]
2025-12-02 16:39:29 +08:00
public void Load(IInteraction.JudgementLevel levelId, string ab, string prefab, float duration)
2025-09-25 19:04:05 +08:00
{
2025-12-02 16:39:29 +08:00
TaskLoader.Add(this.LoadAssetBundle(ab, assetBundle =>
{
var obj = assetBundle.LoadAsset<GameObject>(prefab);
GameObject sub = Instantiate(obj);
sub.SetActive(false);
Prefabs.Add(levelId, sub);
sub.transform.SetParent(transform);
AssetBundles.Add(levelId, ab);
Durations.Add(levelId, duration);
}));
2025-09-25 19:04:05 +08:00
}
private void CreateParticle(GameObject prefab)
{
prefab.SetActive(true);
}
private void DestroyParticle(GameObject prefab)
{
prefab.SetActive(false);
}
public override void OnJudgement(IInteraction.JudgementLevel level)
2025-09-25 19:04:05 +08:00
{
if (Prefabs.TryGetValue(level, out var effect))
2025-09-25 19:04:05 +08:00
{
ConventionUtility.CreateSteps()
.Next(() => CreateParticle(effect))
.Wait(Durations[level], () => DestroyParticle(effect))
.Invoke();
2025-09-25 19:04:05 +08:00
}
}
}
}