Camera reports wrong viewport size in edit mode

I’m writing some scripts that run at edit-time as well as runtime, and I’ve discovered that when running the game, the camera correctly reports the width and height, in pixels, of the game view. But when the game is stopped, any camera you check will report the width and height of the scene view instead of the game view. This leads to very unexpected behavior.

Is there any way to get the size of the game view viewport when the game is not running?

when the game is not in play mode it actually does not exist at all (the game is shut down).

so you get the editor camera size instead which is the then active camera view.

don’t know, at least for me thats the expected behavior as I want such information otherwise I wouldn’t use RunInEditMode

That doesn’t seem intuitive to me since depending on whether the game is running or not, the same value (the viewport size) has an entirely different meaning. And the cameras do exist when the game is not running, or else the game view wouldn’t be able to give an accurate preview of what the game will look like when running.

It would seem to be a shame if there’s no way to get the size of the game view and only the scene view. Somewhere inside Unity something has to know the size of the game view for it to draw it.

The game view really has no size as it can be changed dynamically and without being initialized its just 0.
would you prefer you crash the dx context completely by trying to initialize 0 sized stuff? :slight_smile:

The simple solution is to not make it run in edit mode if you only want the game window size.

For SM2, not running in edit mode is not an option. The developer needs a preview of what they’re working on. And for some features, the size of the area being rendered is needed.

There has to be a way, that information has to be there somewhere. If not as part of a camera component, then somewhere else.

Until you go into game mode its nowhere.
but that does not prevent you from detecting that you went into game mode and store the info.
then they will at least be valid until the user changes the size.

the info thats there is not available to you as you have no access to any layout related stuff as far as I’m aware.

on the iphone it would be simple. detect the orientation setting → ding

but as you see at latest by ipad your code will have to work differently as it has to handle the size at runtime not editor time

If you ask me you don’t need the screen size at all, as the orthographic cam size kind of defines the whole field, independent on if the screen is 10 pixel or 10 million pixel in size

Unless you’re using PixelPerfect.

Right

Thats why its required to cache the game mode screen size for the calculations

not much you can do about it without using undocumented functionality that potentially / likely even differs between 2.5 editor and 2.1 editor

the alternative is to let the user decide the “screen resolution” to render too so he can set it according the real game view post build