Get value from dynamically created EditorGUILayout.IntField

I am writing an editor script, and part of it is to dynamically create a number of EditorGUILayout.IntField which are to receive user input. The fields are dynamically created in a loop based on properties of another object so I don’t know beforehand how many fields will be created. This is all working fine, but I’m struggling to find any documentation or examples on how to get the values of these dynamically created fields or reference them in any way .

Below is an idea of the code I’m using:

foreach(...){
    EditorGUILayout.IntField(0); // dynamically create IntField
}

Any suggestion on how I get the value of these fields? Or is there some kind of event handler for when the fields value changes? I want to keep the values of these fields in a list, is there some way to expose the list value as an IntField? Or do I need to name them somehow and reference them by name? The docs don’t have any details on anything like this

hmmm… this actually looks like an error in the docs… this line:

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

should in fact be this line…

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

for it to work as they claim it does…

As to your question, I think you do know how to get the values from the properties of that other object and you are struggling in how to display them in your intfield and update them whenever the value changes? If not, this piece will probably not help and I didn’t understand you…

The way the legacy GUI system and the editor GUI system (which uses the legacy GUI system) works is that OnGUI or OnInspectorGUI is called multiple times every frame with different events (it is called once per frame for layout, once per frame for repaint, and perhap a few extra times for events like mousedown,mouseup, keydown etc.). So it’s not a one time creating of UI elements, this piece of code get called by Unity a lot of times as if it was in an update loop on steroids :wink: to get the current type you can check [Event][1].[current][2].[type][3]

EditorGUILayout.IntField returns the changed value (or the same if it didn’t change.

so in order to answer your question, to get the changed value, you simply say:

for(int i = 0; i < myList.Length; i++)
{
    myList _= EditorGUILayout.IntField("Number " + i, myList*);*_

}
Here you set myList to the result of the IntField.
If you don’t want to set the property every time because you do something in the setter, you could use EditorGUI.[BeginchangeCheck][4]/EndChangeCheck like this:
EditorGUI.BeginChangeCheck();
int tempResult = EditorGUILayout.IntField("Number " + i, myList*);*
if (EditorGUI.EndChangeCheck())
{
myList = tempResult;
}
*[1]: http://docs.unity3d.com/ScriptReference/Event.html*_
[2]: http://docs.unity3d.com/ScriptReference/Event-current.html*_
_[3]: http://docs.unity3d.com/ScriptReference/Event-type.html*_

_*[4]: http://docs.unity3d.com/ScriptReference/EditorGUI.BeginChangeCheck.html*_