方法来自百度 这里只是当作笔记记录

用于Unity游戏存档加密 C#语言

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System.IO;
using UnityEngine;
 
/// 
/// 
/// * Writer:June
/// 
/// * Data:2021.11.9
/// 
/// * Function:序列化数据测试类
/// 
/// * Remarks:
/// 
/// 
 
 
public class SerializeDataTest : MonoBehaviour
{
    private string filePath;
 
    private void Start() => filePath = Path.Combine(Application.streamingAssetsPath, "Data.txt");
    
 
    private void Update()
    {
        //保存
        if (Input.GetKeyDown(KeyCode.S))
        {
            //实例化玩家
            Player player = new Player()
            {
                playerName = "June",
                attack = 20,
                armor = 50
            };
            SerializeDataManager.SaveData(filePath, player);
        }
        //加载
        if (Input.GetKeyDown(KeyCode.L))
        {
            Player player = SerializeDataManager.LoadData(filePath);
            Debug.Log($"玩家名字:{player.playerName}   攻击力:{player.attack}   护甲:{player.armor}");
        }
    }
}