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 /
  • Help Room /
avatar image
1
Question by alxcyl · Feb 14, 2017 at 12:38 PM · camerauicanvas

Scale a canvas based on reference camera viewport

I have a split screen game that spawns a camera and a canvas based on the number of players (from 1 to 4). This is what my Canvas Inspector looks like:

canvas settings

When I run the game and spawn more than 1 camera + canvas, they get displayed normally, but are not scaled properly. For example, since the width of the viewport is smaller, the HP and the Energy bars on the top overlap each other, even though they are anchored in the top-left and top-right corner of the screen.

The UI elements are designed for a single 1920x1080 view. I expected it to scale down accordingly when I split the view but it doesn't seem to do that.

What settings do I need to change so that it scales based on camera viewport size? Or do I need to manually resize the canvas by code when in split-screen mode?

capture.png (26.3 kB)
Comment
Add comment · Show 1
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 hexagonius · Feb 14, 2017 at 01:12 PM 0
Share

I found this which might do the trick (the setVertices part):

https://docs.unity3d.com/ScriptReference/Canvas.html

1 Reply

· Add your reply
  • Sort: 
avatar image
8

Answer by Ideka · Aug 04, 2017 at 04:19 AM

I just had to make a component that does the trick. Use it instead of the default CanvasScaler.

I inherited from the default CanvasScaler and added lines 20 to 24 in the overriden HandleScaleWithScreenSize.

 using UnityEngine;
 using UnityEngine.UI;
 
 public class CameraCanvasScaler : CanvasScaler
 {
     // The log base doesn't have any influence on the results whatsoever, as long as the same base is used everywhere.
     public const float kLogBase = 2;
 
     private Canvas m_Canvas;
 
     protected override void OnEnable()
     {
         m_Canvas = GetComponent<Canvas>();
         base.OnEnable();
     }
 
     protected override void HandleScaleWithScreenSize()
     {
         Vector2 screenSize = new Vector2(Screen.width, Screen.height);
         if (m_Canvas.renderMode == RenderMode.ScreenSpaceCamera && m_Canvas.worldCamera != null)
         {
             screenSize.x *= m_Canvas.worldCamera.rect.width;
             screenSize.y *= m_Canvas.worldCamera.rect.height;
         }
 
         float scaleFactor = 0;
         switch (m_ScreenMatchMode)
         {
             case ScreenMatchMode.MatchWidthOrHeight:
             {
                 // We take the log of the relative width and height before taking the average.
                 // Then we transform it back in the original space.
                 // the reason to transform in and out of logarithmic space is to have better behavior.
                 // If one axis has twice resolution and the other has half, it should even out if widthOrHeight value is at 0.5.
                 // In normal space the average would be (0.5 + 2) / 2 = 1.25
                 // In logarithmic space the average is (-1 + 1) / 2 = 0
                 float logWidth = Mathf.Log(screenSize.x / m_ReferenceResolution.x, kLogBase);
                 float logHeight = Mathf.Log(screenSize.y / m_ReferenceResolution.y, kLogBase);
                 float logWeightedAverage = Mathf.Lerp(logWidth, logHeight, m_MatchWidthOrHeight);
                 scaleFactor = Mathf.Pow(kLogBase, logWeightedAverage);
                 break;
             }
             case ScreenMatchMode.Expand:
             {
                 scaleFactor = Mathf.Min(screenSize.x / m_ReferenceResolution.x, screenSize.y / m_ReferenceResolution.y);
                 break;
             }
             case ScreenMatchMode.Shrink:
             {
                 scaleFactor = Mathf.Max(screenSize.x / m_ReferenceResolution.x, screenSize.y / m_ReferenceResolution.y);
                 break;
             }
         }
 
         SetScaleFactor(scaleFactor);
         SetReferencePixelsPerUnit(m_ReferencePixelsPerUnit);
     }
 }
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 bayerly · Aug 15, 2018 at 09:32 PM 1
Share

This was a godsend - many many thanks! Works perfectly.

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

152 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

Related Questions

what is the main camera size? 1 Answer

Prevent World Space canvas render mode to overlay other game objects 0 Answers

How to match position with Mini Map UI 0 Answers

Major Issues with Camera Switching 1 Answer

How would I Consistantly 'Snap' a UI Element to the Edge of a Camera? 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