2014-11-14 76 views
1

我在Unity3d中製作了一個簡單的應用程序,它可以在按下按鈕時生成六個隨機的不同數字。生成的數字只是六個UI畫面的子畫面。我的問題是,這些法規改變自己的立場,根據屏幕的尺寸:
Texts position in full screen mode.
Texts position in normal modeUnity3d UI元素更改位置

這是我使用的腳本:

void OnGUI() 
{ 
    float rx = Screen.width/1080.0f; 
    float ry = Screen.height/1920.0f; 
    GUI.matrix = Matrix4x4.TRS(new Vector3(0,0,0),Quaternion.identity,new Vector3(rx,ry,1)); 

    GUI.skin = GameSkin; 

    if (GUI.Button (GenerateRect, "GENERATE")) 
        GenerateNumbers(); 
} 

private void GenerateNumbers() 
{ 
    int n; 
     for(int i = 1;i<=6;i++) 
     { 
      n = UnityEngine.Random.Range (1, 100); 
      while(nr.Contains(n)) /this while loop ensure that the numbers are different 
       n = UnityEngine.Random.Range (1, 100); 
      nr.Add (n); // here nr is a list<int> that contains the numbers. 
     } 

     Nr1.text = nr[0].ToString(); 
     Nr2.text = nr[1].ToString(); 
     Nr3.text = nr[2].ToString(); 
     Nr4.text = nr[3].ToString(); 
     Nr5.text = nr[4].ToString(); 
     Nr6.text = nr[5].ToString(); 
     nr.Clear(); 

    } 
    StartCoroutine ("ShowNumbers"); //the ShowNumbers coroutine show the numbers. 
} 

我使用Unity 4.6 (beta) PS對不起英語不好,我來自羅馬尼亞。

+0

我認爲,您需要根據屏幕大小設置腳本中所有Nrs的位置。 – maZZZu 2014-11-14 21:36:34

+0

我如何設置位置?是否有內置的禮儀可以讓我調整文本的位置? – user3765498 2014-11-14 21:42:02

+0

我認爲如果它們是場景中的gameObjects,它們應該進行轉換。在這種情況下'transform.postion = new Vector3(1.0f,2.0f,3.0f);'。或者,您可以使用[GUI.Box](http://docs.unity3d.com/ScriptReference/GUI.Box.html)從腳本完全創建文本。 – maZZZu 2014-11-14 22:08:04

回答

0

該問題與您的代碼無關,但與畫布設置無關。選擇這些UI對象的父UI畫布,您將自動添加名爲「畫布縮放器」的腳本,您可以在其中設置如何讓用戶界面對分辨率變化做出反應,嘗試試驗因素並縮放模式,直到你會得到滿意的結果。