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

Unity3D仿星露谷物语开发66之NPC存档

1、目标

保存NPC的位置信息。

2、修改NPCMovement.cs脚本

添加一个新方法来取消NPC移动,如果有NPC正在移动则取消之。

添加函数:

    public void CancelNPCMovement(){npcPath.ClearPath();npcNextGridPosition = Vector3Int.zero;npcNextWorldPosition = Vector3.zero;npcIsMoving = false;if(moveToGridPositionRoutine != null){StopCoroutine(moveToGridPositionRoutine);}// Reset move animationResetMoveAnimation();// Clear event animationClearNPCEventAnimation();npcTargetAniamtionClip = null;// Reset idle animationResetIdleAnimation();// Set idle animationSetIdleAnimation();}

3、重写NPC.cs脚本

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;[RequireComponent(typeof(NPCMovement))]
[RequireComponent(typeof(GenerateGUID))]
public class NPC : MonoBehaviour, 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 NPCMovement npcMovement;private void OnEnable(){ISaveableRegister();}private void OnDisable(){ISaveableDeregister();}private void Awake(){ISaveableUniqueID = GetComponent<GenerateGUID>().GUID;GameObjectSave = new GameObjectSave();}private void Start(){// get npc movement componentnpcMovement = GetComponent<NPCMovement>();  }public void ISaveableDeregister(){SaveLoadManager.Instance.iSaveableObjectList.Remove(this);}public void ISaveableRegister(){SaveLoadManager.Instance.iSaveableObjectList.Add(this);}public void ISaveableRestoreScene(string sceneName){// Nothing required here since on persistent scene}public void ISaveableStoreScene(string sceneName){// Nothing required here since on persistent scene}public GameObjectSave ISaveableSave(){// Remove current scene saveGameObjectSave.sceneData.Remove(Settings.PersistentScene);// Create scene saveSceneSave sceneSave = new SceneSave();// Create vector 3 serialisable dictonarysceneSave.vector3Dictionary = new Dictionary<string, Vector3Serializable>();// Create string dictionarysceneSave.stringDictionary = new Dictionary<string, string>(); // Store target grid position, target world position, and target scenesceneSave.vector3Dictionary.Add("npcTargetGridPosition", new Vector3Serializable(npcMovement.npcTargetGridPosition.x, npcMovement.npcTargetGridPosition.y,npcMovement.npcTargetGridPosition.z));sceneSave.vector3Dictionary.Add("npcTargetWorldPosition", new Vector3Serializable(npcMovement.npcTargetWorldPosition.x, npcMovement.npcTargetWorldPosition.y,npcMovement.npcTargetWorldPosition.z));sceneSave.stringDictionary.Add("npcTargetScene", npcMovement.npcTargetScene.ToString());// Add scene save to game objectGameObjectSave.sceneData.Add(Settings.PersistentScene, sceneSave);return GameObjectSave;}public void ISaveableLoad(GameSave gameSave){// Get game object sceneif(gameSave.gameObjectData.TryGetValue(ISaveableUniqueID, out GameObjectSave gameObjectSave)){GameObjectSave = gameObjectSave;// Get scene saveif(GameObjectSave.sceneData.TryGetValue(Settings.PersistentScene, out SceneSave sceneSave)){// if dictionaries are not nullif(sceneSave.vector3Dictionary != null && sceneSave.stringDictionary != null){// target grid positionif(sceneSave.vector3Dictionary.TryGetValue("npcTargetGridPosition", out Vector3Serializable savedNPCTargetGridPosition)){npcMovement.npcTargetGridPosition = new Vector3Int((int)savedNPCTargetGridPosition.x, (int)savedNPCTargetGridPosition.y, (int)savedNPCTargetGridPosition.z);npcMovement.npcCurrentGridPosition = npcMovement.npcTargetGridPosition;}// target world positionif(sceneSave.vector3Dictionary.TryGetValue("npcTargetWorldPosition", out Vector3Serializable savedNPCTargetWorldPosition)){npcMovement.npcTargetWorldPosition = new Vector3(savedNPCTargetWorldPosition.x, savedNPCTargetWorldPosition.y, savedNPCTargetWorldPosition.z);transform.position = npcMovement.npcTargetWorldPosition;}// target sceneif(sceneSave.stringDictionary.TryGetValue("npcTargetScene", out string savedTargetScene)){if(Enum.TryParse<SceneName>(savedTargetScene, out SceneName sceneName)){npcMovement.npcTargetScene = sceneName;npcMovement.npcCurrentScene = npcMovement.npcTargetScene;}}// Clear any current NPC movementnpcMovement.CancelNPCMovement();}}}}}

运行游戏

NPC走到Scene3_Cabin后点击Save Game。

然后重新Load Game,NPC出现在Scene3_Cabin里面。

http://www.lqws.cn/news/457111.html

相关文章:

  • 如何使用postman做接口自动化测试?
  • python高校工作室管理系统
  • 基于存储过程的MySQL自动化DDL同步系统设计
  • Spring WebFlux和Spring MVC的对比
  • websocket入门到实战(详解websocket,实战聊天室,消息推送,springboot+vue)
  • MVCC(多版本并发控制)深度解析:原理、流程与实战应用
  • NVIDIA开源Fast-dLLM!解析分块KV缓存与置信度感知并行解码技术
  • Flowise工作流引擎的本地部署与远程访问实践
  • cili3d笔记20 正交投影3d重建笔记1
  • http2与websocket关系
  • Seq2seq机器翻译
  • window显示驱动开发—使用状态刷新回调函数
  • ”三读四隔“:图解数据库三种读问题与四种事务隔离级别
  • 华为和H3C服务器配置远控管理地址
  • [2-02-02].第59节:功能函数 - 函数基础
  • 【案例】性能优化在持续集成与持续交付中的应用
  • Cargo:Rust包管理器的所有命令详解
  • Mac电脑 磁盘检测和监控工具 DriveDx
  • python+uniapp基于微信小程序的高校二手商品交易系统
  • 【Dify学习笔记:】本地部署RagFlow适配Dify
  • React Native +Taro创建项目,开发Android
  • IP 风险画像网络违规行为识别
  • 聊聊spring.mvc.servlet.load-on-startup
  • 浏览器调试核心技术指南:从基础到高级的完全掌握
  • el-table复杂表头(多级表头行或列的合并)
  • Llama 4 模型卡及提示格式介绍
  • 数据融合平台是什么?如何搭建数据融合平台?
  • windows清理系统备份文件夹WinSxS文件夹清理
  • 深入理解JVM执行引擎
  • FramePack 安装指南(中文)