using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowingUI : MonoBehaviour
{
? ? public Canvas canvas;
? ? public GameObject target;
? ? private Camera uiCamera;
? ? void Start()
? ? {
? ? ? ? if(canvas == null || target ==null)
? ? ? ? {
? ? ? ? ? ? Debug.LogError("FollowingUI: No canvas!");
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? if (canvas.renderMode == RenderMode.ScreenSpaceCamera
? ? ? ? ? ? || canvas.renderMode == RenderMode.WorldSpace)
? ? ? ? {
? ? ? ? ? ? uiCamera = canvas.worldCamera;
? ? ? ? }
? ? ? ? else if (canvas.renderMode == RenderMode.ScreenSpaceOverlay)
? ? ? ? {
? ? ? ? ? ? uiCamera = null;
? ? ? ? }
? ? }
? ? // Update is called once per frame
? ? void Update()
? ? {
? ? ? ? if (canvas == null || target == null)
? ? ? ? {
? ? ? ? ? ? Debug.LogError("FollowingUI: No params!");
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? Vector3 uiPoint = WorldPointToUIPoint(target);
? ? ? ? transform.position = uiPoint;
? ? }
? ? private Vector3 WorldPointToUIPoint(GameObject gameObject)
? ? {
? ? ? ? //world to screen
? ? ? ? Vector2 screenPoint = WorldPointToScreenPoint(gameObject.transform.position);
? ? ? ? //screen to ui
? ? ? ? // 將屏幕坐標轉(zhuǎn)換為 UI transform.position
? ? ? ? RectTransform rt = this.gameObject.GetComponent<RectTransform>();
? ? ? ? Vector3 uiPoint = ScreenPointToUIPoint(rt, screenPoint);
? ? ? ? return uiPoint;
? ? }
? ? /// <summary>
? ? /// 世界坐標轉(zhuǎn)換為屏幕坐標
? ? /// </summary>
? ? /// <param name="worldPoint">屏幕坐標</param>
? ? /// <returns></returns>
? ? public Vector2 WorldPointToScreenPoint(Vector3 worldPoint)
? ? {
? ? ? ? // Camera.main 世界攝像機
? ? ? ? Vector2 screenPoint = Camera.main.WorldToScreenPoint(worldPoint);
? ? ? ? return screenPoint;
? ? }
? ? /// <summary>
? ? /// 屏幕坐標轉(zhuǎn)換為世界坐標
? ? /// </summary>
? ? /// <param name="screenPoint">屏幕坐標</param>
? ? /// <param name="planeZ">距離攝像機 Z 平面的距離</param>
? ? /// <returns></returns>
? ? public static Vector3 ScreenPointToWorldPoint(Vector2 screenPoint, float planeZ)
? ? {
? ? ? ? // Camera.main 世界攝像機
? ? ? ? Vector3 position = new Vector3(screenPoint.x, screenPoint.y, planeZ);
? ? ? ? Vector3 worldPoint = Camera.main.ScreenToWorldPoint(position);
? ? ? ? return worldPoint;
? ? }
? ? // RectTransformUtility.WorldToScreenPoint
? ? // RectTransformUtility.ScreenPointToWorldPointInRectangle
? ? // RectTransformUtility.ScreenPointToLocalPointInRectangle
? ? // 上面三個坐標轉(zhuǎn)換的方法使用 Camera 的地方
? ? // 當(dāng) Canvas renderMode 為 RenderMode.ScreenSpaceCamera挟鸠、RenderMode.WorldSpace 時 傳遞參數(shù) canvas.worldCamera
? ? // 當(dāng) Canvas renderMode 為 RenderMode.ScreenSpaceOverlay 時 傳遞參數(shù) null
? ? // UI 坐標轉(zhuǎn)換為屏幕坐標
? ? public Vector2 UIPointToScreenPoint(Vector3 worldPoint)
? ? {
? ? ? ? // RectTransform:target
? ? ? ? // worldPoint = target.position;
? ? ? ? Vector2 screenPoint = RectTransformUtility.WorldToScreenPoint(uiCamera, worldPoint);
? ? ? ? return screenPoint;
? ? }
? ? // 屏幕坐標轉(zhuǎn)換為 UGUI 坐標
? ? public Vector3 ScreenPointToUIPoint(RectTransform rt, Vector2 screenPoint)
? ? {
? ? ? ? Vector3 globalMousePos;
? ? ? ? // 當(dāng) Canvas renderMode 為 RenderMode.ScreenSpaceCamera、RenderMode.WorldSpace 時 uiCamera 不能為空
? ? ? ? // 當(dāng) Canvas renderMode 為 RenderMode.ScreenSpaceOverlay 時 uiCamera 可以為空
? ? ? ? RectTransformUtility.ScreenPointToWorldPointInRectangle(rt, screenPoint, uiCamera, out globalMousePos);
? ? ? ? // 轉(zhuǎn)換后的 globalMousePos 使用下面方法賦值
? ? ? ? // target 為需要使用的 UI RectTransform
? ? ? ? // rt 可以是 target.GetComponent<RectTransform>(), 也可以是 target.parent.GetComponent<RectTransform>()
? ? ? ? // target.transform.position = globalMousePos;
? ? ? ? return globalMousePos;
? ? }
? ? // 屏幕坐標轉(zhuǎn)換為 UGUI RectTransform 的 anchoredPosition
? ? public Vector2 ScreenPointToUILocalPoint(RectTransform parentRT, Vector2 screenPoint)
? ? {
? ? ? ? Vector2 localPos;
? ? ? ? RectTransformUtility.ScreenPointToLocalPointInRectangle(parentRT, screenPoint, uiCamera, out localPos);
? ? ? ? // 轉(zhuǎn)換后的 localPos 使用下面方法賦值
? ? ? ? // target 為需要使用的 UI RectTransform
? ? ? ? // parentRT 是 target.parent.GetComponent<RectTransform>()
? ? ? ? // 最后賦值 target.anchoredPosition = localPos;
? ? ? ? return localPos;
? ? }
}
https://blog.csdn.net/LIQIANGEASTSUN/article/details/124413387