Unity类银河战士恶魔城学习总结(P146 Delete Save file-P147 Encryption of save data删除数据和加密数据)

【Unity教程】从0编程制作类银河恶魔城游戏_哔哩哔哩_bilibili

教程源地址:https://www.udemy.com/course/2d-rpg-alexdev/

本章节实现了快速删除存档和加密存档

以下是加密前和加密后的对比

SaveManager.cs

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;



//2024.11.25
public class SaveManager : MonoBehaviour
{
    public static SaveManager instance;

    [SerializeField] private string fileName;
    [SerializeField] private bool encryptData;

    private GameData gameData;
    private List<ISaveManager> saveManagers = new List<ISaveManager>();
    private FileDataHandler dataHandler;


    [ContextMenu("Delete save file")]
    private void DeleteSaveData()
    {
        dataHandler = new FileDataHandler(Application.persistentDataPath, fileName, encryptData);
        dataHandler.Delete();
    }

    private void Awake()
    {
        if (instance != null)
            Destroy(instance.gameObject);
        else
            instance = this;
    }

    private void Start()
    {
        dataHandler = new FileDataHandler(Application.persistentDataPath,fileName, encryptData);
        saveManagers = FindAllSaveManagers();

        LoadGame();
    }


    public void NewGame()
    {
        gameData = new GameData();
    }


    public void LoadGame()
    {
        gameData = dataHandler.Load();

        if (this.gameData == null)
        {
            Debug.Log("没有找到存档");
            NewGame();
        }

        foreach (ISaveManager saveManager in saveManagers)
        {
            saveManager.LoadData(gameData);
        }

        
    }


    public void SaveGame()
    {
        foreach(ISaveManager saveManager in saveManagers)
        {
            saveManager.SaveData(ref gameData);
        }

        dataHandler.Save(gameData);
    }


    private void OnApplicationQuit()
    {
        SaveGame();
    }


    private List<ISaveManager> FindAllSaveManagers()
    {
        IEnumerable<ISaveManager> saveManagers = FindObjectsOfType<MonoBehaviour>(true).OfType<ISaveManager>();

        return new List<ISaveManager>(saveManagers);

    }
}

FileDataHandler.cs

Save 方法

  • 功能:将 GameData 数据保存到指定路径的文件中。
  • 核心逻辑 :
    1. 创建完整路径 :通过 Path.Combine 组合文件夹路径和文件名。
    2. 确保文件夹存在 :调用 Directory.CreateDirectory 确保路径中的文件夹被创建。
    3. 序列化数据 :使用 JsonUtility.ToJson 将 GameData 对象转为 JSON 格式的字符串。
    4. 加密数据(可选) :如果 encrypData 为 true,调用 EncryptDecrypt 方法加密数据。
    5. 写入文件 :通过 FileStream 和 StreamWriter 写入数据到文件。
    6. 异常处理:捕获并报告文件操作可能抛出的异常。

Load 方法

  • 功能:从文件中读取数据并反序列化为 GameData 对象。
  • 核心逻辑 :
    1. 创建完整路径 :和 Save 方法类似,通过 Path.Combine 拼接路径。
    2. 检查文件是否存在 :通过 File.Exists 判断文件是否存在。
    3. 读取数据 :使用 FileStream 和 StreamReader 读取文件内容。
    4. 解密数据(可选) :如果 encrypData 为 true,调用 EncryptDecrypt 解密数据。
    5. 反序列化 :使用 JsonUtility.FromJson 将 JSON 字符串解析为 GameData 对象。
    6. 异常处理:捕获读取过程中的异常并报告。
cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.IO;

//2024.11.27
public class FileDataHandler
{
    private string dataDirPath = "";//数据文件夹路径
    private string dataFileName = "";//数据文件名

    private bool encrypData=false;//是否加密数据
    private string codeWord = "FuNignNa";//加密密码



    public FileDataHandler(string _dataDirPath, string _dataFileName, bool encrypData)
    {
        dataDirPath = _dataDirPath;
        dataFileName = _dataFileName;
        this.encrypData = encrypData;
        
    }

    public void Save(GameData _data)
    {
        string fullPath = Path.Combine(dataDirPath, dataFileName);


        try
        {
            Directory.CreateDirectory(Path.GetDirectoryName(fullPath));//确保路径中的文件夹被创建

            string dataToStore = JsonUtility.ToJson(_data, true);//将数据转换为json格式

            if (encrypData)
                dataToStore = EncryptDecrypt(dataToStore);//加密数据

            using (FileStream stream = new FileStream(fullPath, FileMode.Create))
            {
                using (StreamWriter writer = new StreamWriter(stream))
                {
                    writer.Write(dataToStore);
                }
            }

        }

        catch (Exception e)
        {
            Debug.LogError("保存数据错误: " + fullPath + "\n" + e);
        }
    }

    public GameData Load()
    {
        string fullPath = Path.Combine(dataDirPath, dataFileName);//找到文件路径
        GameData loadData = null;

        if (File.Exists(fullPath))
        {
            try
            {
                string dataToLoad = "";

                using (FileStream stream = new FileStream(fullPath, FileMode.Open))//打开文件
                {
                    using (StreamReader reader = new StreamReader(stream))//读取文件
                    {
                        dataToLoad = reader.ReadToEnd();//读取文件
                    }
                }

                if (encrypData)
                    dataToLoad = EncryptDecrypt(dataToLoad);//解密数据

                loadData = JsonUtility.FromJson<GameData>(dataToLoad);

            }

            catch (Exception e)
            {
                Debug.LogError("读取数据错误: " + fullPath + "\n" + e);
            }
        }


        return loadData;
    }


    public void Delete()//删除数据
    {
        string fullPath = Path.Combine(dataDirPath, dataFileName);//找到文件路径

        if (File.Exists(fullPath))//如果文件存在就删除s
            File.Delete(fullPath);

    }

    private string EncryptDecrypt(string _data)//加密解密
    {
        string modifiedData = "";//修改后的数据

        for (int i = 0; i < _data.Length; i++)
        {
            modifiedData += (char)(_data[i] ^ codeWord[i % codeWord.Length]);  
        }

        return modifiedData;
    }
}
相关推荐
鱼很腾apoc27 分钟前
【Redis】第2期 详解Redis五大数据结构:命令+C++实现、渐进式遍历与数据库管理
linux·服务器·c语言·数据结构·c++·redis·学习
楚山之石16 小时前
【Lancet】BRAID 随机试验:乳腺致密女性补充成像,abbreviated MRI 与 CEM 检出更多早期癌(首轮中期结果)
人工智能·科技·学习·mri·医学影像
Sunflower_ac20 小时前
强化学习学习笔记:Lab 0–Lab 2
笔记·学习
Frankie_stand20 小时前
无人机售后故障处理全流程:从受理、SLA 时限到责任划分与改进闭环
游戏引擎·无人机·cocos2d
Z21-X1 天前
云原生运维学习笔记:Kubernetes 自动扩缩容与资源管控实战
运维·笔记·学习·云原生·kubernetes
可涵不会debug1 天前
ChatServer 模块学习笔记:HTTP 接入层、多线程与流式响应
笔记·学习·http
凌镜icecraft1 天前
我改文件扩展名的时候那个警告弹窗再也不会弹出来了
windows·c#·wpf·explorer
console.log('npc')2 天前
Python第9课 类学习笔记,前端js语法对照
前端·python·学习
Jeff-Tsai2 天前
一个不用注册方法的 JSON-RPC 包,怎么决定哪些方法能被调用?
c#·.net·asp.net core·json-rpc
艾莉丝努力练剑2 天前
【AI大模型接入SDK】解决Unicode码转化以及界面变形问题
前端·c++·人工智能·学习·安全·架构