javascript – How does EditorWindow.OnGUI () work in Unity 3D?

Question:

I don't understand how the rendering of fields and getting the values ​​entered by the user in Unity 3D works.

Here's an example from the documentation:

    // Editor Script that clones the selected GameObject a number of times.

    class EditorGUILayoutIntField extends EditorWindow {

        var clones : int = 1;

        @MenuItem("Examples/Clone Object")
        static function Init() {
            var window = GetWindow(EditorGUILayoutIntField);
            window.Show();
        }

        function OnGUI() {
            clones = EditorGUILayout.IntField("Number of clones:", clones);
            if(GUILayout.Button("Clone!"))
                for(var i = 0; i < clones; i++)
                    Instantiate(Selection.activeGameObject, Vector3.zero, Quaternion.identity);
        }
    }

It's not clear how this line works:

clones = EditorGUILayout.IntField("Number of clones:", clones);

It kind of renders the field and returns the value entered by the user. Can you explain please.

Answer:

Yes, similar fields in the EditorGUI return the value entered by the user.

Logically, everything is simple to understand: in order to set a value to a field, it is enough for it to transfer some value from a variable or write it manually. However, in this case, in fact, this field will be read-only , since the value in this field cannot be changed in any way through the inspector. Which is logical, because OnGUI works several times per second, and in the field we can write EditorGUILayout.IntField("Number of clones:", 100500); , as a result of which a field with a constant value is redrawn several times per second.

Why write like this: clones = EditorGUILayout.IntField("Number of clones:", clones); ? To do, so to speak, a message between the variable and the value entered by the user. It turns out a kind of endless circle:

  • The value of the variable is entered into the field.
  • User changes the value in the field
  • This value is passed back to the variable so that:
  • After the next update of OnGUI value of this same variable got into IntField again IntField Thus, we can influence the variable through the field. And the field displays the real value of the variable at the moment

If you don't want the value of a variable to change through a field, just write EditorGUILayout.IntField("Number of clones:", clones); without clones =

It might seem logical to ask why not transfer by reference? But not all meanings are written this way. Some do not return a value to a variable, so that the field remains just read-only and the value of the variable cannot be changed through the field.

It might seem that it would be more logical for developers to do something like that EditorGUILayout.IntField("Number of clones:", clones, true/false); where true / false specifies whether to pass by value by reference or not, but I think this is more logically difficult to implement than just returning a value. Perhaps for some other reason.

Scroll to Top