Coder Social home page Coder Social logo

pepersistence's Introduction

Pepersistence

Extensible save/load system package for Unity game engine, designed to simplify the management of game data persistence.

Installation

You can install Pepersistence in one of the following ways:

Using Unity Package Manager:

  1. Open your Unity project.
  2. Go to the Unity Package Manager via Window > Package Manager.
  3. Click on the "Add package from git URL" button.
  4. Enter the following URL: https://github.com/Aleshmandr/Pepersistence.git.

Manual Download:

Alternatively, you can download the source files directly from the Pepersistence GitHub repository and import them into your Unity project manually.

Package dependencies:

This package depends on Json.NET by Newtonsoft (com.unity.nuget.newtonsoft-json version 3.1.2)

How to use

Follow these steps to integrate Pepersistence into your project:

  1. Create a class for your global savable game data that implements the ISaveData interface:

    [Serializable]
    public class GameSaveData : ISaveData
    {
        public LevelsSaveData Levels;
    }
    [Serializable]
    public struct LevelsSaveData
    {
        public int CompletedCount;
    }
  2. Create your own persistence manager class by extending the BasePersistenceManager<T> where T : ISaveData, new() class:

    public class PersistenceManager : BasePersistenceManager<GameSaveData>
    {
        public PersistenceManager(ISaveSource saveSource) : base(saveSource) { }
    }
  3. Ensure that all objects you want to save implement ISavable<T> where T : ISaveData:

     public class LevelsDataManager : ISavable<GameSaveData>
     {
         private int completedCount;
         
         public void Load(GameSaveData saveData)
         {
             completedCount = saveData.Levels.CompletedCount;
         }
    
         public void Save(ref GameSaveData saveData)
         {
             saveData.Levels.CompletedCount = completedCount;
         }
     }
  4. Register all objects you want to save with your PersistenceManager:

    ...
    persistenceManager.Register(levelsDataManager);
    ...

    Alternatively, if you use dependency injection (DI) in your project, you can register the necessary objects inside your PersistenceManager:

    public class PersistenceManager : BasePersistenceManager<SaveData>
     {
         public PersistenceManager(ISaveSource saveSource, IReadOnlyList<ISavable> savableObjects) : base(saveSource)
         {
             foreach (var savableObject in savableObjects)
             {
                 Register(savableObject);
             }
         }
     }

    Where LevelsDataManager implements ISavable instead of ISavable<GameSaveData>:

    public interface ISavable : ISavable<SaveData> { }
  5. Load/save your game data

    ...
    persistenceManager.Load();
    ...
    persistenceManager.Save();
  6. You can delete all save files in the editor by selecting the Edit > Pepersistence > Clear All Saves. This action will remove all files with an extension that matches the pattern ^.*sav$, including the default .jsav. Therefore, if you create your own save file format, make sure to use a file extension that matches this pattern, as it will also be removed when clearing saves.

Features

  1. Contains classes for local saves, supporting both binary and JSON formats.
  2. Extensibility: You can implement your custom save source, such as cloud-based saves, to meet your specific needs.
  3. Save file encryprion

TODO

  1. Migrations support

pepersistence's People

Contributors

aleshmandr avatar

Watchers

 avatar

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.