_xiaofang/xiaofang/Assets/Res/gsj/xiaozhigou.cs

151 lines
4.6 KiB
C#
Raw Normal View History

2024-12-26 22:26:18 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class xiaozhigou : MonoBehaviour
{
public Camera mainCamera; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public RectTransform uiCanvas; // UI Canvas <20><> RectTransform
public GameObject targetIconPrefab; // UI ͼ<><CDBC><EFBFBD><EFBFBD>Ԥ<EFBFBD><D4A4><EFBFBD><EFBFBD>
private LinkedList<GameObject> playerList = new LinkedList<GameObject>(); // <20><EFBFBD><E6B4A2>ǩΪ "Player" <20>Ķ<EFBFBD><C4B6><EFBFBD>
private GameObject peopleposition;//λ<>õ<EFBFBD><C3B5><EFBFBD>Ϣ
private Dictionary<GameObject, RectTransform> playerUIMap = new Dictionary<GameObject, RectTransform>(); // <20><EFBFBD><E6B4A2>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6> UI <20><><EFBFBD><EFBFBD>
private void Start()
{
peopleposition = GameObject.Find("peopleposition");
if (peopleposition == null)
{
Debug.LogError("<22><>ȷ<EFBFBD><C8B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>Ϊ 'peopleposition' <20><> GameObject <20><>Ϊ UI <20>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD>塣");
return;
}
// <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD> "Player" <20><>ǩ<EFBFBD><C7A9><EFBFBD>󲢴<EFBFBD><F3B2A2B4><EFBFBD> UI <20><><EFBFBD><EFBFBD>
UpdatePlayerList();
CreateUIForPlayers();
}
void Update()
{
// ʵʱ<CAB5><CAB1><EFBFBD><EFBFBD> Player <20>б<EFBFBD><D0B1>Ͷ<EFBFBD>Ӧ<EFBFBD><D3A6> UI
UpdatePlayerList();
UpdateUIForPlayers();
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD> "Player" <20><><EFBFBD><EFBFBD>
private void UpdatePlayerList()
{
// <20><>ȡ<EFBFBD><C8A1><EFBFBD>д<EFBFBD><D0B4><EFBFBD> "Player" <20><>ǩ<EFBFBD>Ķ<EFBFBD><C4B6><EFBFBD>
GameObject[] players = GameObject.FindGameObjectsWithTag("Player");
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Player
foreach (GameObject player in players)
{
if (!playerList.Contains(player)) // <20><><EFBFBD><EFBFBD><EFBFBD>Ľ<EFBFBD>ɫ
{
playerList.AddLast(player);
// Ϊ<><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD> UI <20><><EFBFBD><EFBFBD>
if (!playerUIMap.ContainsKey(player))
{
CreateUIForPlayer(player);
}
}
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƴ<EFBFBD><C6B3><EFBFBD> Player
var node = playerList.First;
while (node != null)
{
var nextNode = node.Next;
if (!System.Array.Exists(players, p => p == node.Value))
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ<EFBFBD>ѱ<EFBFBD><D1B1>Ƴ<EFBFBD><C6B3><EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6> UI <20><><EFBFBD><EFBFBD>
if (playerUIMap.ContainsKey(node.Value))
{
Destroy(playerUIMap[node.Value].gameObject);
playerUIMap.Remove(node.Value);
}
playerList.Remove(node);
}
node = nextNode;
}
}
// Ϊ<><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҵ<EFBFBD><D2B4><EFBFBD> UI <20><><EFBFBD><EFBFBD>
private void CreateUIForPlayers()
{
foreach (var player in playerList)
{
if (!playerUIMap.ContainsKey(player))
{
CreateUIForPlayer(player);
}
}
}
// Ϊ<><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҵ<EFBFBD><D2B4><EFBFBD> UI <20><><EFBFBD><EFBFBD>
private void CreateUIForPlayer(GameObject player)
{
if (targetIconPrefab == null || uiCanvas == null)
{
Debug.LogError("<22><>ȷ<EFBFBD><C8B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> TargetIconPrefab <20><> uiCanvas<61><73>");
return;
}
// ʵ<><CAB5><EFBFBD><EFBFBD> UI ͼ<><CDBC>
GameObject iconInstance = Instantiate(targetIconPrefab, peopleposition.transform);
// <20><>ȡ RectTransform <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>
RectTransform iconRectTransform = iconInstance.GetComponent<RectTransform>();
// <20><><EFBFBD>ó<EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
Text nameText = iconInstance.GetComponentInChildren<Text>();
if (nameText != null)
{
nameText.text = player.name; // <20><>ʾ<EFBFBD><CABE>ɫ<EFBFBD><C9AB><EFBFBD><EFBFBD>
}
// <20><> UI ͼ<><CDBC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҷ<EFBFBD>Ӧ
playerUIMap[player] = iconRectTransform;
}
// ʵʱ<CAB5><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD> UI
private void UpdateUIForPlayers()
{
foreach (var player in playerList)
{
if (!playerUIMap.ContainsKey(player)) continue;
RectTransform targetIcon = playerUIMap[player];
// <20><>ȡ<EFBFBD><C8A1><EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD>Ļ<EFBFBD><C4BB><EFBFBD><EFBFBD>
Vector3 screenPoint = mainCamera.WorldToScreenPoint(player.transform.position);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD><C4BB>
if (screenPoint.z > 0 && screenPoint.x > 0 && screenPoint.x < Screen.width && screenPoint.y > 0 && screenPoint.y < Screen.height)
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD>ڣ<EFBFBD><DAA3><EFBFBD>ʾͼ<CABE><CDBC>
targetIcon.gameObject.SetActive(true);
targetIcon.position = screenPoint;
//// <20><><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//Text nameText = targetIcon.GetComponentInChildren<Text>();
//if (nameText != null)
//{
// float distance = Vector3.Distance(mainCamera.transform.position, player.transform.position);
// nameText.text = $"{player.name}\n<><6E><EFBFBD><EFBFBD>: {distance:F1} <20><>";
//}
}
else
{
// <20><><EFBFBD>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD>ڣ<EFBFBD><DAA3><EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC>
targetIcon.gameObject.SetActive(false);
}
}
}
}