2012年7月3日 星期二

UNITY3D - 自動調整GUITexture比例


開發手機、平板電腦遊戲最怕就是各廠商尺寸解析度不同,造成UI被破壞。
若是本來就在OnGUI做UI處理的可以在該程式中處理比例問題,但如果有使用到大量GUITexture,將以下程式附加在EmptyObject上即可將問題處理。

/*******************************************************/
C#
/*******************************************************/
public Vector2 oriScale = new Vector2(1024.0f, 768.0f);

void Start () {
GUITexture[] guis = FindObjectsOfType(typeof(GUITexture)) as GUITexture[];
Vector2 size = new Vector2();
size.x = Screen.width / oriScale.x;
size.y = Screen.height / oriScale.y;
foreach(GUITexture gui in guis)
{
gui.pixelInset = new Rect(gui.pixelInset.x * size.x, gui.pixelInset.y * size.y, gui.pixelInset.width * size.x, gui.pixelInset.height * size.y);
}
}



/*******************************************************/
Javascript
/*******************************************************/


public var oriScale : Vector2 = Vector2(1024.0f, 768.0f);

function Start () {
var guis : GUITexture[] = FindObjectsOfType(typeof(GUITexture));
var size : Vector2;
size.x = Screen.width / 1024.0f;
size.y = Screen.height / 768.0f;
for(var gui : GUITexture in guis)
{
gui.pixelInset = Rect(gui.pixelInset.x * size.x, gui.pixelInset.y * size.y, gui.pixelInset.width * size.x, gui.pixelInset.height * size.y);
}
}


1 則留言:

  1. 不,這裡的public var oriScale : Vector2 = Vector2(1024.0f, 768.0f);是指原本設定時是以1024X768的比例去作設定,但我們實際執行的裝置可能不是用1024X768執行,因此用這段程式可以讓GUITexture做自動縮放到適合目前的大小及位置。

    回覆刪除