Files
Convention-Unity-Demo/Assets/Scripts/Interaction/IEffectHookObject.cs
2025-12-15 17:20:55 +08:00

157 lines
6.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections;
using System.IO;
using Convention;
using UnityEngine;
namespace Demo.Game
{
namespace ConfigType
{
// IEffectHookObject 配置抽象基类Config
public class IEffectHookObjectConfig : ScriptLoadableConfig
{
public IEffectHookObject.InteractiveEffectType MyInteractiveLevel;
public override void Deserialize(BinaryReader reader)
{
MyInteractiveLevel = (IEffectHookObject.InteractiveEffectType)BinarySerializeUtility.ReadInt(reader);
base.Deserialize(reader);
}
public override void Serialize(BinaryWriter writer)
{
BinarySerializeUtility.WriteInt(writer, (int)MyInteractiveLevel);
base.Serialize(writer);
}
}
}
public abstract class IEffectHookObject : ScriptableObject, IHookInteraction
{
public enum InteractiveEffectType
{
VisibleDuration = 3,
InteractiveDuration = 2,
InteractableScoreInterval = 1,
InteractableIntervalThatCanScoreBest = 0
}
[Content, SerializeField] private IInteraction m_MyInteractionModule;
[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;
}
}
public abstract void OnInit();
public abstract void OnBegin();
public abstract void OnEnd();
protected override IEnumerator DoSomethingDuringApplyScript()
{
yield return base.DoSomethingDuringApplyScript();
switch (MyInteractiveLevel)
{
case InteractiveEffectType.VisibleDuration:
{
MyInteractionModule.VisibleDurationBeforeEvent.AddListener(OnInit);
MyInteractionModule.VisibleDurationBeginEvent.AddListener(OnBegin);
MyInteractionModule.VisibleDurationEndEvent.AddListener(OnEnd);
}
break;
case InteractiveEffectType.InteractiveDuration:
{
MyInteractionModule.InteractiveDurationBeforeEvent.AddListener(OnInit);
MyInteractionModule.InteractiveDurationBeginEvent.AddListener(OnBegin);
MyInteractionModule.InteractiveDurationEndEvent.AddListener(OnEnd);
}
break;
case InteractiveEffectType.InteractableScoreInterval:
{
MyInteractionModule.InteractableScoreIntervalBeforeEvent.AddListener(OnInit);
MyInteractionModule.InteractableScoreIntervalBeginEvent.AddListener(OnBegin);
MyInteractionModule.InteractableScoreIntervalEndEvent.AddListener(OnEnd);
}
break;
case InteractiveEffectType.InteractableIntervalThatCanScoreBest:
{
MyInteractionModule.InteractableIntervalThatCanScoreBestBeforeEvent.AddListener(OnInit);
MyInteractionModule.InteractableIntervalThatCanScoreBestBeginEvent.AddListener(OnBegin);
MyInteractionModule.InteractableIntervalThatCanScoreBestEndEvent.AddListener(OnEnd);
}
break;
}
}
public override IEnumerator UnloadScript()
{
// 如果是父物体那么在此处执行前就会重新生成对应的event
// 但通过绑定得到的其他物体并不能保证顺序关系
// 因此此处仍需要对象自行清理
switch (MyInteractiveLevel)
{
case InteractiveEffectType.VisibleDuration:
{
MyInteractionModule.VisibleDurationBeforeEvent.RemoveListener(OnInit);
MyInteractionModule.VisibleDurationBeginEvent.RemoveListener(OnBegin);
MyInteractionModule.VisibleDurationEndEvent.RemoveListener(OnEnd);
}
break;
case InteractiveEffectType.InteractiveDuration:
{
MyInteractionModule.InteractiveDurationBeforeEvent.RemoveListener(OnInit);
MyInteractionModule.InteractiveDurationBeginEvent.RemoveListener(OnBegin);
MyInteractionModule.InteractiveDurationEndEvent.RemoveListener(OnEnd);
}
break;
case InteractiveEffectType.InteractableScoreInterval:
{
MyInteractionModule.InteractableScoreIntervalBeforeEvent.RemoveListener(OnInit);
MyInteractionModule.InteractableScoreIntervalBeginEvent.RemoveListener(OnBegin);
MyInteractionModule.InteractableScoreIntervalEndEvent.RemoveListener(OnEnd);
}
break;
case InteractiveEffectType.InteractableIntervalThatCanScoreBest:
{
MyInteractionModule.InteractableIntervalThatCanScoreBestBeforeEvent.RemoveListener(OnInit);
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)
{
MyInteractionModule = target;
}
/// <summary>
/// 设置监听状态,当目标进入指定监听的状态时触发启动事件,退出时触发结束事件
/// </summary>
/// <param name="type">VisibleDurationInteractiveDurationInteractableScoreIntervalInteractableIntervalThatCanScoreBest</param>
[Convention.RScript.Variable.Attr.Method]
public void SetInteractiveEffectType(InteractiveEffectType type)
{
MyInteractiveLevel = type;
}
}
}