简体   繁体   English

如何在编辑窗口脚本的 OnGui 中创建一个 int 字段?

[英]How can I create a int field in OnGui in editorwindow script?

private void OnGUI()
    {
        if(GUILayout.Button("Test"))
        {
           testTarget = EditorGUILayout.IntField("Testing : ", TestTarget());
        }
    }

I see only the button but not the int field inside.我只看到按钮而不是里面的 int 字段。

testTarget is a static int variable and TestTarget is just a simple method that returns an int number. testTarget 是一个 static int 变量,而 TestTarget 只是一个返回 int 数字的简单方法。

I don't quite understand what you are trying to achieve.. if you put the int field within the button if case... well then it will be drawn only exactly in the one frame where you click the button... you most probably want the int field drawn always so either before or after the button draw.我不太明白你想要达到的目标.. 如果你把 int 字段放在按钮中,如果是的话......那么它只会精确地在你点击按钮的一帧中绘制......你最可能希望在按钮绘制之前或之后始终绘制 int 字段。

What I guess you want to do is something like我猜你想做的是像

private int? tempTestTarget;

private void OnGUI()
{
    // Draw the intfield above the button
    // store the value in a temporary field
    // Maybe only the first time get the value from the method
    tempTestTarget = EditorGUILayout.IntField("Testing : ", tempTestTarget.HasValue ? tempTestTarget.Value : TestTarget());

    if(GUILayout.Button("Test"))
    {
       // only on click assign the value to the actual target field
       testTarget = tempTestTarget.Value;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM