2014年8月24日 星期日

手機/平版的平台上,OnGUI對應螢幕解析度做縮放的方式

方法1:

//建議不要在Awake函式做,因為Awake太早被呼叫,此時螢幕解析度可能有誤.
void Start()
{
  //宣告一個Vector3的參數,將X、Y被縮放的比例填入,Z不會縮放所以填1就好.
  //1280x800為原始設計螢幕大小,Screen.widthxScreen.height為目前螢幕真實大小.
  GUIScaleValue = new Vector3((float)Screen.width/1280.0f, (float)Screen.height/800.0f, 1);
}

void OnGUI()
{
  //scale to fit, when screen change.
  GUI.matrix = Matrix4x4.Scale(GUIScaleValue);
}



方法2:

//建議不要在Awake函式做,因為Awake太早被呼叫,此時螢幕解析度可能有誤.
void Start()
{
  //1280x800為原始設計螢幕大小,Screen.widthxScreen.height為目前螢幕真實大小.
  pivotScale = new Vector2((float)Screen.width/1280.0f, (float)Screen.height/800.0f);
  pivotPoint = new Vector2(Screen.width/2, Screen.height/2);
}

void OnGUI()
{
  //scale to fit, when screen change.
  GUIUtility.ScaleAroundPivot(pivotScale,Vector2.zero);
  //也可以直接旋轉整個UI,底下例子:以pivotPoint為原點旋轉90度.
  GUIUtility.RotateAroundPivot(90,pivotPoint);
}

沒有留言:

張貼留言