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

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.dtcms.com/a/224392.html

相关文章:

  • 精简多功能办公软件
  • CodeTop100 Day18
  • 用 Pandera 高效验证和清洗 Pandas 数据集——实用分步指南
  • offset三大家族
  • stm32默认复位刚开始由hsi作为主时钟源而后来才换成的pll
  • 硬件开发全解:从入门教程到实战案例与丰富项目资源
  • 【深度学习】14. DL在CV中的应用章:目标检测: R-CNN, Fast R-CNN, Faster R-CNN, MASK R-CNN
  • Python实现P-PSO优化算法优化循环神经网络LSTM分类模型项目实战
  • day23-计算机网络-1
  • Deepin 23.10安装Docker
  • Rk3568驱动开发_GPIO点亮LED_12
  • 生成https 证书步骤
  • Express中使用MySQL数据库的完整示例
  • VS Code / Cursor 将默认终端设置为 CMD 完整指南
  • C++:指针(Pointers)
  • 深入理解CSS常规流布局
  • SQL Server 事务详解:概念、特性、隔离级别与实践
  • 关于5090安装tensorrt(python api)的过程
  • reverse_ssh 建立反向 SSH 连接指南 混淆AV [好东西哟]
  • 【鱼皮-用户中心】笔记
  • CppCon 2014 学习:Modern Template Metaprogramming A Compendium
  • OpenLayers 图形交互编辑
  • Leetcode 1908. Nim 游戏 II
  • 数的划分--dfs+剪枝
  • 第3节 Node.js 创建第一个应用
  • vscode不满足先决条件问题的解决——vscode的老版本安装与禁止更新(附安装包)
  • 线程(上)【Linux操作系统】
  • 多类别分类中的宏平均和加权平均
  • Centos系统搭建主备DNS服务
  • pytest中的元类思想与实战应用