From d9af993db4201bc466f4e387019c81172684d825 Mon Sep 17 00:00:00 2001 From: huyulong <1838407198@qq.com> Date: Thu, 12 Dec 2024 18:51:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86NPC=E7=9A=84?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E7=B1=BBNPCController?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- xiaofang/Assets/ProtoBuf/test.cs | 18 ++++---- .../Assets/Script/hylScripts/NPCController.cs | 41 +++++++++++++++++++ .../Script/hylScripts/NPCController.cs.meta | 11 +++++ xiaofang/Assets/Script/npc/RecuseNpc.cs | 7 ++++ 4 files changed, 69 insertions(+), 8 deletions(-) create mode 100644 xiaofang/Assets/Script/hylScripts/NPCController.cs create mode 100644 xiaofang/Assets/Script/hylScripts/NPCController.cs.meta diff --git a/xiaofang/Assets/ProtoBuf/test.cs b/xiaofang/Assets/ProtoBuf/test.cs index ff579256..9a36acea 100644 --- a/xiaofang/Assets/ProtoBuf/test.cs +++ b/xiaofang/Assets/ProtoBuf/test.cs @@ -13,8 +13,6 @@ public class test : MonoBehaviour { public string token; public WEBScriptListener wEBScriptListener; - public GameObject npc; - public Transform trans; public class auth_login { @@ -515,16 +513,20 @@ public class test : MonoBehaviour float z = float.Parse(npcData.Z.ToString()); Vector3 position = new Vector3(x, y, z); - // 使用指定的世界坐标位置来实例化 NPC,而不是依赖 trans 位置 - GameObject.Instantiate(npc, position, Quaternion.identity); // 直接指定位置和旋转 - // Npc生成后调用选择路线,=================这里也会删掉,在等UI逻辑那边处理完 - + NPCController.instance.InitNPC(position, npcData); + + } if(npcData.Type == 2)//npc移动 { Vector3 v = new Vector3(-float.Parse(npcData.X.ToString()), float.Parse(npcData.Y.ToString()), float.Parse(npcData.Z.ToString())); - RecuseNpc.instance.SetNpcDes(v); - + foreach(var item in NPCController.instance.npcsList) + { + if(npcData.UserId == item.npcId) + { + item.SetNpcDes(v); + } + } } //npc.transform.name = npcData.UserId; diff --git a/xiaofang/Assets/Script/hylScripts/NPCController.cs b/xiaofang/Assets/Script/hylScripts/NPCController.cs new file mode 100644 index 00000000..193eba32 --- /dev/null +++ b/xiaofang/Assets/Script/hylScripts/NPCController.cs @@ -0,0 +1,41 @@ +using System.Collections; +using System.Collections.Generic; +using System.Xml.Linq; +using UnityEngine; +using UnityEngine.UIElements; + +public class NPCController : MonoBehaviour +{ + public static NPCController instance; + + public List npcsList = new List();//存放每个生成的npc的脚本 + public GameObject npc;//npc人物预制体 + + + + + + + // Start is called before the first frame update + void Start() + { + instance = this; + } + + public void InitNPC(Vector3 v,NpcData npcData) + { + // 使用指定的世界坐标位置来实例化 NPC,而不是依赖 trans 位置 + GameObject go = GameObject.Instantiate(npc, v, Quaternion.identity); // 直接指定位置和旋转 + RecuseNpc recuseNpc = go.GetComponent(); + recuseNpc.SetNPCInfo(npcData.UserId);//初始化npcid + npcsList.Add(recuseNpc);//将生成的NPC的脚本加入list存放 + } + + + + // Update is called once per frame + void Update() + { + + } +} diff --git a/xiaofang/Assets/Script/hylScripts/NPCController.cs.meta b/xiaofang/Assets/Script/hylScripts/NPCController.cs.meta new file mode 100644 index 00000000..7cd205e4 --- /dev/null +++ b/xiaofang/Assets/Script/hylScripts/NPCController.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 1a829b8d12d88e14487451b9c601ab85 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/xiaofang/Assets/Script/npc/RecuseNpc.cs b/xiaofang/Assets/Script/npc/RecuseNpc.cs index e3c72cf5..7f780245 100644 --- a/xiaofang/Assets/Script/npc/RecuseNpc.cs +++ b/xiaofang/Assets/Script/npc/RecuseNpc.cs @@ -12,6 +12,8 @@ public enum Npcstate public class RecuseNpc : MonoBehaviour { + public string npcId; + public static RecuseNpc instance; private Button recusebtn; @@ -51,6 +53,11 @@ public class RecuseNpc : MonoBehaviour recusebtn.gameObject.SetActive(false); } + public void SetNPCInfo(string id) + { + npcId = id; + } + //设置NPC的状态 public void Setnpcstate()//点击救援按钮执行完动作后对按钮进行隐藏 {