Unity类银河恶魔城学习记录13-1 p142 Save system源代码

embedded/2024/9/22 14:34:07/

   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 = "";public FileDataHandler(string _dataDirPath, string _dataFilePath)//构造函数拿到需要保存的位置和文件名称{dataDirPath = _dataDirPath;dataFileName = _dataFilePath;}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转换成文本形式并且使其可读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();}}loadData = JsonUtility.FromJson<GameData>(dataToLoad);//转换为游戏需要的类型}catch (Exception e){Debug.LogError(e);}}return loadData;}
}

ISaveManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public interface ISaveManager
{void LoadData(GameData _data);void SaveData(ref GameData _data);
}

SaveManager.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;GameData gameData;private List<ISaveManager> saveManagers;private FileDataHandler dataHandler;private void Awake(){if (instance != null)Destroy(instance);elseinstance = this;}private void Start(){dataHandler = new FileDataHandler(Application.persistentDataPath, fileName);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);}
}

GameData.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class GameData
{public int currency;public GameData(){this.currency = 0;}
}
PlayerManager.cs
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;public class PlayerManager : MonoBehaviour, ISaveManager
{public static PlayerManager instance;public Player player;//这是通过在外部设置了一个组件,让这个组件能够直接把Player找到,从而减少FInd的方式所带来的高负载public int currency;private void Awake(){if(instance != null){Destroy(instance.gameObject);}elseinstance = this;}public bool HaveEnoughMoney(int _price){if(_price > currency){Debug.Log("Not enough money");return false;}currency -= _price;return true;}public int GetCurrency() => currency;public void LoadData(GameData _data){currency = _data.currency;}public void SaveData(ref GameData _data){_data.currency = this.currency;}
}


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

相关文章

图像哈希:Global+Local

文章信息 作者&#xff1a;梁小平&#xff0c;唐振军期刊&#xff1a;ACM Trans. Multimedia Comput. Commun. Appl&#xff08;三区&#xff09;题目&#xff1a;Robust Hashing via Global and Local Invariant Features for Image Copy Detection 目的、实验步骤及结论 目…

CSS中的层叠上下文

HTML 文档中的三维概念 平时我们从设备终端看到的 HTML 文档都是一个平面的&#xff0c;事实上 HTML 文档中的元素却是存在于三个维度中。除了大家熟悉的平面画布中的 x 轴和 y 轴&#xff0c;还有控制第三维度的 z 轴。 其中 x 轴通常用来表示水平位置&#xff0c;y 轴来表示…

短视频解析接口分发系统

宝塔面板&#xff1a;Nginx系统 php7.2 Mysql 5.6-5.7 伪静态Thinkphp 上传文件直接访问域名安装即可 源码免费下载地址抄笔记 (chaobiji.cn)https://chaobiji.cn/

【Yolov系列】Yolov5学习(一)补充1.2:自适应锚框计算详解+代码注释

一、自适应锚框计算详解 自适应锚框计算的具体过程&#xff1a; ①获取数据集中所有目标的宽和高。 ②将每张图片中按照等比例缩放的方式到 resize 指定大小&#xff0c;这里保证宽高中的最大值符合指定大小。 ③将 bboxes 从相对坐标改成绝对坐标&#xff0c;这里…

SpringBoot 动态加载 Jar 包:实现灵活的动态配置方案

引言 简介动态加载 Jar 包的需求背景 在当今快速发展的软件开发环境中&#xff0c;灵活性和可扩展性已成为了开发者们日常工作的关键词。特别是在微服务架构、云计算和DevOps等现代技术背景下&#xff0c;软件的配置和功能经常需要进行动态更新和修改。这就带来了对动态加载J…

Qt QStyle详解

1.简介 QStyle类是 Qt 框架中用于控制应用程序界面元素外观的一个抽象基类。这个类提供了一种方式来定制窗口部件&#xff08;widgets&#xff09;的绘制和行为&#xff0c;可以通过改变主题或风格来更改应用程序的外观&#xff0c;而无需修改窗口部件本身的代码。 Qt包含一组…

拖尾渲染器-Unity拖尾渲染器的使用

Unity拖尾渲染器是一种特效组件&#xff0c;用于在游戏中创建拖尾效果。它可以用于模拟物体的运动轨迹、增加动感和视觉效果。以下是Unity拖尾渲染器的使用方法&#xff1a; 添加拖尾渲染器组件&#xff1a;在Unity编辑器中&#xff0c;选中需要添加拖尾效果的游戏对象&#xf…

操作系统(Operating System)知识点复习——第八章 虚拟内存

目录 0.前言 1.硬件和控制结构 1.1 局部性原理Locality 1.2 分页Paging 1.2.1 多级页表Multi-level Paging System 1.2.2 反向页表/倒排页表Inverted Page Table 1.2.3 快表Translation Lookaside Buffer(TLB) 1.2.4 页尺寸 1.3 分段Segment 1.4 段页混合式Combined …