Table of Contents

Example - Save and Load

You want the camera to come back exactly where the player left it—across save slots, scene reloads, or app restarts.

GetState returns an OrbitalCameraState struct holding position, Y-rotation, tilt, and zoom. SetState puts those values back. The struct is [Serializable], so it works with JsonUtility.

using UnityEngine;
using Goehler.OrbitalCam;

public class CameraSaveLoad : MonoBehaviour {

    private const string SAVE_KEY = "MyGame.CameraState";

    [SerializeField] private OrbitalCameraSystem _camera;

    public void Save() {
        string json = JsonUtility.ToJson(_camera.GetState());
        PlayerPrefs.SetString(SAVE_KEY, json);
        PlayerPrefs.Save();
    }

    public void Load() {
        if (!PlayerPrefs.HasKey(SAVE_KEY)) return;
        string json = PlayerPrefs.GetString(SAVE_KEY);
        OrbitalCameraState state = JsonUtility.FromJson<OrbitalCameraState>(json);
        _camera.SetState(state);
    }
}

PlayerPrefs is fine for a quick demo. For real save games, write the same JSON string into your existing save file alongside the rest of your game state.

Note

SetState clamps everything back into legal range on the way in: position to your bounding box (if enabled), tilt to your tilt range, and zoom to your zoom range. So a state captured under different limits won't blow up; it'll just snap to the nearest valid value.

Note

The follow target is not part of the snapshot. Transform references don't survive serialization, so save the target's identity yourself (a unit ID, a scene GUID, etc.) and call SetFollowTarget after SetState once you've resolved the transform on load.

This pattern is wired up in the Scripting Demo scene. The Save and Load UI buttons drive a CameraStateSaveLoadDemo component, which uses the same pattern as the script above. Move the camera around, click Save, restart Play mode, click Load.