Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 NateJC · Jan 29, 2015 at 11:24 PM · uicanvassizedimensions

Unity 4.6 UI Canvas width & height

Is there a way to dynamically get the width & height of the Canvas? or else the top-right coordinates? I can't seem to find it in the docs, but maybe I'm looking for the wrong entry...

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

2 Replies

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

Answer by DCTShinobi · Jan 30, 2015 at 06:30 AM

I'm not sure exactly when or how you want to display it, but this is what I've been using for my UI scripts.

Just attach a script to your canvas, and stick this in the Start() for a one time display in the console, or in your Update() for it to display every frame...

         RectTransform objectRectTransform = gameObject.GetComponent<RectTransform> ();
         Debug.Log("width: " + objectRectTransform.rect.width + ", height: " + objectRectTransform.rect.height);


Or you can put this into your Update() so you can get the size every time you press the spacebar...

         if (Input.GetKeyDown("space"))
         {
             RectTransform objectRectTransform = gameObject.GetComponent<RectTransform> ();
             Debug.Log("width: " + objectRectTransform.rect.width + ", height: " + objectRectTransform.rect.height);
         }


Of course, this is for the width and height of the object (it works for whatever UI object you attach it to, not just a canvas). I'm not sure if it will give you the proper coordinates, though. I only work with canvases that have the Render Mode set to Screen Space - Overlay, so that's why it works for my projects.

I hope it helps! :-)

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 NateJC · Mar 16, 2015 at 05:52 PM 1
Share

This solves my question, thanks for the answer! (Sorry I didn't accept sooner, I got pulled out of Unity work for a bit and just now made my way back)

avatar image
3

Answer by whoopdeedoo03 · Jan 02, 2019 at 06:28 AM

You may also call RectTransform from the canvas itself...

 Canvas canvas = FindObjectOfType<Canvas>();
 
 float h = canvas.GetComponent<RectTransform>().rect.height;
 float w = canvas.GetComponent<RectTransform>().rect.width;
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

21 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

Related Questions

Set Native Size in UI 1 Answer

Universal Method of finding pixel dimensions of UI elements 1 Answer

UI IMAGE doesn't work and resize with the screen 2d 1 Answer

Screen to canvas space 2 Answers

[4.6 GUI] Displaying my pause menu when ESC is pressed 2 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