using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
public class ArrowsManager : MonoBehaviour
{
private GameObject m_arrows;
private Vector3 m_Screen_Center;
private void Start()
{
m_Screen_Center = new Vector3(Screen.width / 2, Screen.height / 2, 0);
m_arrows = GameObject.Find("Canvas/Arrows");
InitArrowsColor();
}
void Update()
{
if (!GameManager.GetInst().m_bStart) return;
ReflushArrows();
}
private void ReflushArrows()
{
for(int i = 0;i < GameManager.GetInst().m_groupManager.m_listEnemyGroup.Count;i++)
{
Group group = GameManager.GetInst().m_groupManager.m_listEnemyGroup[i];
string str = Enum.GetName(typeof(E_ClothesType), group.m_clothesType);
Transform tfArrow = m_arrows.transform.Find(str);
tfArrow.gameObject.SetActive(!IsInScreen(group.transform));
SetLookAt(tfArrow.GetComponent<RectTransform>(), group.transform);
//设置距离数值
float distance = Vector3.Distance(group.transform.position, GameManager.GetInst().m_groupManager.m_playerGroup.transform.position);
tfArrow.GetChild(0).GetComponent<Text>().text = (int)distance + "m";
SetEdge(tfArrow);
}
}
//设置到屏幕边缘
private void SetEdge(Transform tf)
{
Vector3 CachePosion = tf.right * (Screen.width > Screen.height ? Screen.width - 100 : Screen.height - 100);
CachePosion.x = Mathf.Clamp(CachePosion.x, -Screen.width / 2 + 100, Screen.width / 2 - 100);
CachePosion.y = Mathf.Clamp(CachePosion.y, -Screen.height / 2 + 100, Screen.height / 2 - 100);
tf.GetComponent<RectTransform>().anchoredPosition3D = CachePosion;
}
private void SetLookAt(RectTransform arrow,Transform target)
{
//将目标的世界坐标转换成屏幕坐标
Vector3 target_ScreenPoint = Camera.main.WorldToScreenPoint(target.position);
//计算target_ScreenPoint和屏幕中心点的绝对值角度
float angle = Mathf.Atan(Mathf.Abs(target_ScreenPoint.y - Screen.height / 2)
/ Mathf.Abs(target_ScreenPoint.x - Screen.width / 2)) * 180 / Mathf.PI;
//通过判断target_ScreenPoint相对屏幕中心点所在象限处理angle的差值
if (target_ScreenPoint.x <= Screen.width / 2)
angle = target_ScreenPoint.y >= Screen.height / 2 ? 90 - angle : 90 + angle;
else
angle = target_ScreenPoint.y >= Screen.height / 2 ? 270 + angle : 270 - angle;
#region 计算摄像机和目标的叉乘
//以屏幕中心所在的单位向量为起始向量from,并将from的y值设成和目标y值保持一致
Vector3 from = Camera.main.transform.forward;
from.y = target.forward.y;
//将屏幕中心坐标转换成世界坐标
Vector3 cameraPos = Camera.main.ScreenToWorldPoint(m_Screen_Center);
cameraPos.y = target.position.y;
//求出目标点与摄像机之间的向量
Vector3 to = target.position - cameraPos;
//求出两向量之间的叉乘
Vector3 cross = Vector3.Cross(from, to);
#endregion
//根据叉乘求出带符号的角度
//cross.y > 0:目标向量位于起始向量右侧
//cross.y < 0:目标向量位于起始向量左侧
if (cross.y > 0 && angle < 180)
angle += 180;
else if (cross.y < 0 && angle > 180)
angle -= 180;
Vector3 euler = arrow.eulerAngles;
euler.z = angle;
//设置箭头的角度
arrow.eulerAngles = euler;
}
private bool IsInScreen(Transform tf)
{
Vector2 vt2 = ToScreenPos(tf);
if(vt2.x > 0 && vt2.y > 0 && vt2.x < Screen.width && vt2.y < Screen.height)
{
return true;
}
return false;
}
private Vector2 ToScreenPos(Transform tf)
{
return Camera.main.WorldToScreenPoint(tf.position);
}
private void InitArrowsColor()
{
for(int i = 0;i < m_arrows.transform.childCount;i++)
{
Transform child = m_arrows.transform.GetChild(i);
E_ClothesType type = (E_ClothesType)Enum.Parse(typeof(E_ClothesType), child.name);
child.GetComponent<Image>().color = Utils.GetColor(type);
}
}
}
网友评论