113 lines
3.9 KiB
C#
113 lines
3.9 KiB
C#
using Convention;
|
|
using Demo.Attr;
|
|
using NUnit.Framework.Internal;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace Demo.Game
|
|
{
|
|
[Scriptable]
|
|
public class ParticleJudgement : IJudgementHookObject, IAssetBundleLoader
|
|
{
|
|
public static ParticleJudgement Make()
|
|
{
|
|
return new GameObject().AddComponent<ParticleJudgement>();
|
|
}
|
|
|
|
[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();
|
|
[Setting, SerializeField] private bool IsZooming = true;
|
|
[Setting, SerializeField] private MathExtension.EaseCurveType ZoomCurve = MathExtension.EaseCurveType.OutCubic;
|
|
|
|
[Convention.RScript.Variable.Attr.Method]
|
|
public void SetZooming(bool status)
|
|
{
|
|
IsZooming = status;
|
|
}
|
|
|
|
[Convention.RScript.Variable.Attr.Method]
|
|
public void SetZoomingCurve(MathExtension.EaseCurveType curve)
|
|
{
|
|
ZoomCurve = curve;
|
|
}
|
|
|
|
protected override IEnumerator DoSomethingDuringApplyScript()
|
|
{
|
|
yield return base.DoSomethingDuringApplyScript();
|
|
foreach (var task in TaskLoader)
|
|
yield return task;
|
|
}
|
|
|
|
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>
|
|
[Convention.RScript.Variable.Attr.Method]
|
|
public void Load(IInteraction.JudgementLevel levelId, string ab, string prefab, float duration)
|
|
{
|
|
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);
|
|
}));
|
|
}
|
|
|
|
private GameObject CreateParticle(GameObject prefab)
|
|
{
|
|
prefab.SetActive(true);
|
|
return prefab;
|
|
}
|
|
|
|
private void DestroyParticle(GameObject prefab)
|
|
{
|
|
prefab.SetActive(false);
|
|
}
|
|
|
|
private IEnumerator DoParticleRuntime(float duration, GameObject effect)
|
|
{
|
|
CreateParticle(effect);
|
|
if (IsZooming)
|
|
{
|
|
for (float clock = 0f; clock < duration; clock += Time.deltaTime)
|
|
{
|
|
float scale = MathExtension.Evaluate(clock / duration, ZoomCurve);
|
|
effect.transform.localScale = new Vector3(scale, scale, scale);
|
|
yield return null;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
effect.transform.localScale = Vector3.one;
|
|
yield return new WaitForSeconds(duration);
|
|
}
|
|
DestroyParticle(effect);
|
|
}
|
|
|
|
public override void OnJudgement(IInteraction.JudgementLevel level)
|
|
{
|
|
if (Prefabs.TryGetValue(level, out var effect))
|
|
{
|
|
ConventionUtility.StartCoroutine(DoParticleRuntime(Durations[level], effect));
|
|
}
|
|
}
|
|
}
|
|
}
|