UnityUGUI代碼生成UI設(shè)置為相對(duì)位置問(wèn)題

      網(wǎng)友投稿 1194 2022-05-30

      什么是RectTransform?

      創(chuàng)建一個(gè)UGUI組件時(shí),查看其Inspector面板,原來(lái)Transform已經(jīng)被替換成RectTransform,面板屬性也不一樣了,如下圖:

      Unity官方對(duì)RectTransform的描述:

      Position, size, anchor and pivot information for a rectangle.

      RectTransforms are used for GUI but can also be used for other things. It’s used to store and manipulate the position, size, and anchoring of a rectangle and supports various forms of scaling based on a parent RectTransform.

      矩形的位置、大小、錨點(diǎn)和樞軸信息。

      RectTransforms用于GUI,也可以用于其他用途。它用于存儲(chǔ)和操作矩形的位置、大小和錨定,并支持基于父矩形變換的各種形式的縮放

      相較于RectTransform,RectTransform提供了更強(qiáng)大的功能來(lái)對(duì)矩形進(jìn)行操作,這主要?dú)w功于新增加的兩個(gè)概念:Anchor(錨點(diǎn))和Pivot(中心)。

      做適配時(shí),主要設(shè)置好錨點(diǎn)和要讓其顯示的位置就可以了,

      對(duì)其基礎(chǔ)設(shè)置:

      //posx,y,z --> 相對(duì)位置 go.transform.localPosition = new Vector3(35, 0, 0); //也可以這樣 go.GetComponent().anchoredPosition3D =new Vector3(35, 0, 0); go.GetComponent().anchoredPosition = new Vector2(0,1); //width , height --> go.GetComponent().sizeDelta = new Vector2(134, 187); //top --> 點(diǎn)擊Unity預(yù)定義的錨點(diǎn)位置,其Anchors 下的Min,Max 對(duì)應(yīng)的值就會(huì)發(fā)生變化 //代碼設(shè)置為對(duì)應(yīng)的值,就會(huì)有相應(yīng)的效果 go.transform.GetComponent().anchorMin = new Vector2(0.5f, 1); go.transform.GetComponent().anchorMax = new Vector2(0.5f, 1);

      官方文檔:https://docs.unity3d.com/ScriptReference/RectTransform.html

      基礎(chǔ)操作就不在這里說(shuō)了,官方有很詳細(xì)的說(shuō)明,其他博主也有很詳盡的解析,,,

      實(shí)現(xiàn)目標(biāo):在不同分辨率下生成的UI在相對(duì)位置

      Unity 之 UGUI代碼生成UI設(shè)置為相對(duì)位置問(wèn)題

      如下圖:(不管是什么比例,都和頂部保持一定的距離)

      PS:如果只在此處使用,那么完全可以將錨點(diǎn)設(shè)置為這樣:

      但是我多處使用,又不想為了適配來(lái)兩個(gè)預(yù)制體使用,,,只好用代碼來(lái)幫助我實(shí)現(xiàn)了

      UGUI內(nèi)置的兩個(gè)方法,

      SetInsetAndSizeFromParentEdge SetSizeWithCurrentAnchors

      獲取動(dòng)態(tài)生成的UI(RectTransform),以及調(diào)試好的相對(duì)位置偏移,調(diào)用如下方法:

      ///

      /// 設(shè)置生成 UI 的相對(duì)位置 (right) /// 更改運(yùn)算符即可設(shè)置Left (如注釋) /// /// RectTransform /// Bot值 public void SetRight(RectTransform rect, float right) { if (right < -rect.offsetMax.x) //Left (left < rect.offsetMin.x) { float value = -rect.offsetMax.x - right;//rect.offsetMin.x - left; rect.offsetMin = new Vector2(rect.offsetMin.x + value, rect.offsetMin.y);// - value rect.offsetMax = new Vector2(-right, rect.offsetMax.y); //(left, rect.offsetMax.y) } else if (right > -rect.offsetMax.x) // (left > rect.offsetMin.x) { float value = right + rect.offsetMax.x; //- rect.offsetMin = new Vector2(rect.offsetMin.x - value, rect.offsetMin.y);// + value rect.offsetMax = new Vector2(-right, rect.offsetMax.y); //(left, rect.offsetMax.y) } } /// /// 設(shè)置生成 UI 的相對(duì)位置 (Bottom) /// 更改運(yùn)算符即可設(shè)置Top (如注釋) /// /// RectTransform /// Bot值 public void SetBottom(RectTransform rect, float bot) { if (bot < rect.offsetMin.y) //(top < -rect.offsetMax.y) { float value = rect.offsetMin.y - bot; //-rect.offsetMax.y - top; rect.offsetMin = new Vector2(rect.offsetMin.x, bot); rect.offsetMax = new Vector2(rect.offsetMax.x, rect.offsetMax.y - value);//top: + value } else if (bot > rect.offsetMin.y) //(top > -rt.offsetMax.y) { float value = bot - rect.offsetMin.y; //top + rect.offsetMin = new Vector2(rect.offsetMin.x, bot); // - top rect.offsetMax = new Vector2(rect.offsetMax.x, rect.offsetMax.y + value); // - value } }

      很實(shí)用的方法,,,這樣可以實(shí)現(xiàn)在不同分辨率下生成的UI在相對(duì)位置,(當(dāng)然如果可以事先對(duì)預(yù)制體設(shè)置好了適配,生成時(shí)直接設(shè)置位置就可以了)

      還有一種很便捷的方式(推薦使用):

      大致思路:設(shè)置要目標(biāo)UI的錨點(diǎn),然后通過(guò)anchoredPosition3D屬性,設(shè)置去相對(duì)于錨點(diǎn)的位置,(不是相對(duì)于中心點(diǎn),如下面的img_3)

      using UnityEngine; using UnityEngine.UI; public class TestTransDemo : MonoBehaviour { public Image img_1; public Image img_2; public Image img_3; // Use this for initialization void Start () { //錨點(diǎn)為中心 img_1.transform.localPosition = Vector3.zero; //此時(shí)錨點(diǎn)在左上 img_2.rectTransform.anchoredPosition3D = Vector3.zero; //設(shè)置錨點(diǎn)為右上 img_3.GetComponent().anchorMax = Vector2.one; img_3.GetComponent().anchorMin = Vector2.one; img_3.rectTransform.anchoredPosition3D = Vector3.zero; } }

      下面1,2,3和上面代碼img_1,2,3一一對(duì)應(yīng),,,

      運(yùn)行效果圖:

      HTML

      版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。

      上一篇:物聯(lián)網(wǎng)標(biāo)準(zhǔn)、協(xié)議、技術(shù)術(shù)語(yǔ)快捷指南(二)
      下一篇:技術(shù)分享 | 測(cè)試人員必須掌握的測(cè)試用例
      相關(guān)文章
      亚洲福利在线观看| 亚洲VA成无码人在线观看天堂| 亚洲av鲁丝一区二区三区| 精品亚洲成α人无码成α在线观看 | 亚洲人成无码网站在线观看| 亚洲精品第一国产综合野| 亚洲乱码日产精品BD在线观看| 亚洲综合在线观看视频| 亚洲国产精品lv| 亚洲国产精品久久久久| 亚洲一区二区三区夜色| 亚洲一区二区三区高清| 久久久亚洲欧洲日产国码二区 | 亚洲伊人久久综合中文成人网| 亚洲乱码中文字幕综合| 丝袜熟女国偷自产中文字幕亚洲| 亚洲乱码中文字幕综合234| 亚洲av无码专区在线观看素人| 亚洲av日韩片在线观看| 亚洲国产免费综合| 亚洲综合最新无码专区| 一本色道久久综合亚洲精品| 亚洲人成中文字幕在线观看| 亚洲码国产精品高潮在线| 久久久亚洲精品视频| 久久久久久亚洲Av无码精品专口 | 国产精品亚洲精品| 亚洲日韩精品国产3区| 亚洲AV无码XXX麻豆艾秋| 青青青国产色视频在线观看国产亚洲欧洲国产综合 | 亚洲天堂一区二区| 亚洲成a人片在线网站| 亚洲国产91在线| 亚洲高清乱码午夜电影网| 日韩亚洲精品福利| 亚洲综合伊人久久大杳蕉| 亚洲AV成人片色在线观看高潮| 亚洲高清在线mv| 亚洲综合在线一区二区三区| 爱爱帝国亚洲一区二区三区| 亚洲综合国产精品第一页|