Samples/ Newtonsoft JSON
This sample demonstrates a complete file-based save system using Newtonsoft JSON within Luna. It shows how to implement a concrete save manager that works seamlessly with Luna's UI components.
Refer to Save System Framework to learn about the UI-agnostic save system architecture.
Key Features: Pluggable serializers, secure type allow-lists, fast metadata loading, and complete UI integration.
What You Get
GameSaveManagerExample— concrete manager handling files, folders, and JSON IOGameSaveDataExample— example player/save data modelGameSaveMetadataExample— lightweight metadata for lists and previewsInitializeSerializerExample— safe serializer initialization with converters and type allow-list
Quick Start
1. Initialize Serializer
Add the serializer initializer to your bootstrap scene:
Keep the type allow-list tight for security when using TypeNameHandling features.
2. Create Save Data Classes
3. Create Metadata Class
Define lightweight metadata for UI display:
Use [JsonProperty(Order = -100)] on Metadata so it appears first in JSON for fast metadata reads.
4. Implement Save Manager
Inherit from GameSaveManager<MyGameSaveData, MyGameSaveMetadata> and implement the JSON-specific methods:
UI Integration
Wire your manager into Luna UI components by overriding the provider method:
Once connected, UI buttons (Continue, Load, New Game, Delete) automatically work through your manager. No additional UI code needed!
File Locations
- Save Directory:
Application.persistentDataPath/saves/ - File Extension:
.json(configured by your manager) - Version: Set via
GetSaveVersion()method
Advanced Features
Encryption
Add encryption/decryption in your manager's save/load methods:
Cloud Storage
Replace file IO with cloud API calls while keeping the same UI interface:
Troubleshooting
No save files found
Ensure Application.persistentDataPath/saves exists and the platform has write permissions.
Metadata shows null
Ensure Metadata is serialized first with [JsonProperty(Order = -100)] and created before saving.
Deserialization errors
Update the serializer allow-list and add required converters for custom types.
UI buttons disabled
Verify your manager returns valid metadata from GetLastMetadata() and the serializer is properly initialized.
Best Practices
- Use
[JsonProperty(Order = -100)]on Metadata for fast metadata reads - Implement fast metadata loading to avoid deserializing full saves for UI lists
- Keep type allow-lists minimal for security when using TypeNameHandling
- Handle version migration in your load methods for schema changes
- Use autosave functionality built into the manager
- Test save/load on target platforms early in development
Remember: The UI remains completely unchanged when you swap between JSON, binary, cloud, or encrypted storage. Only your manager implementation changes.