| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using TMPro;
- public class PortalsCountDownUI : MonoBehaviour
- {
- public CameraController cameraController;
- public Vector3 portalPos; //传送门位置
- public TextMeshProUGUI[] text; //倒计时数字
- public GameObject[] effect; //UI指示器
- public GameObject[] countDown; //text和effect的父物体
- public Vector2 parentRect; //父物体Rect Transform的长宽的一半
- public float hypotenuse; //父物体Rect Transform斜边的长度
- public float myRect; //自己的边长的一半
- private void Start()
- {
- hypotenuse = parentRect.magnitude;
- cameraController = GameObject.Find("View").GetComponent<CameraController>();
- }
- private void Update()
- {
- refreshPortalUIPos(cameraController.mainCamera, 0,
- -parentRect + new Vector2(myRect, myRect),
- parentRect - new Vector2(myRect, myRect));
- if (countDown[1].activeSelf)
- {
- countDown[1].SetActive(false);
- }
- //if (cameraController.isSplit)
- //{
- // Camera camera0 = cameraController.player0Camera;
- // Camera camera1 = cameraController.player1Camera;
- // refreshPortalUIPos(
- // camera0, 0,
- // new Vector2(25 + (camera0.rect.x - 0.5f) * parentRect.x * 2 + myRect,
- // - parentRect.y + myRect),
- // new Vector2(-25 + camera0.rect.x * parentRect.x * 2 - myRect,
- // parentRect.y - myRect));
- // refreshPortalUIPos(
- // camera1, 1,
- // new Vector2(25 + (camera1.rect.x - 0.5f) * parentRect.x * 2 + myRect,
- // - parentRect.y + myRect),
- // new Vector2(-25 + camera1.rect.x * parentRect.x * 2 - myRect,
- // parentRect.y - myRect));
- //}
- //else
- //{
- // refreshPortalUIPos(cameraController.mainCamera, 0,
- // - parentRect + new Vector2(myRect, myRect),
- // parentRect - new Vector2(myRect, myRect));
- // if (countDown[1].activeSelf)
- // {
- // countDown[1].SetActive(false);
- // }
- //}
- }
- //刷新指示箭头显示
- void refreshPortalUIPos(Camera camera,int id,Vector2 RectClampMin,Vector2 RectClampMax)
- {
- //计算指示箭头的方向
- Vector3 pos = camera.transform.position;
- float angle = Mathf.Atan2(portalPos.y - pos.y, portalPos.x - pos.x) * Mathf.Rad2Deg - 90;
- effect[id].transform.rotation = Quaternion.Euler(Vector3.forward * angle);
- //判断传送门是否在屏幕内
- Vector3 viewportPoint = camera.WorldToViewportPoint(portalPos);
- if (viewportPoint.x >= 0 && viewportPoint.y >= 0
- && viewportPoint.x <= camera.rect.width && viewportPoint.y <= 1
- && viewportPoint.z > camera.nearClipPlane)
- {
- if (countDown[id].activeSelf)
- {
- countDown[id].SetActive(false);
- }
- }
- else
- {
- if (!countDown[id].activeSelf)
- {
- countDown[id].SetActive(true);
- }
- //计算指示箭头的位置
- Vector3 target = new Vector3(portalPos.x - pos.x, portalPos.y - pos.y, 0);
- target = target.normalized * hypotenuse;
- target.x = Mathf.Clamp(target.x, RectClampMin.x, RectClampMax.x);
- target.y = Mathf.Clamp(target.y, RectClampMin.y, RectClampMax.y);
- countDown[id].transform.localPosition = target;
- }
- }
- }
|