Skip to content

A library for saving player's progress in Unity.

License

Notifications You must be signed in to change notification settings

Delt06/persistence

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Persistence

Version License: MIT

A library for saving player's progress in Unity.

Developed and tested with Unity 2020.3.16f1

Table of contents

Installation

Option 1

Option 2

Add the following line to Packages/manifest.json:

"com.deltation.persistence": "https://github.com/Delt06/persistence.git?path=Packages/com.deltation.persistence",

Usage

  • Create a persistent model:
using System;

[Serializable]
public class DemoModel
{
    public string PlayerName = "Player";
    public string[] Data = { "1", "2", "3" };
}
  • Define a component that will manage the persistent state:
using DELTation.Persistence;
using DELTation.Persistence.Building;

public class DemoPersistentStateMono : PersistentStateMonoBase<DemoModel>
{
    protected override void ConstructPersistentState(PersistentStateBuilder<DemoModel> builder)
    {
        builder
            .WithJsonUtilitySerializer() // use JSON
            .WithDefaultModelFallback() // use default constructor if no data was stored
            .WithPeriodicWrite(1f) // save once a second automatically
            ;
    }
}
  • Use the component to manage persistent data
using DELTation.Persistence;
using UnityEngine;
using UnityEngine.UI;

public class Demo : MonoBehaviour
{
    [SerializeField] private InputField _text;

    private PersistentStateMonoBase<DemoModel> _modelContainer;

    private void Awake()
    {
        // get access to the component
        _modelContainer = GetComponentInChildren<PersistentStateMonoBase<DemoModel>>();
    }

    public void Read()
    {
        // read a value from the model 
        _text.text = _modelContainer.Model.PlayerName;
    }

    public void Write()
    {
        // write a value to the model and then save it (mark as dirty)
        _modelContainer.Model.PlayerName = _text.text;
        _modelContainer.Save();
    }
}

About

A library for saving player's progress in Unity.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages