From d98d907ca2bccef7f911469232148ba9d33e0aa8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=8F=B7=E6=95=AC?= <153802103@qq.com> Date: Mon, 16 Dec 2024 10:24:19 +0800 Subject: [PATCH] NPctext --- xiaofang/Assets/ProtoBuf/test.cs | 29 ++++++++++++++++--- .../Assets/Script/hylScripts/NPCController.cs | 14 ++++++--- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/xiaofang/Assets/ProtoBuf/test.cs b/xiaofang/Assets/ProtoBuf/test.cs index d1d3ba43..c88f9f10 100644 --- a/xiaofang/Assets/ProtoBuf/test.cs +++ b/xiaofang/Assets/ProtoBuf/test.cs @@ -9,6 +9,7 @@ using Google.Protobuf; using System.Net.WebSockets; using System; using System.Threading.Tasks; +using static UnityEditor.Progress; public class test : MonoBehaviour { public string token; @@ -550,16 +551,36 @@ public class test : MonoBehaviour Vector3 position = new Vector3(x, y, z); NPCController.instance.InitNPC(position, npcData); + } if (npcData.Type == 2)//npc移动 { Vector3 v = new Vector3(float.Parse(npcData.X.ToString()), float.Parse(npcData.Y.ToString()) + 0.5f, float.Parse(npcData.Z.ToString())); - foreach (RecuseNpc item in NPCController.instance.npcsList) + //NPCController.instance.npcposition.Add(v); + //foreach (RecuseNpc item in NPCController.instance.npcsList) + //{ + // if (npcData.UserId == item.npcId) + // { + // //Debug.Log("================" + v); + // item.SetNpcDes(v); + // } + //} + foreach (var npcDict in NPCController.instance.npcsList) { - if (npcData.UserId == item.npcId) + // 遍历每个字典中的键值对 + foreach (var kvp in npcDict) { - //Debug.Log("================" + v); - item.SetNpcDes(v); + RecuseNpc npc = kvp.Key; // 获取 NPC 脚本(键) + Vector3 position = kvp.Value; // 获取 NPC 的位置(值) + + // 在这里,你可以使用 npc 和 position 进行其他操作 + if (npcData.UserId == npc.npcId) + { + //Debug.Log("================" + v); + npc.SetNpcDes(v); + } + + } } } diff --git a/xiaofang/Assets/Script/hylScripts/NPCController.cs b/xiaofang/Assets/Script/hylScripts/NPCController.cs index 933ac4cb..4bac0f97 100644 --- a/xiaofang/Assets/Script/hylScripts/NPCController.cs +++ b/xiaofang/Assets/Script/hylScripts/NPCController.cs @@ -1,6 +1,7 @@ using System.Collections; using System.Collections.Generic; using System.Xml.Linq; +using Unity.VisualScripting; using UnityEngine; using UnityEngine.UIElements; @@ -8,10 +9,9 @@ public class NPCController : MonoBehaviour { public static NPCController instance; - public List npcsList = new List();//存放每个生成的npc的脚本 + public List> npcsList = new List>();//存放每个生成的npc的脚本 public GameObject npc;//npc人物预制体 - - + public List npcposition = new List();//存放每个生成的npc位置 @@ -29,7 +29,13 @@ public class NPCController : MonoBehaviour RecuseNpc recuseNpc = go.GetComponent(); recuseNpc.SetNPCInfo(npcData.UserId);//初始化npcid Debug.Log(npcData.UserId); - npcsList.Add(recuseNpc);//将生成的NPC的脚本加入list存放 + Dictionary npcDict = new Dictionary + { + { recuseNpc, v } // 将 recuseNpc 和其位置 v 作为键值对添加到字典中 + }; + + // 将字典添加到 npcsList + npcsList.Add(npcDict);//将生成的NPC的脚本加入list存放 }