Files
Convention-Unity-Demo/Assets/Scripts/Interaction/IEffectHookObject.cs

135 lines
6.0 KiB
C#
Raw Normal View History

2025-09-25 19:04:05 +08:00
using System;
using System.Collections;
using Convention;
using UnityEngine;
namespace Demo.Game
{
public abstract class IEffectHookObject : ScriptableObject, IHookInteraction
{
public enum InteractiveEffectType
{
VisibleDuration = 3,
InteractiveDuration = 2,
InteractableScoreInterval = 1,
InteractableIntervalThatCanScoreBest = 0
}
[Content, SerializeField] private IInteraction m_MyInteractionModule;
2025-09-25 19:04:05 +08:00
[Content, SerializeField] private InteractiveEffectType MyInteractiveLevel = default;
private IInteraction MyInteractionModule
{
get
{
if (m_MyInteractionModule == null)
m_MyInteractionModule = Parent.GetComponent<IInteraction>();
return m_MyInteractionModule;
}
set
{
m_MyInteractionModule = value;
}
}
2025-10-08 00:35:50 +08:00
public abstract void OnInit();
2025-09-25 19:04:05 +08:00
public abstract void OnBegin();
public abstract void OnEnd();
protected override IEnumerator DoSomethingDuringApplyScript()
2025-09-25 19:04:05 +08:00
{
yield return base.DoSomethingDuringApplyScript();
2025-09-25 19:04:05 +08:00
switch (MyInteractiveLevel)
{
case InteractiveEffectType.VisibleDuration:
{
2025-10-08 00:35:50 +08:00
MyInteractionModule.VisibleDurationBeforeEvent.AddListener(OnInit);
2025-09-25 19:04:05 +08:00
MyInteractionModule.VisibleDurationBeginEvent.AddListener(OnBegin);
MyInteractionModule.VisibleDurationEndEvent.AddListener(OnEnd);
}
break;
case InteractiveEffectType.InteractiveDuration:
{
2025-10-08 00:35:50 +08:00
MyInteractionModule.InteractiveDurationBeforeEvent.AddListener(OnInit);
2025-09-25 19:04:05 +08:00
MyInteractionModule.InteractiveDurationBeginEvent.AddListener(OnBegin);
MyInteractionModule.InteractiveDurationEndEvent.AddListener(OnEnd);
}
break;
case InteractiveEffectType.InteractableScoreInterval:
{
2025-10-08 00:35:50 +08:00
MyInteractionModule.InteractableScoreIntervalBeforeEvent.AddListener(OnInit);
2025-09-25 19:04:05 +08:00
MyInteractionModule.InteractableScoreIntervalBeginEvent.AddListener(OnBegin);
MyInteractionModule.InteractableScoreIntervalEndEvent.AddListener(OnEnd);
}
break;
case InteractiveEffectType.InteractableIntervalThatCanScoreBest:
{
2025-10-08 00:35:50 +08:00
MyInteractionModule.InteractableIntervalThatCanScoreBestBeforeEvent.AddListener(OnInit);
2025-09-25 19:04:05 +08:00
MyInteractionModule.InteractableIntervalThatCanScoreBestBeginEvent.AddListener(OnBegin);
MyInteractionModule.InteractableIntervalThatCanScoreBestEndEvent.AddListener(OnEnd);
}
break;
}
}
public override IEnumerator UnloadScript()
{
// 如果是父物体那么在此处执行前就会重新生成对应的event
// 但通过绑定得到的其他物体并不能保证顺序关系
// 因此此处仍需要对象自行清理
switch (MyInteractiveLevel)
{
case InteractiveEffectType.VisibleDuration:
{
2025-10-08 00:35:50 +08:00
MyInteractionModule.VisibleDurationBeforeEvent.RemoveListener(OnInit);
2025-09-25 19:04:05 +08:00
MyInteractionModule.VisibleDurationBeginEvent.RemoveListener(OnBegin);
MyInteractionModule.VisibleDurationEndEvent.RemoveListener(OnEnd);
}
break;
case InteractiveEffectType.InteractiveDuration:
{
2025-10-08 00:35:50 +08:00
MyInteractionModule.InteractiveDurationBeforeEvent.RemoveListener(OnInit);
2025-09-25 19:04:05 +08:00
MyInteractionModule.InteractiveDurationBeginEvent.RemoveListener(OnBegin);
MyInteractionModule.InteractiveDurationEndEvent.RemoveListener(OnEnd);
}
break;
case InteractiveEffectType.InteractableScoreInterval:
{
2025-10-08 00:35:50 +08:00
MyInteractionModule.InteractableScoreIntervalBeforeEvent.RemoveListener(OnInit);
2025-09-25 19:04:05 +08:00
MyInteractionModule.InteractableScoreIntervalBeginEvent.RemoveListener(OnBegin);
MyInteractionModule.InteractableScoreIntervalEndEvent.RemoveListener(OnEnd);
}
break;
case InteractiveEffectType.InteractableIntervalThatCanScoreBest:
{
2025-10-08 00:35:50 +08:00
MyInteractionModule.InteractableIntervalThatCanScoreBestBeforeEvent.RemoveListener(OnInit);
2025-09-25 19:04:05 +08:00
MyInteractionModule.InteractableIntervalThatCanScoreBestBeginEvent.RemoveListener(OnBegin);
MyInteractionModule.InteractableIntervalThatCanScoreBestEndEvent.RemoveListener(OnEnd);
}
break;
}
yield return base.UnloadScript();
}
/// <summary>
/// 绑定IInteraction对象若不手动绑定则会自动绑定到父物体的IInteraction
/// </summary>
/// <param name="path"></param>
[Convention.RScript.Variable.Attr.Method]
public void Bind(IInteraction target)
2025-09-25 19:04:05 +08:00
{
MyInteractionModule = target;
2025-09-25 19:04:05 +08:00
}
/// <summary>
/// 设置监听状态,当目标进入指定监听的状态时触发启动事件,退出时触发结束事件
/// </summary>
/// <param name="type">VisibleDurationInteractiveDurationInteractableScoreIntervalInteractableIntervalThatCanScoreBest</param>
[Convention.RScript.Variable.Attr.Method]
public void SetInteractiveEffectType(InteractiveEffectType type)
2025-09-25 19:04:05 +08:00
{
MyInteractiveLevel = type;
2025-09-25 19:04:05 +08:00
}
}
}