- Home /
How to have GUI Elements formatted by matrix display correctly in Unity Game View?
So I am using a GUI.matrix to format the position and size of all my GUI buttons, textures, text, etc.
It is working really well and seems to work for all the devices tested so far.
Here is an example of the code in case it helps you answer my question:
//screen dimensions used to format Gui.matrix
float native_width = 1280;
float native_height = 720;
//variables used in matrix
float rx;
float ry;
//button size
Vector2 vectorSize = new Vector2(120, 120);
//button position
Vector2 laserBeamVectorPosition = new Vector2 (200, 560);
void Start()
{
//set matrix formatting variables
rx = Screen.width/ native_width;
ry = Screen.height / native_height;
//format button sizes
vectorSize.x = vectorSize.x * (Screen.width/native_width);
vectorSize.y = vectorSize.y * (Screen.height/native_height);
//format button positions
laserBeamVectorPosition.x = laserBeamVectorPosition.x * (Screen.width/native_width);
laserBeamVectorPosition.y = laserBeamVectorPosition.y * (Screen.width/native_width);
}
void OnGUI()
{
//use matrix
GUI.matrix = Matrix4x4.TRS (new Vector3(0, 0, 0), Quaternion.identity, new Vector3 (rx, ry, 1));
//position button with size, position, 2D Texture, GUIStyle
if (GUI.Button (new Rect (laserBeamVectorPosition.x, laserBeamVectorPosition.y, vectorSize.x, vectorSize.y), laserBeamTexture, GUIStyleButton))
{
//button functionality
}
}
So like I said everything seems to be working correctly on PC or Android devices. Not all the GUI components require this much code to set up, but I try to be exact with buttons.
THE PROBLEM: Anyways the problem is that even though the code seems to work correctly on devices it does not work correctly in the Unity Editor when using Game View.
You can stretch the screen around in the editor and the buttons will change positions when you press Play. I have tried "Free Aspect" and pretty much every single resolution setting including my Build Setting ones 1280x720.
I'm guessing that for some reason the GUI.Matrix stuff does not work correctly in the Unity Editor?
But I am not sure about if I am doing something wrong? If there is any way to fix this?
The same problem seems to occur on my Android Unity Remote App.
So essentially I cannot test my game easily unless I build it to PC or my Android Device. The buttons being in the wrong spot in Editor mode really mess everything up since I use raycasts and button positions to move the player, the buttons have to be in the exact right spot or pressing a button may still activate the movement command (like ray shoots through button) instead of being halted by conditions.
The problem is even worst because my build works for PC and Android so the builds take longer to test especially with Android. I also play videos in the project which seems like PC and Android are not compatible so my:
if UNITY_ANDROID
endif
stuff seems to require at least 2 builds when transitioning from PC to Android or vice versa.
If there is no way to fix this for Unity Editor I feel that this is a huge flaw in the program. Having inaccurate displays is not that huge of a deal for just testing for a day or two. But over the lifetime of a project having to build repeatedly to accurately test a game instead of being able to use the Unity Editor Game View is a HUGE PROBLEM and wastes countless hours.
Does anyone have a solution for this, or is this a Unity bug?
Bump, still really want to find a way to test my game accurately in Unity Editor while using GUI.matrix to format it. It's super time consu$$anonymous$$g having to test it on PC or Android Builds when working on trying to get exact positions for new GUI components or even just general game play testing.
There's got to be a solution for this?!
I did sort out the problem forcing me build more than once to switch from PC to Android or vice-versa. It turns out using:
if UNITY_EDITOR
Was causing the problems so it seems I cannot use that for some reason only:
if UNITY_STANDALONE_WIN
if UNITY_ANDROID
I'm wondering if I somehow was able to use:
if UNITY_EDITOR
for all OnGUI() functions would I be able to test the game accurately in the Editor? I kind of doubt it since it seems like the Editor just tries to emulate whatever current Build you are using (PC/Android/iOS/etc).
So I'm not really sure what's even the point of using #if UNITY_EDITOR but it seems to not work in my project without multiple compilations and no apparent benefits.
I'm not sure if using a $$anonymous$$atrix to format is uncommon or if everyone has these same GUI issues while using Unity Editor? But this is the single greatest problem I have right now for testing my game and it slows me down quite a bit, hopefully someone can answer this.
Answer by RyanZimmerman87 · Aug 18, 2013 at 12:17 AM
Wow so after reading your message Ben Jarnell it sounded like things should work as long as everything was correct on Start().
So I tried something I have apparently never done before... and now I feel really stupid for never trying this previously :)
So I had tried seemingly every single resolution or aspect ratio possible previously, but I always tried to use Game View with "Maximize on Play" disabled. I didn't really like maximize on play because it obviously made it difficult to switch between Game View and Scene View.
I had tried using "Maximize on Play" previously but I guess I always tried using "Free Aspect" which I mistakenly thought would format to the correct project resolution based on my scripts.
So just now for the first time after reading your message I finally tried using Standalone (1280x720) in Game View while ALSO having it set to "Maximize on Play".
BINGO. It now works!!!
I never really liked "Maximize on Play" before but I guess this was a huge mistake because it was causing all my attempts to get this working to fail. I also have a duel screen monitor set up so I could have easily just had the 1280x720 Game view on my other monitor and also solved my problem with not being able to switch back and forth from game to scene view.
So I guess the solution is you need to have a duel screen monitor set up if you want to be able to use Game View with the correct resolution (assuming it won't fit in the Editor correctly without "Maximize on Play".
I think this duel monitor setup is necessary for efficient testing if you want to have access to scene and game view simultaneously and still maintain the correct GUI format.
So hopefully this will help others who have had this problem. I'm not sure if I'm the only one who somehow never realized this from the start ^^ But I sure do feel stupid all the trouble I've been going through without this.
So I think I can now test my game as I program about 5 times faster thank you very much for your answer which led me to find this!
Answer by $$anonymous$$ · Aug 17, 2013 at 11:19 PM
Because you have all of your setup math in the Start() method, your gui wont react to a change in resolution, of corse you don't change the resolution in the game itself... Change the Start() method to an Update() method, but it will have to do some math every frame, so I would revert it beack to Start() when it comes time for the final build
Your answer
Follow this Question
Related Questions
How to Position a GUI at the Top-Right of the Screen? 2 Answers
Button position 4 Answers
GUI positioning 1 Answer
Camera rotation around player while following. 6 Answers
Make position of Buttons Elastic. 0 Answers