Unity类银河恶魔城学习记录13-5,6 p146 Delete save file,p147 Encryption of saved data源代码

embedded/2024/10/21 0:56:24/

    Alex教程每一P的教程原代码加上我自己的理解初步理解写的注释,可供学习Alex教程的人参考
此代码仅为较上一P有所改变的代码

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

FileDataHandler.cs
using System;
using System.IO;
using UnityEngine;
public class FileDataHandler
{private string dataDirPath = "";private string dataFileName = "";private bool encryptData = false;//是否加密判断private string codeWord = "sukaczev";//加密密码public FileDataHandler(string _dataDirPath, string _dataFilePath,bool _encryptData)//构造函数拿到需要保存的位置和文件名称{dataDirPath = _dataDirPath;dataFileName = _dataFilePath;encryptData = _encryptData;}public void Save(GameData _data){string fullPath = Path.Combine(dataDirPath, dataFileName);//合成路径函数 将位置和文件合并成实际的可以读取的路径try//用try防止其报错{Directory.CreateDirectory(Path.GetDirectoryName(fullPath));//通过路径创建出需要的文件,存在就不创建了string dataToStore = JsonUtility.ToJson(_data, true);//将传过来的gameData转换成文本形式并且使其可读if (encryptData)dataToStore = EncryptDecrypt(dataToStore);using (FileStream stream = new FileStream(fullPath, FileMode.Create))//两个using 第一个进入文件使其变为可编写模式{using (StreamWriter writer = new StreamWriter(stream))//第二个拿到文件对其进行编辑{writer.Write(dataToStore);//写入函数}}}catch (Exception e){Debug.LogError("Error on trying to save data to file " + 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(encryptData){dataToLoad = EncryptDecrypt(dataToLoad);}loadData = JsonUtility.FromJson<GameData>(dataToLoad);//转换为游戏需要的类型}catch (Exception e){Debug.LogError(e);}}return loadData;}public void Delete()//删除对应文件函数{string fullPath = Path.Combine(dataDirPath, dataFileName);if(File.Exists(fullPath))//存在才能删除 File操作{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;}
}
GameData.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;public class SaveManager : MonoBehaviour
{public static SaveManager instance;[SerializeField] private string fileName;[SerializeField] private bool encryptData;GameData gameData;private List<ISaveManager> saveManagers;private FileDataHandler dataHandler;[ContextMenu("Delete save file")]//把一个方法在Unity里面显示出来的做法private void DeleteSaveData()//调用并被公开的删除文件函数{dataHandler = new FileDataHandler(Application.persistentDataPath, fileName, encryptData);dataHandler.Delete();}private void Awake(){if (instance != null)Destroy(instance);elseinstance = 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("No data");NewGame();}foreach(ISaveManager saveManager in saveManagers)//循环调用所有的找到脚本的LoadData和SaveData到,这样便可以将所有的数据汇聚到gameData中,并从中拿到data{saveManager.LoadData(gameData);}Debug.Log("Loaded currency " + gameData.currency);}public void SaveGame()循环调用所有的找到脚本的LoadData和SaveData到,这样便可以将所有的数据汇聚到gameData中,并从中拿到data{foreach(ISaveManager saveManager in saveManagers){saveManager.SaveData(ref gameData);}dataHandler.Save(gameData);}private void OnApplicationQuit(){SaveGame();}private List<ISaveManager> FindAllSaveManagers()//全局寻找带ISave的脚本的函数{IEnumerable<ISaveManager> saveManager = FindObjectsOfType<MonoBehaviour>().OfType<ISaveManager>();return new List<ISaveManager>(saveManager);}
}


http://www.ppmy.cn/embedded/6421.html

相关文章

【算法】二分查找

快乐的流畅&#xff1a;个人主页 个人专栏&#xff1a;《算法神殿》《数据结构世界》《进击的C》 远方有一堆篝火&#xff0c;在为久候之人燃烧&#xff01; 文章目录 引言一、二分查找二、查找元素的第一个和最后一个位置三、x的平方根四、搜索插入位置五、山脉数组的峰顶索引…

TensorFlow 的基本概念和使用场景

TensorFlow是一个开源机器学习框架&#xff0c;由Google开发。它通过使用数据流图来表示计算任务&#xff0c;并使用张量&#xff08;Tensor&#xff09;来表示数据&#xff0c;从而实现了高效的计算。 TensorFlow的基本概念包括以下几点&#xff1a; 1. 张量&#xff08;Ten…

Python模块之logging

官方文档 常见用法 logging模块是Python标准库中用于记录日志的模块。它提供了灵活且可配置的日志记录功能&#xff0c;可以用于在应用程序中捕获和输出各种级别的日志消息。以下是logging模块的常见用法示例&#xff1a; python import logging# 配置日志记录器 logging.b…

设计模式学习笔记 - 开源实战一(上):通过剖析JDK源码学习灵活应用设计模式

工厂模式在 Calendar 类中的应用 在前面讲到工厂模式的时候&#xff0c;大部分工厂类都是以 Factory 作为后缀来命名&#xff0c;并且工厂类主要负责创建对象这样一件事情。但在实际的项目开发中&#xff0c;工厂类的设计更加灵活。我们来看下&#xff0c;工厂模式在 Java JDK…

继东风一汽通信后,天磊咨询再次与东风集团达成深度业务合作

&#xff08;天磊咨询总经理&#xff1a;刘文喜&#xff09; 在风起云涌的市场激战中&#xff0c;天磊咨询凭借其出类拔萃的专业实力与服务品质&#xff0c;犹如一颗璀璨明星般脱颖而出&#xff0c;成功与赫赫有名的东风集团达成业务合作。这一合作的达成&#xff0c;不单彰显…

React + 项目(从基础到实战) -- 第八期

ajax 请求的搭建 引入mockAP接口设计AJAX 通讯 前置知识 HTTP 协议 , 前后端通讯的桥梁API : XMLHttpRequest 和 fetch常用工具axios mock 引入 Mock.js (mockjs.com) 使用 mockJS 前端代码中引入 mockJs定义要模拟的路由 , 返回结果mockJs 劫持ajax请求(返回模拟的结果)…

linux设备树-of_parse_phandle_with_args

1.设备树实例 interrupt-controller1 { compatible "vendor,gic"; #interrupt-cells <2>; interrupt-controller; reg <0x01 0x1000>; }; deviceA { compatible "vendor,device-a"; reg <0x02 0x100>; interrupts <&interr…

Spark---RDD的创建分类和基础操作算子详解

一、RDD的创建 原生api提供了两种创建方式&#xff0c;一种就是读取文件textFile&#xff0c;还有一种就是加载一个scala集合parallelize。当然&#xff0c;也可以通过transformation算子来创建的RDD。 //创建RDD//加载数据&#xff0c;textFile&#xff08;参数1&#xff0c;…