in unity

Unity: Get Game View Resolution

Get Game View Resolution Unity

Update:  You just need to use UnityEditor.Handles.GetMainGameViewSize() and don’t need any reflection at all (even though it still might be useful for something else). Thanks to Zanleo for posting an easier option in comments!

 

 

Original Article:

The game that I’m currently developing should switch spritesheets for different device sizes.

I have 3 of them:
SD for iPhone 3GS
HD for iPhone 4(S), iPhone 5(S,C) and iPad 1 (non-retina)
UD (Ultra HD) for iPad’s with retina.

And I have a script that switches between them by detecting current device resolution.

Problem

Everything works well when I test on device, but when I test from Unity editor the Screen class always returns resolution of my desktop display (1920×1080), no matter which value I set in the GameView.

Get Game View Resolution in Unity Editor

As you can see on the screenshot, I’ve set my resolution to iPhone 3Gs (480×320) and would like to get that resolution (480 x 320) in the code, so that I could test loading and switching between spritesheets.

Solution

Good news that we can use Reflection to get this value. And here is a code to do this:

The code to take the game view I found in this answer.

This way we can set the resolution we want to test in in the Unity Editor (e.g. 960×480 to make the code load iPhone 4 resources) and test that the code picks correct resources and adjusts everything just as if it was running on device with target resolution.

Hope this helps someone.

Write a Comment

Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  1. Great Solution! It helps me a lot to test Bundle download and screen size on my project! Thank you!

    • Thanks! That’s actually a better way of doing it. I’m going to add it to the article, even though I’m surprised to still get any attention to this article more than 4 years later 🙂