How to update Editor GUI fields from another method?
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?
Answer by goodguy · Jun 15, 2016 at 05:17 AM
Ahh.. googled so much, and found the solution right after I asked the question here. So, in case anyone stumbles upon the same issue, you just need to call Repaint() after you make any changes to force the window to update all its values
$$anonymous$$an, I love it when people return to their questions and share the solution when they come up with something. I appreciate it!