【Unity实用插件功能】SaveGameFree存档系统

 【1】什么是SaveGameFree?

它是一种可以通过JSON、XML和二进制格式来进行游戏数据储存的便捷工具,并且你可以通过使用加密算法来保证这些数据的安全性,以防作弊或者黑客行为。

插件地址:Save Game Free - Gold Update | 输入管理 | Unity Asset Store

特征:

● 跨平台:支持所有平台

● 网页和云端:保存数据到你的服务器上,也可以从服务器读取出来

● 加密:加密:使用最安全的算法进行加密‎

●‎ 自动保存:自动保存游戏对象数据‎

● 开源

【2】基础保存和加载

该插件提供了简单而一致的API,可让您轻松保存和加载数据。‎

保存一个 integer 数据类型:

SaveGame.Save<int>("score", score);

加载一个integer 数据类型:

int score = SaveGame.Load<int>("score");

‎以下是“免费保存游戏”的一个简单示例,用于保存和加载数据:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
using BayatGames.SaveGameFree;
 
public class SimpleUsage : MonoBehaviour {
 
    public int score;
 
    void Start () {
 
        // Saving the data
        SaveGame.Save<int> ( "score", score );
    }
 
}

‎如您所见,我们可以使用 SaveGame API 轻松保存和加载游戏数据,现在让我们保存一个简单的字符串值:‎

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
using BayatGames.SaveGameFree;
 
public class SimpleUsage : MonoBehaviour {
 
    public string username;
 
    void Start () {
 
        // Saving the data
        SaveGame.Save<string> ( "username", username );
    }
 
}

‎保存一个数据集合:‎

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
using BayatGames.SaveGameFree;
 
public class SimpleUsage : MonoBehaviour {
 
    void Start () {
        Dictionary<string, int> playerScores = new Dictionary<string, int> ();
        playerScores.Add ( "John", 100 );
        playerScores.Add ( "Jack", 200 );
 
        // Saving the data
        SaveGame.Save<Dictionary<string, int>> ( "playerScores", playerScores );
    }
 
}

保存一个自定义数据类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
using BayatGames.SaveGameFree;
 
public class SimpleUsage : MonoBehaviour {
 
    public class PlayerData {
        public int score;
        public string name;
    }
 
    void Start () {
        PlayerData data = new PlayerData ();
        data.score = 453;
        data.name = "John";
 
        // Saving the data
        SaveGame.Save<PlayerData> ( "playerData", data );
    }
 
}

【3】脚本API

脚本API示例
API 示例

SaveGame.Save

保存简单数据 

SaveGame.Save<string> ( "simple.txt", "Simple Data" );

保存数组 

string[] names = new string[] { "John", "James", "Kyle" };
SaveGame.Save<string[]> ( "names.txt", names );

SaveGame.Load

加载简单数据

// Loading by Generic
string simple = SaveGame.Load<string> ( "simple.txt", "The Default Value" );
 
// Loading by Type
string simple = ( string )SaveGame.Load ( "simple.txt", typeof ( string ), "The Default Value" );

加载数组

// Loading by Generic
string[] names = SaveGame.Load<string[]> ( "names.txt" );
 
// Loading by Type
string[] names = ( string[] )SaveGame.Load ( "names.txt", typeof ( string[] ) );

SaveGame.Delete

SaveGame.Delete ( "simple.txt" );

SaveGame.Exists

Simple Checking

bool exists = SaveGame.Exists ( "simple.txt" );

SaveGame.Clear

SaveGame.Clear ();

SaveGame.GetFiles

从主目录中检索文件

FileInfo[] files = SaveGame.GetFiles ();

从目录中检索文件

FileInfo[] files = SaveGame.GetFiles ( "myFolder" );

猜你喜欢

转载自blog.csdn.net/m0_70379630/article/details/125826608