Screen,height reporting editor game window height

As per title. Screen.height and Screen.width report the dimensions of the Game window in the editor which contains the running game, not the dimensions of the area the game is actually playing in.

This occurs if you use Screen.width/height from an editor script. This makes some sense; however, it presents a problem for me.

The problem is that while running in the editor I want to be able to change values via my custom Inspector and have these changes affect the game. The problem is that when I make a change I need to tell my game to recalculate various things. These things are dependant on the Screen dimensions.

One work around I’ve found has been to make the recalculation method a coroutine which yields immediately when called for one frame and then executes normally the next frame. If I do this when the coroutine executes the Screen dimensions are correct. I assume that after the editor classes have been executed the Screen dimensions get changed to the actual game dimensions for the non-editor behaviours. There are multiple different methods and scenarios that mean creating coroutine versions of all my methods is not a great solution.

Another option I don’t want to use is to poll all the variables of my component in one of its Update methods so that I can invoke the recalculation from there.

Anyone got any other ideas? Is there a way of getting the actual game window dimensions rather than the containing window during OnInspectorGUI?

Sorry for the necro post, but I have a very similar issue. Almost 7 years to the day.

Anyone one has a workaround?

Thanks for any help

I think if you have a screen-space canvas, that stores the height/width no? Perhaps you can get it from the canvases dimensions.

At the moment, I’ve just kept the result of width/height in two static variable at Awake. That’s working. But maybe there’s a less ugly trick.

I’m replying to this post for the people who may have the same problem and come across this post.

As OP stated, using ‘Screen.width/height’ inside ‘OnInspectorGUI()’ function will leave you with a size of ‘Inspector’, not ‘Game’ (current resolution). If you want the latter, then you will have to invoke them somewhere else.

Example:

[CustomEditor(typeof(pseudoScript)), CanEditMultipleObjects]
public class pseudoScriptEditor : Editor
{
    float width, height;

    void OnEnable(){
        width = Screen.width;
        height = Screen.height;
    }

    public override void OnInspectorGUI(){
        ...
        if (GUILayout.Button("Log", GUILayout.MaxWidth(50f))){
            Debug.log(width);
            Debug.log(height);
        }
        ...
    }
}

Now you have it. If not, try to select a different GameObject momentarily and then try again.