Unity3D仿星露谷物語開發56之保存角色位置到文件

1、目標

游戲中通過Save Game保存角色位置,當重啟游戲后,通過Load Game可以恢復角色的位置。

2、Player對象操作

(1)組件添加

給Hierarchy下的Player組件添加Generate GUID組件。

(2)修改SceneSave.cs腳本

添加2行代碼:

using System.Collections.Generic;[System.Serializable]public class SceneSave
{public Dictionary<string, bool> boolDictionary; // string key is an identifier name we choose for this listpublic Dictionary<string, string> stringDictionary; public Dictionary<string, Vector3Serializable> vector3Dictionary; // 可以保存角色位置// string key is an identifier name we choose for this listpublic List<SceneItem> listSceneItem;public Dictionary<string, GridPropertyDetails> gridPropertyDetailsDictionary; // key是坐標信息,value是地面屬性信息
}

(3)修改Settings.cs腳本

添加PersistentScene的常量,因為Player是在該Scene下。

添加1行代碼如下:

(4)修改Player.cs腳本

添加以下代碼:

using System;
using UnityEngine.SceneManagement;

修改Player類繼承自ISaveable:

接下來就是實現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; } }

在Awake方法中添加屬性的初始化:

在OnDisable和OnEnable中各添加一個步驟:

添加這7個方法的實現:

public void ISaveableRegister()
{SaveLoadManager.Instance.iSaveableObjectList.Add(this);
}public void ISaveableDeregister()
{SaveLoadManager.Instance.iSaveableObjectList.Remove(this);
}public GameObjectSave ISaveableSave()
{// Delete saveScene for game object if it already existsGameObjectSave.sceneData.Remove(Settings.PersistentScene);// Create saveSave for game objectSceneSave sceneSave = new SceneSave();// Create Vector3 DictionarysceneSave.vector3Dictionary = new Dictionary<string, Vector3Serializable>();// Create String DictionarysceneSave.stringDictionary = new Dictionary<string, string>();  // Add Player position to Vector3 dictionaryVector3Serializable vector3Serializable = new Vector3Serializable(transform.position.x, transform.position.y, transform.position.z);sceneSave.vector3Dictionary.Add("playerPosition", vector3Serializable);// Add Current Scene Name to string dictionarysceneSave.stringDictionary.Add("currentScene", SceneManager.GetActiveScene().name);// Add Player Direction to string dictionarysceneSave.stringDictionary.Add("playerDirection", playerDirection.ToString());// Add sceneSave data for player game objectGameObjectSave.sceneData.Add(Settings.PersistentScene, sceneSave);return GameObjectSave;
}public void ISaveableLoad(GameSave gameSave)
{if(gameSave.gameObjectData.TryGetValue(ISaveableUniqueID, out GameObjectSave gameObjectSave)){// Get save data dictionary for sceneif(gameObjectSave.sceneData.TryGetValue(Settings.PersistentScene, out SceneSave sceneSave)){// Get player positionif(sceneSave.vector3Dictionary != null && sceneSave.vector3Dictionary.TryGetValue("playerPosition", out Vector3Serializable playerPosition)){transform.position = new Vector3(playerPosition.x, playerPosition.y, playerPosition.z);}// Get string dictionaryif(sceneSave.stringDictionary != null){// Get player sceneif(sceneSave.stringDictionary.TryGetValue("currentScene", out string currentScene)){SceneControllerManager.Instance.FadeAndLoadScene(currentScene, transform.position);}// Get player directionif(sceneSave.stringDictionary.TryGetValue("playerDirection", out string playerDir)){bool playerDirFound = Enum.TryParse<Direction>(playerDir, true, out Direction direction);if (playerDirFound){playerDirection = direction;SetPlayerDirection(playerDirection);}}}}}
}public void ISaveableStoreScene(string sceneName)
{// Nothing required here since the player is on a persistent scene;
}public void ISaveableRestoreScene(string sceneName)
{// Nothing required here since the player is on a persistent scene;
}private void SetPlayerDirection(Direction playerDirection)
{switch(playerDirection){case Direction.up:// set idle up triggerEventHandler.CallMovementEvent(0f, 0f, false, false, false, false, ToolEffect.none, false, false, false, false, false, false, false, false,false, false, false, false, false, false, false, false, true, false, false, false);break;case Direction.down:// set idle down triggerEventHandler.CallMovementEvent(0f, 0f, false, false, false, false, ToolEffect.none, false, false, false, false, false, false, false, false,false, false, false, false, false, false, false, false, false, true, false, false);break;case Direction.left:EventHandler.CallMovementEvent(0f, 0f, false, false, false, false, ToolEffect.none, false, false, false, false, false, false, false, false,false, false, false, false, false, false, false, false, false, false, true, false);break;case Direction.right:EventHandler.CallMovementEvent(0f, 0f, false, false, false, false, ToolEffect.none, false, false, false, false, false, false, false, false,false, false, false, false, false, false, false, false, false, false, false, true);break;default:// set idle down triggerEventHandler.CallMovementEvent(0f, 0f, false, false, false, false, ToolEffect.none, false, false, false, false, false, false, false, false,false, false, false, false, false, false, false, false, false, true, false, false);break;}
}

3、運行游戲

角色種植了Crop,然后進入另一個Scene后,選擇Save Game。

重啟游戲后,選擇 Load Game,此時:

1)角色在另一個Scene中,并且direction保持一直

2)之前種植的Crop狀態未變化

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/bicheng/83136.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/83136.shtml
英文地址,請注明出處:http://en.pswp.cn/bicheng/83136.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

TKernel模塊--雜項

TKernel模塊–雜項 1.DEFINE_HARRAY1 #define DEFINE_HARRAY1(HClassName, _Array1Type_) \ class HClassName : public _Array1Type_, public Standard_Transient { \public: …

c++ typeid運算符

typeid運算符能獲取類型信息。獲取到的是type_info對象。type_info類型如下&#xff1a; 可以看到&#xff0c;這個類刪除了拷貝構造函數以及等號操作符。有一些成員函數&#xff1a;hash_code、before、name、raw_name, 還重載了和!運算符。 測試&#xff1a; void testTyp…

第304個Vulnhub靶場演練攻略:digital world.local:FALL

digital world.local&#xff1a;FALL Vulnhub 演練 FALL (digitalworld.local: FALL) 是 Donavan 為 Vulnhub 打造的一款中型機器。這款實驗室非常適合經驗豐富的 CTF 玩家&#xff0c;他們希望在這類環境中檢驗自己的技能。那么&#xff0c;讓我們開始吧&#xff0c;看看如何…

【數據庫】數據庫恢復技術

數據庫恢復技術 實現恢復的核心是使用冗余&#xff0c;也就是根據冗余數據重建不正確數據。 事務 事務是一個數據庫操作序列&#xff0c;是一個不可分割的工作單位&#xff0c;是恢復和并發的基本單位。 在關系數據庫中&#xff0c;一個事務是一條或多條SQL語句&#xff0c…

switch-case判斷

switch-case判斷 #include <stdio.h> int main() {int type;printf("請輸入你的選擇&#xff1a;\n");scanf("%d",&type);getchar();switch (type){case 1:printf("你好&#xff01;");break;case 2:printf("早上好&#xff01;…

從監控到告警:Prometheus+Grafana+Alertmanager+告警通知服務全鏈路落地實踐

文章目錄 一、引言1.1 監控告警的必要性1.2 監控告警的基本原理1.2.1 指標采集與存儲1.2.2 告警規則與觸發機制1.2.3 多渠道通知與閉環 二、技術選型與架構設計2.1 為什么選擇 Prometheus 及其生態2.1.1 Prometheus 優勢分析2.1.2 Grafana 可視化能力2.1.3 Alertmanager 靈活告…

STM32 UART通信實戰指南:從原理到項目落地

STM32串口通信實戰指南&#xff1a;從零開始手把手教你 前言&#xff1a;為什么串口這么重要&#xff1f; 在嵌入式開發中&#xff0c;串口就像設備的"嘴巴"和"耳朵"。無論是給單片機下達指令、讀取傳感器數據&#xff0c;還是讓兩個模塊"對話"…

Jmeter requests

1.Jemter元件和組件 1.1 元件和組件的概念 元件&#xff1a;多個功能相似的的組件的容器&#xff0c;類似于一個工具箱。 組件&#xff1a;實現某個特定功能的實例&#xff0c;類似于工具箱中的螺絲刀&#xff0c;十字扳手... 1.2 作用域和執行順序 1.2.1 作用域 例子&#…

計算機視覺---GT(ground truth)

在計算機視覺&#xff08;Computer Vision, CV&#xff09;領域&#xff0c;Ground Truth&#xff08;GT&#xff0c;中文常譯為“真值”或“ ground truth”&#xff09; 是指關于數據的真實標簽或客觀事實&#xff0c;是模型訓練、評估和驗證的基準。它是連接算法與現實世界的…

1-Wire 一線式總線:從原理到實戰,玩轉 DS18B20 溫度采集

引言 在嵌入式系統中&#xff0c;通信總線是連接 CPU 與外設的橋梁。從 I2C、SPI 到 UART&#xff0c;每種總線都有其獨特的應用場景。而本文要介紹的1-Wire 一線式總線&#xff0c;以其極簡的硬件設計和獨特的通信協議&#xff0c;在溫度采集、身份識別等領域大放異彩。本文將…

基于開源AI大模型AI智能名片S2B2C商城小程序源碼的銷售環節數字化實現路徑研究

摘要&#xff1a;在數字化浪潮下&#xff0c;企業銷售環節的轉型升級已成為提升競爭力的核心命題。本文基于清華大學全球產業研究院《中國企業數字化轉型研究報告&#xff08;2020&#xff09;》提出的“提升銷售率與利潤率、打通客戶數據、強化營銷協同、構建全景用戶畫像、助…

Linux淺談

Linux淺談 一、什么是 Linux&#xff1f;先拋開 “內核”&#xff0c;看整體 可以把 Linux 系統 想象成一臺 “組裝電腦”&#xff1a; 最核心的零件是 “主板”—— 這就是 Linux 內核&#xff08;Kernel&#xff09;&#xff0c;負責管理電腦里的所有硬件&#xff08;比如 …

PostgreSQL ERROR: out of shared memory處理

使用pg_dump命令導出一個庫的時候&#xff0c;報 pg_dump: error: query failed: ERROR: out of shared memory HINT: You might need to increase "max_locks_per_transaction". 從錯誤字面上看是超出內存大小了&#xff0c;建議增加max_locks_per_transaction參…

IoT/基于NB28-A/BC28-CNV通信模組使用AT指令連接華為云IoTDA平臺(HCIP-IoT實驗2)

文章目錄 概述檢查通信環境通信模組固件信號強度CGATT指令參數 / 啥是PS域&#xff1f;PS附著狀態&#xff1a;ATCGATTPLMN 選擇&#xff1a;ATCOPSCEREG指令參數 / 啥是EPS與EPC?CEREG指令參數 / 啥是URC?網絡注冊狀態&#xff1a;ATCEREG網絡附著和網絡注冊 AT指令接入IoTD…

紅外遙控(外部中斷)

目錄 1.紅外遙控簡介 通信方式&#xff1a; 紅外LED波長&#xff1a; 通信協議標準&#xff1a; 2.硬件電路 發送部分1&#xff1a; 內部元件介紹&#xff1a; 工作原理&#xff1a; 為什么要以38KHZ亮滅&#xff1f; 電路圖&#xff1a; 發送部分2&#xff1a; 電…

【C#】一個簡單的http服務器項目開發過程詳解

這跟安裝NoteJs程序運行腳本文件搭建一個簡單Http服務器一樣&#xff0c;相比起來&#xff0c;它的優點是可以開發的應用是免安裝&#xff0c;跨平臺的&#xff0c;放在移動盤上便捷的&#xff0c;這里著重講http服務器實現的過程&#xff0c;以便自主實現特定的功能和服務。 …

WPF【11_4】WPF實戰-重構與美化(MVVM 架構)

11-9 【理論】MVVM 架構 在 WPF 項目中&#xff0c;我們主要采用的是一種類似 MVC 的架構&#xff0c;叫做 MVVM。 MVVM 繼承了 MVC 的理念&#xff0c;是 Model-View-ViewModel 的縮寫&#xff0c;中文意思是模型、視圖、視圖模型。這三個詞分開看我們都能看懂&#xff0c;不…

使用PowerBI個人網關定時刷新數據

使用PowerBI個人網關定時刷新數據 PowerBI desktop連接mysql&#xff0c;可以設置定時刷新數據或在PowerBI服務中手動刷新數據,步驟如下&#xff1a; 第一步&#xff1a; 下載網關。以個人網關為例&#xff0c;如圖 第二步&#xff1a; 雙擊網關&#xff0c;點擊下一步&…

深度學習驅動的超高清圖修復技術——綜述

Deep Learning-Driven Ultra-High-Definition Image Restoration: A Survey Liyan Wang, Weixiang Zhou, Cong Wang, Kin-Man Lam, Zhixun Su, Jinshan Pan Abstract Ultra-high-definition (UHD) image restoration?? aims to specifically solve the problem of ??quali…

3 分鐘學會使用 Puppeteer 將 HTML 轉 PDF

需求背景 1、網頁存檔與文檔管理 需要將網頁內容長期保存或歸檔為PDF,確保內容不被篡改或丟失,適用于法律文檔、合同、技術文檔等場景。PDF格式便于存儲和檢索。 2、電子報告生成 動態生成的HTML內容(如數據分析報告、儀表盤)需導出為PDF供下載或打印。PDF保留排版和樣…