_xiaofang/xiaofang/Assets/Res/gsj/xiaozhigou.cs
2024-12-26 22:26:18 +08:00

151 lines
4.6 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class xiaozhigou : MonoBehaviour
{
public Camera mainCamera; // 玩家摄像机
public RectTransform uiCanvas; // UI Canvas 的 RectTransform
public GameObject targetIconPrefab; // UI 图标的预制体
private LinkedList<GameObject> playerList = new LinkedList<GameObject>(); // 存储标签为 "Player" 的对象
private GameObject peopleposition;//位置的信息
private Dictionary<GameObject, RectTransform> playerUIMap = new Dictionary<GameObject, RectTransform>(); // 存储角色与对应的 UI 标记
private void Start()
{
peopleposition = GameObject.Find("peopleposition");
if (peopleposition == null)
{
Debug.LogError("请确保场景中有一个名为 'peopleposition' 的 GameObject 作为 UI 的父物体。");
return;
}
// 初始化获取当前场景中所有的 "Player" 标签对象并创建 UI 标记
UpdatePlayerList();
CreateUIForPlayers();
}
void Update()
{
// 实时更新 Player 列表和对应的 UI
UpdatePlayerList();
UpdateUIForPlayers();
}
// 更新链表中的 "Player" 对象
private void UpdatePlayerList()
{
// 获取所有带有 "Player" 标签的对象
GameObject[] players = GameObject.FindGameObjectsWithTag("Player");
// 检查新增的 Player
foreach (GameObject player in players)
{
if (!playerList.Contains(player)) // 新增的角色
{
playerList.AddLast(player);
// 为新增角色创建 UI 标记
if (!playerUIMap.ContainsKey(player))
{
CreateUIForPlayer(player);
}
}
}
// 检查已移除的 Player
var node = playerList.First;
while (node != null)
{
var nextNode = node.Next;
if (!System.Array.Exists(players, p => p == node.Value))
{
// 如果角色已被移除,删除对应的 UI 标记
if (playerUIMap.ContainsKey(node.Value))
{
Destroy(playerUIMap[node.Value].gameObject);
playerUIMap.Remove(node.Value);
}
playerList.Remove(node);
}
node = nextNode;
}
}
// 为所有玩家创建 UI 标记
private void CreateUIForPlayers()
{
foreach (var player in playerList)
{
if (!playerUIMap.ContainsKey(player))
{
CreateUIForPlayer(player);
}
}
}
// 为单个玩家创建 UI 标记
private void CreateUIForPlayer(GameObject player)
{
if (targetIconPrefab == null || uiCanvas == null)
{
Debug.LogError("请确保已设置 TargetIconPrefab 和 uiCanvas。");
return;
}
// 实例化 UI 图标
GameObject iconInstance = Instantiate(targetIconPrefab, peopleposition.transform);
// 获取 RectTransform 用于设置位置
RectTransform iconRectTransform = iconInstance.GetComponent<RectTransform>();
// 设置初始内容
Text nameText = iconInstance.GetComponentInChildren<Text>();
if (nameText != null)
{
nameText.text = player.name; // 显示角色名称
}
// 将 UI 图标与玩家对应
playerUIMap[player] = iconRectTransform;
}
// 实时更新所有玩家的 UI
private void UpdateUIForPlayers()
{
foreach (var player in playerList)
{
if (!playerUIMap.ContainsKey(player)) continue;
RectTransform targetIcon = playerUIMap[player];
// 获取玩家的屏幕坐标
Vector3 screenPoint = mainCamera.WorldToScreenPoint(player.transform.position);
// 检查玩家是否在屏幕内
if (screenPoint.z > 0 && screenPoint.x > 0 && screenPoint.x < Screen.width && screenPoint.y > 0 && screenPoint.y < Screen.height)
{
// 玩家在屏幕内,显示图标
targetIcon.gameObject.SetActive(true);
targetIcon.position = screenPoint;
//// 更新图标内容
//Text nameText = targetIcon.GetComponentInChildren<Text>();
//if (nameText != null)
//{
// float distance = Vector3.Distance(mainCamera.transform.position, player.transform.position);
// nameText.text = $"{player.name}\n距离: {distance:F1} 米";
//}
}
else
{
// 玩家不在屏幕内,隐藏图标
targetIcon.gameObject.SetActive(false);
}
}
}
}