Unity3D的UGUI布局錨點自動繫結關係

2022-06-01 18:48:10 字數 1375 閱讀 4482

[menuitem("context/recttransform/auto")]

public static void autorectanior()

bounds rectbounds = recttransformutility.calculaterelativerecttransformbounds(canvas, rect);//計算src的包圍盒

bounds parentbounds = recttransformutility.calculaterelativerecttransformbounds(canvas, parent);//計算tar的包圍盒

float cminx = 0.5f, cminy = 0.5f, cmaxx = 0.5f, cmaxy = 0.5f;

//獲取設定ui的寬和高

float cwidth = rectbounds.size.x; * rect.localscale.x;

float chight = rectbounds.size.y; * rect.localscale.y;

//獲取設定ui父級的寬和高

float pwidth = parentbounds.size.x; * parent.localscale.x;

float phight = parentbounds.size.y; * parent.localscale.y;

//重新計算錨點位置

cminx = (pwidth / 2 - (cwidth * rect.pivot.x - rect.anchoredposition.x)) / pwidth;

cminy = (phight / 2 - (chight * rect.pivot.y - rect.anchoredposition.y)) / phight;

cmaxx = (pwidth / 2 + (cwidth * (1 - rect.pivot.x) + rect.anchoredposition.x)) / pwidth;

cmaxy = (phight / 2 + (chight * (1 - rect.pivot.y) + rect.anchoredposition.y)) / phight;

//重新設定ui的錨點位置

rect.anchormin = new vector2(cminx, cminy);

rect.anchormax = new vector2(cmaxx, cmaxy);

//設定ui的相對距離

rect.offsetmax = new vector2(0, 0);

rect.offsetmin = new vector2(0, 0);

debug.log("自適應錨點成功!");

}

上述繫結**未完善。。。後續完善

Unity3D種UGUI與NGUI的對比差別(1)

先來段小八卦,聽說ugui的主創人員是從ngui招過去的,所以,ugui中有很多概念,對於用過ngui的童鞋來說,看起來都似曾相識。先來個概念對比 ngui ugui 錨點anchor recttransform anchor sprite image 文字label text 根節點uiroot ...

Unity3D開發技巧之UGUI虛擬搖桿

首先我們建立兩個image,讓搖桿stick為joystick的子物體,將彼此的相對大小調整好。建立指令碼virtualjoystick.cs繼承scrollrect。using system.collections using system.collections.generic using un...

Unity3D開發(九) Unity3d流光效果

遊戲開 壇 hello game 遊戲開發群 201276069 之前曾經注意過material 中紋理的屬性都有 tiling 和offset 但沒有深究過其用途,今天才知道竟然可以利用 offset做uv 動畫,從而完成各種有趣的動畫,比如流光效果!流過效果即通常一條高光光在物體上劃過,模擬高光...