Hi!
After almost 7 years of AS3 and Java programming Unity’s GUI system almost got me stuck, and I couldn’t manage to google the solution for this. I want to make a little extension for the editor which will make gameplay changes persist after I quit the play mode. I know there are some solutions like this on the asset store, but I want to learn how to do them myself.
So, I’ve created a window and added GUI elements like IntFields, Buttons and other stuff. And I have one IntField which is intended to display the number of objects selected in the scene.
The basic code is like this:
void OnGUI() {
EditorGUILayout.IntField("Num Selected Objects", m_numSelected);
}
void OnSelectionChange() {
Object[] selectedObjects = Selection.GetFiltered(typeof(GameObject), SelectionMode.Unfiltered);
m_numSelected = selectedObjects.Length;
}
The problem is that EditorGUILayout.IntField does not return any reference value. It returns int, so I can’t store the reference to this field and update its value somewhere else.
If I change m_numSelected inside OnGUI() method, the field updates just like it’s supposed to, but if I wanna do it from OnSelectionChange(), nothing happens to the field, although the variable m_numSelected is updated for sure.
Could you explain me how it works? How can I update the field from other places?