Last Updated: November 19, 2020
·
4.714K
· catchco

Refreshing the Unity3d GameView Programatically

Hopefully this quick tip will help you avoid the suffering inflicted upon me. I spent a few frustrating hours last night trying different ways to update the GameViews and SceneViews from a custom EditorWindow. I'll explain things in reverse.

First, the solution

UnityEditorInternal.InternalEditorUtility.RepaintAllViews();

What was my problem in the first place?

Well I had a custom EditorWindow that updated some GameObjects in OnGUI(). The updated objects updated were adorned with the [ExecuteInEditMode] attribute. However, anything that should ExecuteInEditMode only does so if a SceneView window has focus. Because my custom editor window had focus no object was updating. In order to see the result of the changes one had to click on a scene view. Frustrating...

At the end of the day, RepaintAllViews() was not needed but it was a nice find along the way. Perhaps I'll explain why in a future post. :]

Related protips:

Improved PlayerPrefs for Unity