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

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

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}");
        }
    }
}
Last modification:March 9, 2024
If you think my article is useful to you, please feel free to appreciate