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 /
  • Help Room /
avatar image
0
Question by MrJBRPG · Jun 05, 2021 at 04:30 PM · uicamera rotatelayoutrecttransformrotation detection

Making script to Adjust Rect Transform to fit within camera "screen" after rotating UI rect transform

Background

So I am attempting to make a small prototype that the UI screen will rotate and adjust the its borders based on the camera rectangle. However, I am having some problems in implementing that correctly.

The best approach I have is setting the rotatable UI element to stretch the whole screen and having at least one layout group, horizontal or vertical since I can contain the entire user interface within the screen boundaries. The only missing part is finding a way to force the main UI layout group to fit within the screen, after rotation, through scripting.

Attempted Approach

I tried the following so far and it did not work:

 public void rotateUIScreen(float degrees)
 {
     rotatableUI.localRotation = Quaternion.Euler(0,0,degrees);

     var cameraLeft = mainCamera.rect.xMin;
     var cameraRight = mainCamera.rect.xMax;
     var cameraBottom = mainCamera.rect.yMin;
     var cameraTop = mainCamera.rect.yMax;

     bool isUiRotated = (int)(degrees / 90f) % 2 == 1;


     if (isUiRotated)
     {
         rotatableUI.offsetMin = new Vector2(cameraTop, cameraLeft);
         rotatableUI.offsetMax = new Vector2(cameraBottom, cameraRight);
     }
     else
     {
         rotatableUI.offsetMin = new Vector2(cameraLeft, cameraBottom);
         rotatableUI.offsetMax = new Vector2(cameraRight, cameraTop);
     }
     
 }



Screenshots


Normal camera and default UI orientation:

Normal Camera Screen Orientation


Normal Camera and 90° UI orientation - No Adjustment

This contains the correct orientation, but does not yet adjust to the screen


Manually adjusted UI corners after rotation

Manual adjustment to get desired result

^^^
If you notice on the last screenshot, it has all the values from left to right being at an equal distance away. I want all of the rotated blue dots to be attached to the anchor points, not set the anchor points to the blue dots.

Is there a better way to make a script that adjusts a stretched rect transform after it has been rotated?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by MrJBRPG · Jun 08, 2021 at 12:48 AM

Thanks to asking a few colleagues for help, I eventually found the answer: Here is how to rotate the main parent rotatable UI element that will work with any aspect ratio, and all UI elements will conform accordingly.
It will only work if you assign that script to a UI button through "OnClick" event.

 public void rotateUIScreen(float degrees)
     {
 
         Vector2 canvasDimensions = parentRectTransform.rect.size;
         
         Debug.Log("Canvas dimensions:" + canvasDimensions);
         Debug.Log("Rotatable UI dimensions:" + rotatableUI.rect.size);
 
         float rotationDifferenceAnchorOffset = (canvasDimensions.y - canvasDimensions.x) / 2;
 
         Debug.Log("Expected anchor offset: " + Mathf.Abs(rotationDifferenceAnchorOffset));
 
         rotatableUI.localRotation = Quaternion.Euler(0, 0, degrees);
         bool isUiRotated = (int)(degrees / 90f) % 2 == 1;
 
         if (isUiRotated)
         {
             // Remember, this configuration is a must
             // for screen rotation to work:
             // offset min (- , +) and offset max (+ , -) 
             rotatableUI.offsetMin = new Vector2(-rotationDifferenceAnchorOffset, rotationDifferenceAnchorOffset);
             rotatableUI.offsetMax = new Vector2(rotationDifferenceAnchorOffset, -rotationDifferenceAnchorOffset);
         }
         else
         {
             rotatableUI.offsetMin = new Vector2(0, 0);
             rotatableUI.offsetMax = new Vector2(0, 0);
         }
     }


Video Demonstration: UI Screen rotation in action

Comment
Add comment · 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

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

250 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 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 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 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 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 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 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 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 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 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 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

Proper way to wait until dynamically added children of a layout group are rendered 0 Answers

Get the Rect property of a UI object inside a Canvas that has the HorizontalLayoutGroup Component 0 Answers

Layout Group failes to resize children automatically and can't force resize by script when adding child 1 Answer

Help! with the Message text 0 Answers

RectTransform.anchoredPosition changes, but element is not visually updated 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