122 lines
3.6 KiB
C#
122 lines
3.6 KiB
C#
using Core;
|
|
using UnityEngine;
|
|
|
|
namespace Interactions
|
|
{
|
|
/// <summary>
|
|
/// Base class for interactables that participate in the save/load system.
|
|
/// Provides common save ID generation and serialization infrastructure.
|
|
/// </summary>
|
|
public abstract class SaveableInteractable : InteractableBase
|
|
{
|
|
[Header("Save System")]
|
|
[SerializeField]
|
|
|
|
// Save system configuration
|
|
public override bool AutoRegisterForSave => true;
|
|
|
|
/// <summary>
|
|
/// Flag to indicate we're currently restoring from save data.
|
|
/// Child classes can check this to skip initialization logic during load.
|
|
/// </summary>
|
|
protected bool IsRestoringFromSave { get; private set; }
|
|
|
|
|
|
#region Save/Load Lifecycle Hooks
|
|
|
|
protected override string OnSceneSaveRequested()
|
|
{
|
|
object stateData = GetSerializableState();
|
|
if (stateData == null)
|
|
{
|
|
return "{}";
|
|
}
|
|
|
|
return JsonUtility.ToJson(stateData);
|
|
}
|
|
|
|
protected override void OnSceneRestoreRequested(string serializedData)
|
|
{
|
|
if (string.IsNullOrEmpty(serializedData))
|
|
{
|
|
Logging.Warning($"[SaveableInteractable] Empty save data for {SaveId}");
|
|
return;
|
|
}
|
|
|
|
// OnSceneRestoreRequested is guaranteed by the lifecycle system to only fire during actual restoration
|
|
// No need to check IsRestoringState - the lifecycle manager handles timing deterministically
|
|
IsRestoringFromSave = true;
|
|
|
|
try
|
|
{
|
|
ApplySerializableState(serializedData);
|
|
}
|
|
catch (System.Exception e)
|
|
{
|
|
Debug.LogError($"[SaveableInteractable] Failed to restore state for {SaveId}: {e.Message}");
|
|
}
|
|
finally
|
|
{
|
|
IsRestoringFromSave = false;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Virtual Methods for Child Classes
|
|
|
|
/// <summary>
|
|
/// Child classes override this to return their serializable state data.
|
|
/// Return an object that can be serialized with JsonUtility.
|
|
/// </summary>
|
|
protected abstract object GetSerializableState();
|
|
|
|
/// <summary>
|
|
/// Child classes override this to apply restored state data.
|
|
/// Should NOT trigger events or re-initialize logic that already happened.
|
|
/// </summary>
|
|
/// <param name="serializedData">JSON string containing the saved state</param>
|
|
protected abstract void ApplySerializableState(string serializedData);
|
|
|
|
#endregion
|
|
|
|
#region Editor Helpers
|
|
|
|
#if UNITY_EDITOR
|
|
[ContextMenu("Log Save ID")]
|
|
private void LogSaveId()
|
|
{
|
|
Logging.Debug($"Save ID: {SaveId}");
|
|
}
|
|
|
|
[ContextMenu("Test Serialize/Deserialize")]
|
|
private void TestSerializeDeserialize()
|
|
{
|
|
string serialized = OnSceneSaveRequested();
|
|
Logging.Debug($"Serialized state: {serialized}");
|
|
|
|
OnSceneRestoreRequested(serialized);
|
|
Logging.Debug("Deserialization test complete");
|
|
}
|
|
#endif
|
|
|
|
#endregion
|
|
}
|
|
|
|
#region Common Save Data Structures
|
|
|
|
/// <summary>
|
|
/// Base save data for all interactables.
|
|
/// Can be extended by child classes.
|
|
/// </summary>
|
|
[System.Serializable]
|
|
public class InteractableBaseSaveData
|
|
{
|
|
public bool isActive;
|
|
public Vector3 worldPosition;
|
|
public Quaternion worldRotation;
|
|
}
|
|
|
|
#endregion
|
|
}
|