GUI框值未更新

时间:2016-01-06 16:14:08

标签: c# unity3d

大家好我有一个栏,我显示一个因用户输入而改变的变量,它位于此脚本的OnGUI()上:

public class StateManager : MonoBehaviour 
{
    public Vector2 pos1 = new Vector2(10,200);
    public Vector2 size = new Vector2(60,20);
    private HydroElectric ec;
    public Texture2D emptyProd;
    public Texture2D fullProd;

    public StateManager()
    {
        ec = new HydroElectric();
    }

    void OnGUI()
    {
        float tt;
        tt = (ec.HydroControlPanel () / 6 );

        GUI.BeginGroup (new Rect (pos1.x, pos1.y, size.x, size.y));
        GUI.Box (new Rect (0, 0, size.x, size.y), emptyProd,GUIStyle.none);
        GUI.BeginGroup (new Rect (0, 0, size.x *tt , size.y));
        GUI.Box (new Rect (0, 0, size.x, size.y), fullProd, GUIStyle.none);
        GUI.EndGroup ();
        GUI.EndGroup ();

    }
}

此变量可以从0 o 6开始,因此我将其除以得到0到1的比例以显示条形。我用dot sintax调用的值来自:

public float HydroControlPanel ()
        {
            turbina1 = t1Bool ? 2 : 0;
            turbina2 = t2Bool ? 3 : 0;
            turbina3 = t3Bool ? 1 : 0;

            prod = turbina1 + turbina2 + turbina3;
            return prod;
        }

Bool使用切换按钮进行更改,因为在其他脚本中,我将此值显示为:

Public void ShowIt()
        {
            ec.t1Bool = GUI.Toggle (new Rect (25, 55, 100, 50), ec.t1Bool, "Turbina 2 MW");

            ec.t2Bool = GUI.Toggle (new Rect (25, 95, 100, 50), ec.t2Bool, "Turbina 3 MW");

            ec.t3Bool = GUI.Toggle (new Rect (25, 135, 100, 50), ec.t3Bool, "Turbina 1 MW");

            GUI.Box (new Rect (Screen.width - 100, 60, 80, 25), ec.HydroControlPanel().ToString ());      // PRODUCED ENERGY

        }

如何让我的酒吧更新?它开始显示默认值,但在此之后永远不会更改。

1 个答案:

答案 0 :(得分:1)

问题出在这一行

tt = (ec.HydroControlPanel () / 6 );

您将float除以整数,因此结果将始终为整数。你应该按浮动划分:

tt = (ec.HydroControlPanel () / 6f );