当前位置: 首页 > ds >正文

Unity3D仿星露谷物语开发57之保存库存信息到文件

1、目标

保存下面库存栏中信息到文件中。

2、修改SceneSave.cs脚本

添加2行代码:

3、修改InventoryManager对象

添加Generate GUID组件。

4、修改InventoryManager.cs脚本

添加继承自ISaveable

添加属性信息:

    private string _iSaveableUniqueID;public string ISaveableUniqueID { get { return _iSaveableUniqueID; } set { _iSaveableUniqueID = value; } }private GameObjectSave _gameObjectSave;public GameObjectSave GameObjectSave { get { return _gameObjectSave; } set { _gameObjectSave = value; } }private UIInventoryBar inventoryBar;

在Awake()方法中添加2行代码:

添加如下几个方法:

 private void OnDisable(){ISaveableDeregister();}private void OnEnable(){ISaveableRegister();}private void Start()
{inventoryBar = FindObjectOfType<UIInventoryBar>();
}public void ISaveableRegister(){SaveLoadManager.Instance.iSaveableObjectList.Add(this);}public void ISaveableDeregister(){SaveLoadManager.Instance.iSaveableObjectList.Remove(this);}public GameObjectSave ISaveableSave(){// Create new scene saveSceneSave sceneSave = new SceneSave();// Remove any existing scene save for persistent scene for this gameobjectGameObjectSave.sceneData.Remove(Settings.PersistentScene);// Add inventory lists array to persistent scene savesceneSave.listInvItemArray = inventoryLists;// Add inventory list capacity array to persistent scene savesceneSave.intArrayDictionary = new Dictionary<string, int[]>();sceneSave.intArrayDictionary.Add("inventoryListCapacityArray", inventoryListCapacityIntArray);// Add scene save for gameobjectGameObjectSave.sceneData.Add(Settings.PersistentScene, sceneSave);return GameObjectSave;}public void ISaveableLoad(GameSave gameSave){if(gameSave.gameObjectData.TryGetValue(ISaveableUniqueID, out GameObjectSave gameObjectSave)){GameObjectSave = gameObjectSave;// Need to find inventory lists - start by trying to locate saveScene for game objectif(gameObjectSave.sceneData.TryGetValue(Settings.PersistentScene, out SceneSave sceneSave)){// list inv items array exists for persistent sceneif(sceneSave.listInvItemArray != null){inventoryLists = sceneSave.listInvItemArray;// Send events that inventory has been updatedfor(int i = 0; i < (int)InventoryLocation.count; i++){EventHandler.CallInventoryUpdatedEvent((InventoryLocation)i, inventoryLists[i]);}// Clear any items player was carryingPlayer.Instance.ClearCarriedItem();// Clear any highlights on inventory barinventoryBar.ClearHighlightOnInventorySlots();}// int array dictionary exists for sceneif(sceneSave.intArrayDictionary != null && sceneSave.intArrayDictionary.TryGetValue("inventoryListCapacityArray", out int[] inventoryCapacityArray)){inventoryListCapacityIntArray = inventoryCapacityArray;}}}}public void ISaveableStoreScene(string sceneName){// Nothing required here since the inventory manager is on a persistent scene;}public void ISaveableRestoreScene(string sceneName){// Nothing required here since the inventory manager is on a persistent scene;}

5、运行游戏

角色收集的item放到库存栏中,save game后退出游戏,重新进入游戏后load game,库存栏中的信息保持不变。

http://www.xdnf.cn/news/10393.html

相关文章:

  • ROS2与Unitree机器人集成指南
  • Linux 基础IO(上)
  • javaweb-maven以及http协议
  • (LeetCode 每日一题) 909. 蛇梯棋 (广度优先搜索bfs)
  • 电子电器架构 --- OTA测试用例分析(上)
  • 华为OD机试_2025 B卷_小明减肥(Python,100分)(附详细解题思路)
  • 最卸载器——Geek Uninstaller 使用指南
  • 设备健康管理的战略升维:用预测性维护重构企业竞争力
  • JDK21深度解密 Day 9:响应式编程模型重构
  • 性能优化 - 理论篇:CPU、内存、I/O诊断手段
  • 性能优化 - 理论篇:常见指标及切入点
  • 钉钉红包性能优化之路
  • Git入门到精通:30分钟掌握核心技巧
  • 第二章支线三 ·《CSS炼金术:动画与变换高级奥义》
  • C++文件和流基础
  • [蓝桥杯]春晚魔术【算法赛】
  • Socket编程之TCP套件字
  • 深 入 剖 析 单 链 表:从 原 理 到 实 战 应 用
  • day17 常见聚类算法
  • Linux 库制作与原理
  • DockerFile常用关键字指令
  • 用Slash将链接转为快捷方式
  • 深入理解交叉熵损失函数——全面推演各种形式
  • 学习STC51单片机22(芯片为STC89C52RCRC)
  • Python训练打卡Day38
  • CTFHub-RCE 命令注入-过滤运算符
  • 【Java开发日记】基于 Spring Cloud 的微服务架构分析
  • 训练中常见的运动强度分类
  • 多目标粒子群优化算法(MOPSO),用于解决无人机三维路径规划问题,Matlab代码实现
  • 用 Spring Boot 静态资源映射 vs 用 Nginx 提供静态文件服务总结