Add PlayerAnimationSync NetworkBehaviour with owner-authoritative NetworkVariables for pose index and locomotion category (idle/walk/run/fall). Remote avatars switch their PlayerPoseController into network-controlled mode, skipping local input, sprint reset, capsule resize and camera framing, and take pose index plus locomotion externally. Disable pose persistence on network-driven remotes so foreign avatars can't overwrite the local save. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
127 lines
4.4 KiB
C#
127 lines
4.4 KiB
C#
using System;
|
|
using System.IO;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
public partial class PlayerPoseController
|
|
{
|
|
// --- Persistence: the selected pose index ---
|
|
//
|
|
// Saved on quit / play-stop and restored on the next start, so the player keeps the pose they
|
|
// had selected. Mirrors the project's other X.Persistence.cs partials (small JSON state under
|
|
// the shared MeshPaint folder).
|
|
|
|
[Header("Persistence")]
|
|
[Tooltip("Save the selected pose index on quit / play-stop and restore it on the next start. " +
|
|
"Off = the selection starts at the default pose.")]
|
|
public bool persistPose = true;
|
|
|
|
[Tooltip("Save file identifier. Empty = derived from the active scene + object name. Set " +
|
|
"explicitly to keep a save stable across object renames.")]
|
|
public string poseSaveId = "";
|
|
|
|
private const int PoseFileVersion = 1;
|
|
private bool _poseSavedOnTeardown; // guards a double save across OnApplicationQuit + OnDestroy
|
|
|
|
private static string PoseSaveDir => Path.Combine(Application.persistentDataPath, "MeshPaint");
|
|
|
|
private string PoseSavePath()
|
|
{
|
|
string key = string.IsNullOrEmpty(poseSaveId)
|
|
? SanitizePoseKey(SceneManager.GetActiveScene().name + "_" + gameObject.name)
|
|
: SanitizePoseKey(poseSaveId);
|
|
return Path.Combine(PoseSaveDir, key + ".pose");
|
|
}
|
|
|
|
private static string SanitizePoseKey(string s)
|
|
{
|
|
foreach (char c in Path.GetInvalidFileNameChars()) s = s.Replace(c, '_');
|
|
return s.Replace(' ', '_');
|
|
}
|
|
|
|
[Serializable]
|
|
private class PoseState
|
|
{
|
|
public int version = PoseFileVersion;
|
|
public int index;
|
|
}
|
|
|
|
void Start() => RestorePoseState();
|
|
|
|
private void OnApplicationQuit() => SavePoseOnTeardown();
|
|
private void OnDestroy() => SavePoseOnTeardown();
|
|
|
|
/// <summary>Saves once during teardown (OnApplicationQuit and OnDestroy both call this; only the first writes).</summary>
|
|
private void SavePoseOnTeardown()
|
|
{
|
|
if (_poseSavedOnTeardown) return;
|
|
_poseSavedOnTeardown = true;
|
|
SavePoseState();
|
|
}
|
|
|
|
/// <summary>Writes the current pose index to disk.</summary>
|
|
public void SavePoseState()
|
|
{
|
|
// A network-driven remote avatar shares the local "Player(Clone)" save key with the owned avatar;
|
|
// persisting it would clobber the local player's saved pose with a replicated one.
|
|
if (_networkControlled || !persistPose) return;
|
|
|
|
try
|
|
{
|
|
var state = new PoseState { index = _currentIndex };
|
|
Directory.CreateDirectory(PoseSaveDir);
|
|
File.WriteAllText(PoseSavePath(), JsonUtility.ToJson(state, true));
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogWarning($"[PlayerPoseController] Saving pose state failed: {e.Message}", this);
|
|
}
|
|
}
|
|
|
|
/// <summary>Restores the saved pose index, or does nothing if there is no save.</summary>
|
|
private void RestorePoseState()
|
|
{
|
|
// Skip on a network-driven remote avatar: its pose is replicated from the owner, and reading the
|
|
// local save would briefly show the wrong pose before the first network update arrives.
|
|
if (_networkControlled || !persistPose) return;
|
|
|
|
var state = LoadPoseState();
|
|
if (state == null) return;
|
|
SetPoseIndex(state.index);
|
|
}
|
|
|
|
private PoseState LoadPoseState()
|
|
{
|
|
string path = PoseSavePath();
|
|
if (!File.Exists(path)) return null;
|
|
|
|
try
|
|
{
|
|
var state = JsonUtility.FromJson<PoseState>(File.ReadAllText(path));
|
|
if (state == null || state.version != PoseFileVersion) return null;
|
|
return state;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogWarning($"[PlayerPoseController] Loading pose state failed: {e.Message}", this);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// <summary>Deletes the saved pose state (next start uses the default pose). Editor convenience.</summary>
|
|
[ContextMenu("Clear Saved Pose State")]
|
|
public void ClearSavedPoseState()
|
|
{
|
|
try
|
|
{
|
|
string path = PoseSavePath();
|
|
if (File.Exists(path)) File.Delete(path);
|
|
Debug.Log($"[PlayerPoseController] Cleared saved pose state: {path}", this);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Debug.LogWarning($"[PlayerPoseController] Clearing saved pose state failed: {e.Message}", this);
|
|
}
|
|
}
|
|
}
|