Files
zeling_v2/Assets/Scripts/Core/Save/SaveableMonoBehaviour.cs
2026-05-12 15:34:08 +08:00

26 lines
1.1 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 UnityEngine;
using BaseGames.Core;
namespace BaseGames.Core.Save
{
/// <summary>
/// 带自动注册/注销的 ISaveable MonoBehaviour 基类。
/// 继承此类可消除每个存档对象手动调用 ServiceLocator.GetOrDefault&lt;SaveManager&gt;()?.Register/Unregister 的样板代码。
///
/// 生命周期:
/// OnEnable → ServiceLocator.GetOrDefault&lt;SaveManager&gt;()?.Register(this)
/// OnDisable → ServiceLocator.GetOrDefault&lt;SaveManager&gt;()?.Unregister(this)
///
/// 子类只需实现 <see cref="OnSave"/> 和 <see cref="OnLoad"/>。
/// 若子类需要自定义 OnEnable/OnDisable请先调用 base.OnEnable() / base.OnDisable()。
/// </summary>
public abstract class SaveableMonoBehaviour : MonoBehaviour, ISaveable
{
protected virtual void OnEnable() => ServiceLocator.GetOrDefault<ISaveableRegistry>()?.Register(this);
protected virtual void OnDisable() => ServiceLocator.GetOrDefault<ISaveableRegistry>()?.Unregister(this);
public abstract void OnSave(SaveData saveData);
public abstract void OnLoad(SaveData saveData);
}
}