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

133 lines
6.1 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 MyInteractionModule;
[Content, SerializeField] private InteractiveEffectType MyInteractiveLevel = default;
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();
public override IEnumerator LoadScript(string script)
{
yield return base.LoadScript(script);
if (MyInteractionModule == null)
MyInteractionModule = Parent.GetComponent<IInteraction>();
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;
}
MyInteractionModule = null;
yield return base.UnloadScript();
}
/// <summary>
/// 绑定IInteraction对象若不手动绑定则会自动绑定到父物体的IInteraction
/// </summary>
/// <param name="path"></param>
[ScriptableCall(@"
<summary>
IInteraction对象IInteraction
</summary>
")]
public void Bind(string path)
{
MyInteractionModule = FindWithPath(path) as IInteraction;
}
/// <summary>
/// 设置监听状态,当目标进入指定监听的状态时触发启动事件,退出时触发结束事件
/// </summary>
/// <param name="type">VisibleDurationInteractiveDurationInteractableScoreIntervalInteractableIntervalThatCanScoreBest</param>
[ScriptableCall(@"
<summary>
退
</summary>
<param name=""type"">VisibleDurationInteractiveDurationInteractableScoreIntervalInteractableIntervalThatCanScoreBest</param>
")]
public void SetInteractiveEffectType(string type)
{
MyInteractiveLevel = Enum.Parse<InteractiveEffectType>(type);
}
}
}