【Unity】UI跟隨物體GameObject

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末探膊,一起剝皮案震驚了整個濱河市卤橄,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌婿屹,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,470評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異网缝,居然都是意外死亡,警方通過查閱死者的電腦和手機蟋定,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,393評論 3 392
  • 文/潘曉璐 我一進店門粉臊,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人驶兜,你說我怎么就攤上這事扼仲。” “怎么了抄淑?”我有些...
    開封第一講書人閱讀 162,577評論 0 353
  • 文/不壞的土叔 我叫張陵屠凶,是天一觀的道長。 經(jīng)常有香客問我肆资,道長矗愧,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,176評論 1 292
  • 正文 為了忘掉前任郑原,我火速辦了婚禮唉韭,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘犯犁。我一直安慰自己属愤,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,189評論 6 388
  • 文/花漫 我一把揭開白布酸役。 她就那樣靜靜地躺著住诸,像睡著了一般。 火紅的嫁衣襯著肌膚如雪涣澡。 梳的紋絲不亂的頭發(fā)上贱呐,一...
    開封第一講書人閱讀 51,155評論 1 299
  • 那天,我揣著相機與錄音暑塑,去河邊找鬼吼句。 笑死锅必,一個胖子當(dāng)著我的面吹牛事格,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播搞隐,決...
    沈念sama閱讀 40,041評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼驹愚,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了劣纲?” 一聲冷哼從身側(cè)響起逢捺,我...
    開封第一講書人閱讀 38,903評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎癞季,沒想到半個月后劫瞳,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體倘潜,經(jīng)...
    沈念sama閱讀 45,319評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,539評論 2 332
  • 正文 我和宋清朗相戀三年志于,在試婚紗的時候發(fā)現(xiàn)自己被綠了涮因。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,703評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡伺绽,死狀恐怖养泡,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情奈应,我是刑警寧澤澜掩,帶...
    沈念sama閱讀 35,417評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站杖挣,受9級特大地震影響肩榕,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜惩妇,卻給世界環(huán)境...
    茶點故事閱讀 41,013評論 3 325
  • 文/蒙蒙 一点把、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧屿附,春花似錦郎逃、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,664評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至匀泊,卻和暖如春优训,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背各聘。 一陣腳步聲響...
    開封第一講書人閱讀 32,818評論 1 269
  • 我被黑心中介騙來泰國打工揣非, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人躲因。 一個月前我還...
    沈念sama閱讀 47,711評論 2 368
  • 正文 我出身青樓早敬,卻偏偏與公主長得像,于是被迫代替她去往敵國和親大脉。 傳聞我的和親對象是個殘疾皇子搞监,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,601評論 2 353

推薦閱讀更多精彩內(nèi)容