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 Gurunext · Mar 24, 2015 at 10:51 AM · canvasimage

Canvas Scaler / math behind: Scale with Screen Size

The problem I find with using Canvas Scaler is that I can't seem to know the positions of corners in a RectTransform element. Let's say I have an image and I want to find an upper left corner. But first I atleast need to know how much the Canvas Scaler actually scales the image, because image.rect returns unscaled rect of the image.

This code works when Canvas Scaler -> Scale with Screen Size has a match value of 1 or 0 in other cases it misses just enough to piss me off, usually the return value is bigger than the actual scale

 return ((Screen.width / scaler.referenceResolution.x) * (1 - scaler.matchWidthOrHeight) +
                 (Screen.height / scaler.referenceResolution.y) * (scaler.matchWidthOrHeight));

I am also pissed of that there is no way to grab something like scaler.scale or scaler.scaleFactor since it's not used within "Scale with Screen Size" mode could easily return the actual scale... bus since it doesn't can anyone work out the math behind Matching in Scale with Screen Size mode?

P.S. And MAYBE the easy solution to this is just to check the source code of the Canvas Scaler, I am not sure how to do that either

EDIT: I found this: http://stackoverflow.com/questions/27805163/retrieving-the-amount-of-scaling-from-the-canvas-scaler-unity Talks about same problem, gives no solution. Suggests workaround, but rectTransform.localScale is not changed by the Canvas Scaler, nor any other value that I know of

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

3 Replies

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

Answer by Gurunext · Mar 24, 2015 at 05:46 PM

As usual with time I have found a way to extract the current scaleFactor of Canvas Scaler. In theory and if this message is marked as an answer then it worked.

So I will anchor an empty RectTransform to one of the corners of a disabled image. This RectTransform will be able to indicate the position that certain corner and by getting a ratio between the unscaled corner position I get from rectTransform.rect I will get the actual scale. Man... countless times I had to workaround Unity stuff. But I guess I learn something every time.

 public RectTransform scaleTemplateImage, scaleCheckPosition;
 private static float _scaleFactor;

 void Update() {
     _scaleFactor = Mathf.Abs(scaleCheckPosition.position.x - scaleTemplateImage.position.x)/
         Mathf.Abs(scaleTemplateImage.rect.width/2);
 }

 public static float scaleFactor {
     get    {return _scaleFactor;}
 }


Yep it worked, here is the code. ScaleTemplateImage is a blank image and ScaleCheckPosition is another image anchored to upper right corner and positioned at Vector3.zero (pivot = anchor).

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

Answer by Kumo-Kairo · Aug 01, 2015 at 07:54 PM

For anyone reading this - the solution for the Scale With Screen Size is to query the Canvas itself for the scaleFactor and not the CanvasScaler I've learned that after looking at the UGUI sources. It's quite counter-intuitive but that how it works.

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 rotten_tater · Oct 31, 2015 at 08:25 PM 1
Share

It's really important to note: you're going to get back 1 every time if you do this if you query it from Awake. I only got back the correct factor when I called it from Start.

avatar image
1

Answer by PietroBot · Jul 27, 2017 at 10:51 AM

I had the same issue when working with Scale with Screen Size and the scaleFactor on the CanvasScaler was always 1 no matter the size changes, which didn't help at all when trying to set points on specific positions on the canvas. I found that the localScale of the Canvas transform had the correct scale, which you can use to scale the positions. Something like this:

  var canvasScalerTransform = FindObjectOfType<CanvasScaler>().gameObject.transform;
  var unscaledPosition = new Vector3(5, 1);
  var scaledPosition = Vector3.Scale(unscaledPosition, canvasScalerTransform.localScale);
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

22 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

Related Questions

How to fix poor canvas image scaling quality?,How to fix canvas image scaling quality? 1 Answer

Image with transparency cut-offs as button 2 Answers

RealTime image put on a Canvas 2 Answers

I can't see any png files as a source image 4 Answers

4.6 UI Image Color.Lerp 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