User interface Unity3d UI元素更改位置
我在Unity3d中制作了一个简单的应用程序,当按下一个按钮时,它会生成六个随机不同的数字。生成的数字只是画布上的六个UI文本。我的问题是,这些文本会根据屏幕的尺寸改变其位置: 这是我使用的脚本:User interface Unity3d UI元素更改位置,user-interface,unity3d,resolution,User Interface,Unity3d,Resolution,我在Unity3d中制作了一个简单的应用程序,当按下一个按钮时,它会生成六个随机不同的数字。生成的数字只是画布上的六个UI文本。我的问题是,这些文本会根据屏幕的尺寸改变其位置: 这是我使用的脚本: void OnGUI() { float rx = Screen.width / 1080.0f; float ry = Screen.height / 1920.0f; GUI.matrix = Matrix4x4.TRS(new Vector3(0,0,0),Quater
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.6beta
另外,很抱歉我的英语不好,我来自罗马尼亚。
问题与您的代码无关,而是与您的画布设置有关。选择作为这些UI对象父对象的UI画布,在那里您将自动添加名为Canvas Scaler的脚本,在那里您可以设置您希望您的UI如何对分辨率更改做出反应,尝试使用因子和缩放模式进行试验,直到得到满意的结果。
我认为,您需要根据屏幕大小设置脚本中所有NR的位置。如何设置位置?有没有一个内置的规则可以让我调整文本的位置?我认为如果它们是场景中的游戏对象,它们应该有变换。在这种情况下,transform.position=新矢量31.0f、2.0f、3.0f;。或者,您可以使用完全从脚本创建文本。感谢您的帮助:D