I do not understand how the field drawing and getting the values ​​entered by the user in Unity 3D work.

Here is 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 is not clear how this line works:

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

It seems to draw the field and return the value entered by the user. Explain, please.

    3 answers 3

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

    Logically, everything is simple to understand: in order to set the 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 , because through the inspector, the value in this field cannot be changed in any way. Which is logical, because OnGUI works several times a second, and in the field we can write EditorGUILayout.IntField("Number of clones:", 100500); , as a result, a field with a constant value is redrawn several times a second.

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

    • The value of the variable is entered in the field.
    • User changes field value
    • This value is passed back to the variable to:
    • After the next update of OnGUI value of this variable is again in the IntField Thus, we can influence the variable through the field. A field to display the actual value of the variable at the moment

    If you do not want the value to be variable through the field changed, just write EditorGUILayout.IntField("Number of clones:", clones); without clones =

    It may seem logical question, why not make the transfer of the link? But not all values ​​are written that way. Some do not return a value to a variable, so that the field remains just read-only and at the same time the value of the variable cannot be changed through the field.

    It may seem that developers would be more logical to do something like that EditorGUILayout.IntField("Number of clones:", clones, true/false); where true / false sets to pass by value to a link or not, but I think it’s harder logically to implement than just returning a value. Perhaps even for some reason.

    • Thanks for the answer! But meant as EditorGUILayout. IntField ("Number of clones:", clones) learns from what field it is necessary to take value. Those. how the unity understands from which control you need to take the value of clones, because I can write this: clones = EditorGUILayout.IntField ("Number of clones:", clones); clones1 = EditorGUILayout.IntField ("Number of clones:", clones1); etc. - Dmitry Trofimov
    • Well, it will be TWO completely different fields, one that takes the value from one variable and the other from the other. The fact that you wrote Number of clones both there and there does not mean that this is one field for two variables .. It's just an inscription. And if you write 100,500 times EditorGUILayout.IntField there will be 100,500 such fields, with reference to its value, at some level they are assigned their identifier and that's it ... as in css and html - Alexey Shimansky
    • It's clear. But when EditorGUILayout.IntField ("Number of clones:", clones) is called, how does unity pass the field identifier? Those. What does the field identify, when does the identification occur and how does it ping through multiple OnGUI calls? - Dmitry Trofimov
    • I do not understand what you want. Why do you need the internal logic of the unit? It is closed in the classrooms of libraries, so that you do not climbed there and did not think about it. If you think of this as a solution for your application, then apparently you are doing something wrong and want to get to that. If you want to get specifically intentionally straight to the viscera - cut everything through Reflection and look at the internal structure ....... what do you want to end up with? I do not understand - Alexey Shimansky
    • I just wonder how it works. I will try to understand through reflection. - Dmitry Trofimov

    OnGUI is executed several times per frame, depending on the event, whether it is drawing the control, or processing actions from the user. Therefore, this function that draws the controls in the window if you get to dig through Reflection will not look easy. Apparently from here and this kind of call that when handling different events, it should behave in different ways.

      Each call OnGUI Unity processes separately and creates a separate idspace for each call so that the control IDs are not confused. Each time EditorGuiLayout.IntField () is called, the control's identifier is obtained by calling GUIUtility.GetControlID (), which uniquely identifies controls by content, position, and sequence number. Unity :: GUIUtility.GetControlID Documentation