Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by antx · Aug 04, 2014 at 10:50 PM · androidrotatescreeneventorientation

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.

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 Nerevar · Aug 04, 2014 at 11:06 PM 0
Share

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.

avatar image Bunny83 · Aug 04, 2014 at 11:11 PM 0
Share

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?

avatar image antx · Aug 05, 2014 at 12:18 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Aug 04, 2014 at 11:18 PM

To draw 3D stuff within the GUI you need to do 2 things:

  1. Define a rect to draw. When using GUILayout use either GUILayoutUtility.GetRect or as you mentioned GetLastRect of another element.

  2. Use a disabled camera which shows your 3D stuff.

  3. 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.

Comment
Add comment · Show 4 · 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 antx · Aug 05, 2014 at 12:25 AM 0
Share

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.

avatar image Bunny83 · Aug 05, 2014 at 01:43 AM 0
Share

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;
     }
 }


avatar image antx · Aug 05, 2014 at 02:12 AM 0
Share

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.

avatar image antx · Aug 06, 2014 at 11:48 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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