- Home /
Event for orientation change done?
Im using a GetLastRect() in my ONGUI to figure out where I can place my 3D Logo on Screen. This works fine on PC but when I rotate my Tablet the Rotate seems to be too slow and the coordinates that I calculate are wrong. Starting the game shows the logo right in place no matter what orientation I start with, but after the first rotate it is totally off.
So I wonder if there is something like an event in Unity that tells me when I can savely take the measurements of the GUILayout after the screen was rotated / orientation changed.
Hello
maybe you should check the state of your screen orientation before taking the values using input methods.
mobile doc (Screen Orientation section) and Input.deviceOrientation I don't know the exact behavior of this though.
Do you need the screen to rotate in your game? else you can just disable autoRotation.
Can you show the relevant parts of your OnGUI code? The GUI layouting is recalculated every frame, so your GUIelements should be correct at all times. How do you draw your "3D logo", with a camera?
my ONGUI code looks like this:
if (screenWidth != Screen.width || screenHeight != Screen.height) AdjustZoom();
// lots of other code here inbetween...
if (logoRectInitialized == false) {
GUILayout.Box ("", GGGuiSkin.customStyles[(int)customStyle.TransparentBox], GUILayout.Height(300 * zoom));
if (Event.current.type == EventType.Repaint) {
logoLastRect = GUILayoutUtility.GetLastRect();
logoRectInitialized = true;
}
}
else GUILayout.Space (300 * zoom);
screenWidth and screenHeight are being set to Screen.width / Screen.height inside of AdjustZoom() wich does a lot of other things too, including setting logoRectInitialized to false.
So the GetLastRect() is only called once. From the Data in logoLastRect I calcultate in another script ( in Update() ) the new 3D-Position for my logo.
void Update () {
if (screenWidth != Screen.width || screenHeight != Screen.height) {
if (BaseGUI.logoRectInitialized == true) AdjustLogoPos();
}
}
Those screenWidth and screenHeight variables are not the same as those above. This is a different Script! They are reset in AdjustLogoPos().
This works in the editor nicely, but on the tablet it does not. The data from GetLastRect() seems to be off.
Answer by Bunny83 · Aug 04, 2014 at 11:18 PM
To draw 3D stuff within the GUI you need to do 2 things:
Define a rect to draw. When using GUILayout use either GUILayoutUtility.GetRect or as you mentioned GetLastRect of another element.
Use a disabled camera which shows your 3D stuff.
Draw the camera manually during the Repaint event by calling camera.Render(). Don't forget to set the pixelRect of your camera to your drawing rect from point 1.
Just to clear this up. I have A GUILayout GUI and in the background you can see the 3D scene. In the 3D scene I have my logo, but I don't want it to be covered by GUI elements. I left some Space in the Layout but since I don't know on what screen the game runs I need to figure out where and how big the space is. That I do with a temporary GUILayout.Box which I measure via GetLastRect() and then replace with a Layout.Space(). The GetLastRect data I use to calculate my logo position in 3D space and move it there and scale it to fit.
Ins$$anonymous$$d of your "logoRectInitialized" and the Space replacement you should simply use GUILayoutUtility.GetRect ins$$anonymous$$d of GUILayout.Box. It directly returns the layouted rectangle.
Something like:
void OnGUI()
{
// ...
Rect tmp = GUILayoutUtility.GetRect("", GGGuiSkin.customStyles[(int)customStyle.TransparentBox], GUILayout.Height(300 * zoom));
if (Event.current.type == EventType.Repaint) {
logoLastRect = tmp;
}
}
I don't really understand your code. I guess this is some kind of optimization, but why would I need the if (Event.current.type == EventType.Repaint)... then still? Is tmp changed again? Or is it that logoLastRect is then only assigned when the repaint has really happend?
Anyway, I'm really glad you are trying to help me here, since this bugs me a lot. I can only test this on the tablet. Been trying a delay via a CoRoutine before messuareing with GetLastRect in the meantime but works only every second time so far... So thanks for your input.
So just to let you know: This problem got solved. There is probably no specific event for when the orientation change is done, but once the Screen.width or Screen.height or the Orientation value have changed it´s safe to assume, that it´s really done.
$$anonymous$$y problem turned out to be my own fault. I added an additional check before the measureing with GetLastRect(), so that my AdjustZoom() function is really done with what ever it is doing since my calculations are based on it's results.
Your answer
Follow this Question
Related Questions
Detect Orientation change at runtime? 0 Answers
Screen orientation question... 1 Answer
Android Game Orientation cannot be fixed to potrait 0 Answers
How to get game orientation when Input.deviceOrientation is faceUp ? 0 Answers
Hey I would like to learn via script how to change automatically the screen orientation 1 Answer