Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
3
Question by I_Love_Misato · Feb 09, 2017 at 10:17 AM · uicanvasscalepositioning

Can't get an actual canvas size

Hi, guys!

I can't figure out how to get the actual size of a canvas. Here is the canvas in the inspector: alt text

so you can see that its width and height are 901.2876 to 600. How can I access these values via script? When I tried several functuons, neither of them provided me a needed result. Here is the code I've used and its result:

 void Awake()
     {    
         var rect = canvas.transform as RectTransform;

         Debug.Log ("sizeDelta: " + rect.sizeDelta);
             // sizeDelta: 350; 233

         Debug.Log ("canvas width: " + rect.rect.width + "; canvas height: " + rect.rect.height);
             // canvas width: 350; canvas height: 233

         Debug.Log ("scale factor: " + rect.localScale);
             // scale factor: (1.0, 1.0, 1.0)

         Debug.Log ("Screen size: " + Screen.width + "; " + Screen.height);
             // Screen size: 350; 233

         Debug.Log (Screen.currentResolution);
             // 1920 x 1080 @ 60Hz

             Debug.Log ("scale factor " + canvas.scaleFactor);
             // scale factor 1
     }

As you see, most functionl provide me the "350 : 233" size. If I divide it by the scale factor shown in inspector (see the image above), which is equal to 0.3883, I will get the correct result. But when I try to get this value by the rect.localScale functions, it provides me just a unit vector (1; 1; 1). Why? Why the scale factor is 1, when it should be 0.3883? What should I do?

безымянныи.jpg (50.9 kB)
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 FortisVenaliter · Feb 09, 2017 at 02:17 PM 0
Share

Is it inheriting that scale from it's parent? If so, then the localScale would be 1, and you'd need to use the lossy scale. As long as it's scaled uniformly, that should work.

avatar image I_Love_Misato FortisVenaliter · Feb 09, 2017 at 03:04 PM 0
Share

I tryed lossyScale, there is 1 either. Anyway, my canvas has no parent objects and it seems that the value indicated in the inspector should be a local parameter.

avatar image I_Love_Misato · Feb 09, 2017 at 06:05 PM 0
Share

I just found the solution. The needed function is RectTransform.GetLocalCorners(). It doesn't work for a canvas, but for its children objects it works.

1 Reply

· Add your reply
  • Sort: 
avatar image
3

Answer by ctykaya · Oct 15, 2019 at 06:29 PM

Put your logic in to the start function and you will get desire result. In awake canvas scaler component doest not rendered yet so you cannot acces correct rect size.

Comment
Add comment · Show 1 · 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 bhupiister · May 11, 2020 at 09:14 PM 0
Share

@ctykaya This worked for me. I was putting it in an awake method and was getting undesired results. In the start function, it works fine.

 public Canvas CanvasUIElement;//Set in editor
 private float CanvasWidth;
 private float CanvasHeight;
 
  void Start()
 {
 CanvasWidth = CanvasUIElement.GetComponent<RectTransform>().rect.width;
 CanvasHeight = CanvasUIElement.GetComponent<RectTransform>().rect.height;
 Debug.LogFormat("Canvas width:{0} canvas height:{1} ", CanvasWidth, CanvasHeight);
 }
 

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

104 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

Related Questions

Editing RectTransform scale 2 Answers

Is there a way to change UI Scale Mode without causing my UI to mess up? 1 Answer

Use the canvas scaler to resize UI on a RenderTexture's resolution ? 0 Answers

Why are the elements of my scrollview scaled different after refreshing? 0 Answers

Anchor UI elements to the root canvas 0 Answers


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