GithubHelp home page GithubHelp logo

persistence's Introduction

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();
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.