• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Oct 07, 2014 at 11:42 AM by fafase for the following reason:

The question is answered, right answer was accepted

avatar image
11
Question by roamcel · Oct 25, 2011 at 06:56 AM · editorgamewindowdimensions

Game window size from editor window in editor mode

Hi all,

there's several variants of t$$anonymous$$s question floating around, but t$$anonymous$$s one, I haven't found a solution nor even an idea of how to solve.

Basically, from an editor window, in editor mode, I have the need to retrieve the 'game window' current width and height. You might already know in fact that when you address screen.width or height from an editor window (the one created by an editor script), the values you receive are the dimensions of the actual editor window, and not of the game window!

I can't possibly t$$anonymous$$nk that t$$anonymous$$s issue can't be solved in editor mode, from an editor window, so please help me understand how to retrieve the game-window pixel dimensions in edit mode.

Note: for a series of design requisites, I can't simply set the aspect to standalone, and a predetermined X-Y value, and stick with that, basically because these necessarily vary from development seat to seat.

Thanks.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Demigiant · Nov 15, 2011 at 11:36 AM 0
Share

Would love to see an answer to this one :P

avatar image exawon · Dec 05, 2014 at 11:41 AM 2
Share

Why don't you use Handles.GetMainGameViewSize?

avatar image Wolfram · Dec 05, 2014 at 04:01 PM 1
Share

@exawon because that API interface was not available back in 2011 when this question was asked. Handles.GetMainGameViewSize was introduced in Unity 4.x, I believe.

1 Reply

  • Sort: 
avatar image
28
Best Answer

Answer by Bunny83 · Dec 07, 2011 at 12:52 PM

Unfortunately the GameView class is an internal class so the only way would be to use reflection to make your way down to the actual GameView window.

Here's a similar case on how to access internal classes via reflection:

http://answers.unity3d.com/questions/129694/can-you-turn-off-auto-keyframe-in-the-animation-wi.html

But be careful! Those classes are internal and they can change them at any time. It's somet$$anonymous$$ng you actually shouldn't use if you can avoid it.

edit

I've written a little helper function w$$anonymous$$ch will return the size of the main GameView:

// C#
public static Vector2 GetMainGameViewSize()
{
    System.Type T = System.Type.GetType("UnityEditor.GameView,UnityEditor");
    System.Reflection.MethodInfo GetSizeOfMainGameView = T.GetMethod("GetSizeOfMainGameView",System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
    System.Object Res = GetSizeOfMainGameView.Invoke(null,null);
    return (Vector2)Res;
}
Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image roamcel · Dec 07, 2011 at 03:54 PM 0
Share

This is a great solution, thanks a lot. Additional value in your heads up regarding the internal classes usage.

avatar image flamy · Jun 26, 2012 at 12:17 PM 0
Share

Hi @bunny83 is there a way through which i can set the resolution back..

avatar image Bunny83 · Jun 27, 2012 at 03:38 AM 1
Share

@flamy: Set it back? to what? I don't change the resolution. This will just determine the size of the first gameview. It's generally a bad idea to set the size of any editor window via code, since this will most likely undock the window and break your layout. If you want to do this because you have your window already undocked, you can set the size and position for the gameview the same way as for any other Editorwindow. All you need is a reference to the gameview:

untested:

 // C#
 public static EditorWindow GetMainGameView()
 {
     System.Type T = System.Type.GetType("UnityEditor.GameView,UnityEditor");
     System.Reflection.MethodInfo GetMainGameView = T.GetMethod("GetMainGameView",System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
     System.Object Res = GetMainGameView.Invoke(null,null);
     return (EditorWindow)Res;
 }

With this you should be able to use any Editorwindow properties like the position:

 Rect R = GetMainGameView().position;
 R.width = 800;
 R.height = 600;
 GetMainGameView().position = R;


Note that this will set the window's position, the actual view size could be a bit smaller due to the window border.

There is no build in function to set it to a certain resolution. There is the internal function GetGameViewRect which calculates the view size depending on the window size, but not reverse ;)

avatar image Wolfram · Jul 23, 2013 at 11:52 AM 0
Share

Thanks. This just helped me recovering from a Unity bug (seen in 3.5.7f6), where Application.CaptureScreenshot() will create shifted snapshots, leaving a black line at the bottom/left and cropping 1 pixel on the top/right. Application.CaptureScreenshot() only behaves correctly if the width is even and the height is odd (even if the Game window is larger than your capture size!), so I used your method to ensure that.

NOTE: for some reason, when assigning to GetMainGameView().position, you always need to subtract an offset of 5 from the Rect.y value you read from it. If you reassign the same value, the window moves around.

avatar image Wolfram · Dec 05, 2014 at 04:03 PM 1
Share

As @exawon noticed, with Unity 4.x you should be able to use the "official" API call Handles.GetMainGameViewSize, instead of this workaround. Didn't test it, though.

Show more comments

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Ask for Script Compilation from EditorWindow Button 1 Answer

Unity game window size 1 Answer

Accessing Editor Window from Game Code 0 Answers

Editor Script Reverts to Default State on Script Compilition 2 Answers

Type of Inspector 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges