using System; using System.Collections; using System.Collections.Generic; 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; 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(); switch (MyInteractiveLevel) { case InteractiveEffectType.VisibleDuration: { MyInteractionModule.VisibleDurationBeginEvent.AddListener(OnBegin); MyInteractionModule.VisibleDurationEndEvent.AddListener(OnEnd); } break; case InteractiveEffectType.InteractiveDuration: { MyInteractionModule.InteractiveDurationBeginEvent.AddListener(OnBegin); MyInteractionModule.InteractiveDurationEndEvent.AddListener(OnEnd); } break; case InteractiveEffectType.InteractableScoreInterval: { MyInteractionModule.InteractableScoreIntervalBeginEvent.AddListener(OnBegin); MyInteractionModule.InteractableScoreIntervalEndEvent.AddListener(OnEnd); } break; case InteractiveEffectType.InteractableIntervalThatCanScoreBest: { MyInteractionModule.InteractableIntervalThatCanScoreBestBeginEvent.AddListener(OnBegin); MyInteractionModule.InteractableIntervalThatCanScoreBestEndEvent.AddListener(OnEnd); } break; } } public override IEnumerator UnloadScript() { // 如果是父物体那么在此处执行前就会重新生成对应的event // 但通过绑定得到的其他物体并不能保证顺序关系 // 因此此处仍需要对象自行清理 switch (MyInteractiveLevel) { case InteractiveEffectType.VisibleDuration: { MyInteractionModule.VisibleDurationBeginEvent.RemoveListener(OnBegin); MyInteractionModule.VisibleDurationEndEvent.RemoveListener(OnEnd); } break; case InteractiveEffectType.InteractiveDuration: { MyInteractionModule.InteractiveDurationBeginEvent.RemoveListener(OnBegin); MyInteractionModule.InteractiveDurationEndEvent.RemoveListener(OnEnd); } break; case InteractiveEffectType.InteractableScoreInterval: { MyInteractionModule.InteractableScoreIntervalBeginEvent.RemoveListener(OnBegin); MyInteractionModule.InteractableScoreIntervalEndEvent.RemoveListener(OnEnd); } break; case InteractiveEffectType.InteractableIntervalThatCanScoreBest: { MyInteractionModule.InteractableIntervalThatCanScoreBestBeginEvent.RemoveListener(OnBegin); MyInteractionModule.InteractableIntervalThatCanScoreBestEndEvent.RemoveListener(OnEnd); } break; } MyInteractionModule = null; yield return base.UnloadScript(); } /// /// 绑定IInteraction对象,若不手动绑定则会自动绑定到父物体的IInteraction /// /// [ScriptableCall(@" 绑定IInteraction对象,若不手动绑定则会自动绑定到父物体的IInteraction ")] public void Bind(string path) { MyInteractionModule = FindWithPath(path) as IInteraction; } /// /// 设置监听状态,当目标进入指定监听的状态时触发启动事件,退出时触发结束事件 /// /// VisibleDuration,InteractiveDuration,InteractableScoreInterval,InteractableIntervalThatCanScoreBest [ScriptableCall(@" 设置监听状态,当目标进入指定监听的状态时触发启动事件,退出时触发结束事件 VisibleDuration,InteractiveDuration,InteractableScoreInterval,InteractableIntervalThatCanScoreBest ")] public void SetInteractiveEffectType(string type) { MyInteractiveLevel = Enum.Parse(type); } } }