C# unity 3d OnGUI循环
我有一个小问题,我在做计算器,我想通过写循环来减少代码长度,对于数字部分,我解决了(“OnGUI”)部分的无限循环问题,但现在它没有显示我的任何数字,有人能向我解释为什么吗? 多谢各位C# unity 3d OnGUI循环,c#,unity3d,C#,Unity3d,我有一个小问题,我在做计算器,我想通过写循环来减少代码长度,对于数字部分,我解决了(“OnGUI”)部分的无限循环问题,但现在它没有显示我的任何数字,有人能向我解释为什么吗? 多谢各位 using UnityEngine; using System.Collections; public class Calculator : MonoBehaviour { int temp,rectX,t,count; bool endOfCalc; string val;
using UnityEngine;
using System.Collections;
public class Calculator : MonoBehaviour {
int temp,rectX,t,count;
bool endOfCalc;
string val;
private void Start()
{
endOfCalc = false;
val = "";
temp = 0;
t = 9;
count = 0;
/*for(int x = 0; x <= 9; x++)
{
rectX += 20;
Debug.Log (rectX);
if (GUI.Button (new Rect(10,160+rectX,30,20), x.ToString ()))
{
Calculation(x.ToString ());
}
}*/
}
private void OnGUI()
{
val = GUI.TextField (new Rect(10,100,200,20), val);
if (GUI.Button (new Rect(40,120,30,20), "+"))
{
temp += int.Parse (val);
val = "";
}
if (GUI.Button (new Rect(10,120,30,20),"="))
{
temp += int.Parse (val);
val = temp.ToString ();
endOfCalc = true;
}
// The problem is here, i can't see any buttons.
for(int x= 0; x<=t; t--)
{
if (GUI.Button (new Rect(10,140,30,20), count.ToString()))
{
Calculation(count.ToString ());
}
count++;
}
}
void Calculation(string str)
{
if (!endOfCalc)
val += str;
else
val = "";
val += str;
endOfCalc = false;
temp = 0;
}
}
使用UnityEngine;
使用系统集合;
公共类计算器:单行为{
int-temp、rectX、t、count;
bool-endOfCalc;
字符串val;
私有void Start()
{
endOfCalc=false;
val=”“;
温度=0;
t=9;
计数=0;
/*对于(int x=0;x
[更新]
我刚刚尝试了以下代码:
void OnGUI () {
int offset = 0;
for (int x = 0; x <= 9; x++) {
if (GUI.Button (new Rect (10 + offset, 140, 30, 20), x.ToString ())) {
Debug.Log ("Pressed: " + x);
}
offset += 35;
}
}
[更新]
我刚刚尝试了以下代码:
void OnGUI () {
int offset = 0;
for (int x = 0; x <= 9; x++) {
if (GUI.Button (new Rect (10 + offset, 140, 30, 20), x.ToString ())) {
Debug.Log ("Pressed: " + x);
}
offset += 35;
}
}
当我查看您的代码时,我会说您只看到一个按钮,其他所有按钮都在该按钮下,因为您总是将它们放在完全相同的位置:
if (GUI.Button (new Rect(10,140,30,20), count.ToString()))
{
Calculation(count.ToString ());
}
你必须在每一轮改变位置,类似这样:
if (GUI.Button (new Rect(10,30 * t + 140,30,20), count.ToString()))
{
Calculation(count.ToString ());
}
当我查看您的代码时,我会说您只看到一个按钮,其他所有按钮都在该按钮下,因为您总是将它们放在完全相同的位置:
if (GUI.Button (new Rect(10,140,30,20), count.ToString()))
{
Calculation(count.ToString ());
}
你必须在每一轮改变位置,类似这样:
if (GUI.Button (new Rect(10,30 * t + 140,30,20), count.ToString()))
{
Calculation(count.ToString ());
}
看,这是一个有点复杂的问题,在OnGUI类中,如果我们为(int x=0;x写这是一个有点复杂的问题,在OnGUI类中,如果我们为(int x=0;x)写这是一个按钮是的,我知道我以后会修复它,现在需要修复这个部分,但首先我使用了for(int x=0;x自己动手,制作debug.log(x);它将永远消失:)OnGui
是正确的位置。只需x++而不是t-。请参阅我关于count
的更新答案。我假设t是const=9I,我将再试一次,但我认为循环将永远运行。K等一下。是的,问题是相同的。t
始终是9,如果没有其他代码操作它。因此,您可以将其声明为const int t=9或用硬编码的
9替换它。因此我非常确定a
for(int x=0;x按钮是的,我知道我以后会修复它,现在需要修复此部分,但首先我使用了for(int x=0;x自己动手,使debug.log(x);它将永远存在:)OnGui
是正确的位置。只需x++而不是t-。请参阅我关于count
的更新答案。我假设t是const=9I,我将再试一次,但我认为循环将永远运行。K等一下。是的,问题是相同的。t
始终是9,如果没有其他代码操作它。因此,您可以将其声明为const int t=9,或者用硬编码的
9替换它。所以我很确定(int x=0;x我知道,问题是我根本看不到任何按钮,因为OnGUI中的循环将无限次,所以我一直到它达到零来修复这个部分,但当我按run online mode时,我甚至看不到一个按钮。有人能解释为什么吗?我知道,问题是我根本看不到任何按钮,因为loOnGUI中的op将无限次运行,所以我将其设置为零以修复此部分,但当我按run online mode时,我甚至看不到一个按钮。有人能解释为什么吗?
if (GUI.Button (new Rect(10,30 * t + 140,30,20), count.ToString()))
{
Calculation(count.ToString ());
}