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

72 lines
2.2 KiB
C#
Raw Normal View History

2025-09-25 19:04:05 +08:00
using System.Collections;
2025-12-15 17:20:55 +08:00
using System.IO;
2025-09-25 19:04:05 +08:00
using Convention;
using UnityEngine;
namespace Demo.Game
{
2025-12-15 17:20:55 +08:00
namespace ConfigType
{
// IJudgementHookObject 配置抽象基类Config
public class IJudgementHookObjectConfig : ScriptLoadableConfig
{
2025-12-16 17:54:51 +08:00
public int MyInteractionModule;
2025-12-15 17:20:55 +08:00
public override void Deserialize(BinaryReader reader)
{
base.Deserialize(reader);
}
public override void Serialize(BinaryWriter writer)
{
base.Serialize(writer);
}
}
}
2025-09-25 19:04:05 +08:00
public abstract class IJudgementHookObject : ScriptableObject, IHookInteraction
{
[Content, SerializeField] private IInteraction m_MyInteractionModule;
private IInteraction MyInteractionModule
{
get
{
if (m_MyInteractionModule == null)
m_MyInteractionModule = Parent.GetComponent<IInteraction>();
return m_MyInteractionModule;
}
set
{
m_MyInteractionModule = value;
}
}
2025-09-25 19:04:05 +08:00
public abstract void OnJudgement(IInteraction.JudgementLevel level);
2025-09-25 19:04:05 +08:00
protected override IEnumerator DoSomethingDuringApplyScript()
2025-09-25 19:04:05 +08:00
{
yield return base.DoSomethingDuringApplyScript();
2025-09-25 19:04:05 +08:00
MyInteractionModule.JudgementEvent.AddListener(OnJudgement);
}
/// <summary>
/// 绑定IInteraction对象若不手动绑定则会自动绑定到父物体的IInteraction
/// </summary>
[Convention.RScript.Variable.Attr.Method]
2025-12-16 17:54:51 +08:00
public void Bind(IInteraction module)
2025-09-25 19:04:05 +08:00
{
2025-12-16 17:54:51 +08:00
MyInteractionModule = module;
2025-09-25 19:04:05 +08:00
}
public override IEnumerator UnloadScript()
{
// 如果是父物体那么在此处执行前就会重新生成对应的event
// 但通过绑定得到的其他物体并不能保证顺序关系
// 因此此处仍需要对象自行清理
MyInteractionModule.JudgementEvent.RemoveListener(OnJudgement);
MyInteractionModule = null;
yield return base.UnloadScript();
}
}
}