_xiaofang/xiaofang/Assets/Script/hylScripts/TargetIndicator.cs

113 lines
4.4 KiB
C#
Raw Normal View History

2024-12-19 16:06:06 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TargetIndicator : MonoBehaviour
{
public Camera mainCamera; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public Transform target; // Ŀ<><C4BF><EFBFBD><EFBFBD>
public RectTransform uiCanvas; // UI Canvas <20><> RectTransform
public RectTransform targetIcon; // Ŀ<><C4BF>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD> UI
2024-12-19 17:14:14 +08:00
public Text distanceText; // <20><>Ļ<EFBFBD><C4BB><EFBFBD><EFBFBD>ʾ<EFBFBD>ľ<EFBFBD><C4BE><EFBFBD><EFBFBD>ı<EFBFBD>
2024-12-19 16:06:06 +08:00
public RectTransform arrowIcon; // <20><>Ļ<EFBFBD><C4BB><EFBFBD><EFBFBD>ͷͼ<CDB7><CDBC>
2024-12-19 17:14:14 +08:00
public Text arrowDistanceText; // <20><>Ļ<EFBFBD><C4BB><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD>ľ<EFBFBD><C4BE><EFBFBD><EFBFBD>ı<EFBFBD>
2024-12-19 16:06:06 +08:00
public float edgePadding = 50f; // <20><>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD><C4BB>Ե<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
2024-12-19 17:14:14 +08:00
public Vector2 textOffset = new Vector2(0, -30); // <20>ı<EFBFBD><C4B1><EFBFBD><EFBFBD>Լ<EFBFBD>ͷ<EFBFBD><CDB7>ƫ<EFBFBD><C6AB>
2024-12-19 16:06:06 +08:00
void Update()
{
// <20><>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD><C4BB><EFBFBD><EFBFBD>
Vector3 screenPoint = mainCamera.WorldToScreenPoint(target.position);
// <20><><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF><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)
{
// Ŀ<><C4BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD>ڣ<EFBFBD><DAA3><EFBFBD>ʾĿ<CABE><C4BF>ͼ<EFBFBD><CDBC>
targetIcon.gameObject.SetActive(true);
arrowIcon.gameObject.SetActive(false); // <20><><EFBFBD>ؼ<EFBFBD>ͷ
2024-12-19 17:14:14 +08:00
arrowDistanceText.gameObject.SetActive(false); // <20><><EFBFBD>ؼ<EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD>
2024-12-19 16:06:06 +08:00
// <20><><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC>λ<EFBFBD><CEBB>
targetIcon.position = screenPoint;
2024-12-19 17:14:14 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD>ھ<EFBFBD><DABE><EFBFBD><EFBFBD>ı<EFBFBD>
2024-12-19 16:06:06 +08:00
float distance = Vector3.Distance(mainCamera.transform.position, target.position);
distanceText.text = $"{distance:F1}m";
}
else
{
// Ŀ<><C4BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD><EFBFBD><E2A3AC>ʾ<EFBFBD><CABE>ͷ
targetIcon.gameObject.SetActive(false);
2024-12-19 17:14:14 +08:00
arrowIcon.gameObject.SetActive(true); // <20><>ʾ<EFBFBD><CABE>ͷ
arrowDistanceText.gameObject.SetActive(true); // <20><>ʾ<EFBFBD><CABE>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD>
2024-12-19 16:06:06 +08:00
// <20><><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Һ󷽣<D2BA><F3B7BDA3><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>
if (screenPoint.z < 0)
{
screenPoint.x = Screen.width - screenPoint.x; // ˮƽ<CBAE><C6BD>ת
screenPoint.y = Screen.height - screenPoint.y; // <20><>ֱ<EFBFBD><D6B1>ת
}
// <20><><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD><C4BB><EFBFBD>ĵķ<C4B5><C4B7><EFBFBD>
Vector3 direction = (screenPoint - new Vector3(Screen.width / 2, Screen.height / 2, 0)).normalized;
2024-12-19 17:14:14 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD>Ļ<EFBFBD>߽<EFBFBD><DFBD><EFBFBD>λ<EFBFBD><CEBB>
2024-12-19 16:06:06 +08:00
Vector3 edgePosition = GetScreenEdgePosition(direction);
// <20><><EFBFBD>ü<EFBFBD>ͷλ<CDB7><CEBB>
arrowIcon.position = edgePosition;
2024-12-19 17:14:14 +08:00
// <20><><EFBFBD>þ<EFBFBD><C3BE><EFBFBD><EFBFBD>ı<EFBFBD>λ<EFBFBD>ã<EFBFBD><C3A3>ڼ<EFBFBD>ͷ<EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Vector3 textPosition = edgePosition + (Vector3)textOffset;
textPosition = ClampToScreenBounds(textPosition); // <20><><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD><C4BB>Χ<EFBFBD><CEA7>
arrowDistanceText.transform.position = textPosition;
// <20><><EFBFBD>־<EFBFBD><D6BE><EFBFBD><EFBFBD>ı<EFBFBD>ˮƽ<CBAE><C6BD>ʾ
arrowDistanceText.transform.rotation = Quaternion.identity;
// <20><><EFBFBD>¾<EFBFBD><C2BE><EFBFBD><EFBFBD>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD>
float distance = Vector3.Distance(mainCamera.transform.position, target.position);
arrowDistanceText.text = $"{distance:F1}m";
2024-12-19 16:06:06 +08:00
// <20><>ת<EFBFBD><D7AA>ͷʹ<CDB7><CAB9>ָ<EFBFBD><D6B8>Ŀ<EFBFBD><EFBFBD><EAB7BD>
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
arrowIcon.rotation = Quaternion.Euler(0, 0, angle);
}
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD>Ļ<EFBFBD>߽<EFBFBD><DFBD><EFBFBD>λ<EFBFBD><CEBB>
private Vector3 GetScreenEdgePosition(Vector3 direction)
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD><C4BB><EFBFBD><EFBFBD>
float halfScreenWidth = Screen.width / 2 - edgePadding;
float halfScreenHeight = Screen.height / 2 - edgePadding;
// <20>жϼ<D0B6>ͷ<EFBFBD>Ƿ<EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD><C4BB><EFBFBD><EFBFBD><EFBFBD>±<EFBFBD>Ե
float slope = direction.y / direction.x;
if (Mathf.Abs(slope) > halfScreenHeight / halfScreenWidth)
{
// <20><><EFBFBD><EFBFBD><EFBFBD>±<EFBFBD>Ե
float edgeY = Mathf.Sign(direction.y) * halfScreenHeight + Screen.height / 2;
float edgeX = (edgeY - Screen.height / 2) / slope + Screen.width / 2;
return new Vector3(edgeX, edgeY, 0);
}
else
{
// <20><><EFBFBD><EFBFBD><EFBFBD>ұ<EFBFBD>Ե
float edgeX = Mathf.Sign(direction.x) * halfScreenWidth + Screen.width / 2;
float edgeY = slope * (edgeX - Screen.width / 2) + Screen.height / 2;
return new Vector3(edgeX, edgeY, 0);
}
}
2024-12-19 17:14:14 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD><C4BB>Χ<EFBFBD><CEA7>
private Vector3 ClampToScreenBounds(Vector3 position)
{
position.x = Mathf.Clamp(position.x, edgePadding, Screen.width - edgePadding);
position.y = Mathf.Clamp(position.y, edgePadding, Screen.height - edgePadding);
return position;
}
2024-12-19 16:06:06 +08:00
}