123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- /// <summary>
- /// 朝向提示UI
- /// </summary>
- public class ArrowTips_EyeMode : MonoBehaviour
- {
- private Transform _arrow_Rot;
- public bool _openTips = false;
- public Transform _target;
- public Camera _camera;
- private float targetAngle;
- public float lerpIndex = 0.9f;
- private void Awake()
- {
- Init();
- }
- public void Init()
- {
- _arrow_Rot = this.transform.GetChild(0).Find("Arrow_Eye_Rot");
- if (!_openTips)
- {
- _arrow_Rot.gameObject.SetActive(false);
- }
- }
- // Update is called once per frame
- void Update()
- {
- if (_openTips)
- {
- ArrowController();
- }
- }
- public void ArrowController()
- {
- if (_target == null) return;
- if (!_target.gameObject.activeSelf && _arrow_Rot.gameObject.activeSelf)
- {
- _arrow_Rot.gameObject.SetActive(false);
- }
- else if (_target.gameObject.activeSelf && !_arrow_Rot.gameObject.activeSelf)
- {
- _arrow_Rot.gameObject.SetActive(true);
- }
- if (!_target.gameObject.activeSelf) return;
- //物体转化为摄像机屏幕坐标
- Vector3 screenPointInV3 = _camera.WorldToScreenPoint(_target.position);
- Vector2 screenPoint = new Vector2(screenPointInV3.x, screenPointInV3.y);
- if (screenPointInV3.z <= 0) //物体在摄像机后方
- {
- Vector3 screenCenterPos = new Vector3(_camera.pixelWidth / 2, _camera.pixelHeight / 2, 0);
- float angle = 90;
- if (Vector3.Cross(Vector3.up, (screenPointInV3 - screenCenterPos)).z < 0)
- {
- angle = -angle;
- }
- targetAngle = -angle;
- }
- else if (!_camera.pixelRect.Contains(screenPoint)) //摄像机正面且不在画面中
- {
- Vector3 screenCenterPos = new Vector3(_camera.pixelWidth / 2, _camera.pixelHeight / 2, 0);
- float angle = Vector3.Angle(Vector3.up, (screenPointInV3 - screenCenterPos));
- if (Vector3.Cross(Vector3.up, (screenPointInV3 - screenCenterPos)).z > 0)
- {
- angle = -angle;
- }
- targetAngle = -angle;
- if (!_arrow_Rot.gameObject.activeSelf)
- {
- _arrow_Rot.gameObject.SetActive(true);
- _arrow_Rot.transform.localRotation = Quaternion.Euler(0, 0, targetAngle);
- }
- }
- else
- {
- _arrow_Rot.gameObject.SetActive(false);
- }
- //_Arrow朝向是反的
- _arrow_Rot.transform.localRotation = Quaternion.Lerp(_arrow_Rot.transform.localRotation, Quaternion.Euler(0, 0, targetAngle), lerpIndex);
- }
- public void OpenEyeTips(Transform t)
- {
- _target = t;
- if (t != null)
- {
- _openTips = true;
- }
- }
- public void CloseEyeTips()
- {
- _target = null;
- _openTips = false;
- }
- }
|