t1k:unity:base:save-system
| Field | Value |
|---|---|
| Module | base |
| Version | 2.2.2 |
| Effort | medium |
| Tools | — |
Keywords: data, persistence, save system, unity
How to invoke
Section titled “How to invoke”/t1k:unity:base:save-systemUnity Save System — Persistence Patterns
Section titled “Unity Save System — Persistence Patterns”Save system patterns for Unity 6. No built-in save framework — must implement.
Storage Locations
Section titled “Storage Locations”string savePath = Application.persistentDataPath;// Windows: AppData/LocalLow/<company>/<product>/// iOS/Android: platform Documents/files dirs
// PlayerPrefs — settings ONLY, never game saves:PlayerPrefs.SetInt("MusicVolume", 80);→ See references/save-patterns.md for all platform paths, full paths per OS.
Save Data Architecture
Section titled “Save Data Architecture”[System.Serializable]public class SaveData { public int version = 1; // For migration public string timestamp; public PlayerSaveData player; public List<QuestSaveData> quests; public WorldSaveData world;}Gotcha: JsonUtility doesn’t support Dictionary or float3. Use float[] arrays or Newtonsoft.Json.
Serialization Methods
Section titled “Serialization Methods”| Method | Use When | Notes |
|---|---|---|
JsonUtility | Simple data, no Dictionary | Fast, no package needed |
| Newtonsoft.Json | Complex types, polymorphism | More flexible |
BinaryWriter | Performance-critical | Manual field writing |
| Never | Deprecated, insecure |
→ See references/save-patterns.md for full SaveManager, binary, versioning, auto-save, and encryption code.
Key Patterns
Section titled “Key Patterns”- Versioning: Include
int versionin SaveData, migrate inLoadWithMigration() - Auto-save:
InvokeRepeating+OnApplicationPause+OnApplicationQuit - Encryption: XOR for obfuscation;
System.Security.Cryptography.Aesfor real security - Atomic write: Write to temp file, then rename to avoid corruption
Common Gotchas
Section titled “Common Gotchas”- WebGL: No filesystem — use
PlayerPrefsorIndexedDBvia JS interop - Threading: File I/O on main thread causes hitches — use
async/await - iOS: iCloud backup includes
persistentDataPathby default - Save corruption: Always write temp → rename (atomic operation)
Related Skills & Agents
Section titled “Related Skills & Agents”unity-addressables— Loading saved asset referencesunity-mobile— Platform-specific save pathsunity-scene-management— Save/load scene statedots-rpg— DOTS game state (usedots-implementeragent)
Reference Files
Section titled “Reference Files”| File | Contents |
|---|---|
references/save-patterns.md | Full code: JSON, binary, versioning, auto-save, encryption, gotchas |