1

i have a small problem i am making calculator and i want to decrease my code lenght by writting for loop, for the number part, i solved problem for infinity loop on ("OnGUI") part, but now it dosen't show my any numbers, can someone explain to me why is that? Thank you.

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;
    }
}
4

3 回答 3

1
for(int x= 0; x<=t; t--)

我认为这必须是:

for(int x= 0; x<=t; x++)

OnGui每帧至少调用一次。在当前的实现中,t 在第一次调用期间递减到 -1,并且将保持 -1,因为我看不到任何其他将它设置回 9 的地方。

另一点是Rect:所有按钮都显示在同一位置。rectX注释掉的偏移方法Start似乎是您需要的。但rectX由于 Rect 的构造函数,实际上是一个 rectY。

我认为您避免count这样做,因为它似乎未初始化:

for(int x= 0; x<=t; x++) { 
    if (GUI.Button (new Rect(10,140,30,20), x.ToString())) {
        Calculation(x.ToString ());
    }
}

[更新]

我刚刚尝试了以下代码:

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;
    }       
}

并得到:
for循环创建的按钮

还有一些日志输出,例如:

Pressed: 4
UnityEngine.Debug:Log(Object)
MenuController:OnGUI() (at Assets/Scripts/Menu/MenuController.cs:53)
于 2013-06-10T11:09:30.393 回答
0

看这是一个有点复杂的事情,在 OnGUI 类中如果写for (int x = 0; x<= 9; x++)我们会遇到麻烦,因为在 OnGUI 中每一帧这个类都会被调用两次,所以每帧两次 x 将 == 0 并且会从 0 循环到 9 就是这样一个问题,因为我只需要循环一次,我可以使用 start 函数,但是我不知道如何调用 aGUI.Button...所以这是另一个问题,所以我欺骗了系统并使用了从 9 到 0 的计数并修复了无限循环,但是当我修复无限循环时问题不存在,我在那个循环中写的例子:GUI.button它没有出现,我需要解释为什么会发生这种情况,感谢阅读。:) 感谢您的回答。

于 2013-06-10T11:26:46.790 回答
0

当我查看您的代码时,我会说您只看到一个按钮,而所有其他按钮都在该按钮下,因为您总是将它们放在完全相同的位置:

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 ());
}
于 2013-06-10T11:12:24.053 回答