Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Canvas scaler and canvas width tablets.

Discussion in 'Scripting' started by bobcccc, Jul 29, 2020.

  1. bobcccc

    bobcccc

    Joined:
    Mar 12, 2014
    Posts:
    122
    I have a game that works great on mobiles and scales perfectly however for tablets it scales too wide. I tried adding some code to the canvas to limit it's width but I am guessing the canvas scaler is overriding this. Is there anyway to limit the canvas width and have it appear in the middle of the screen on tablets so that the aspect ratio is preserved


    Code (CSharp):
    1. public void resizer()
    2.     {
    3.         RectTransform objectRectTransform = gameObject.GetComponent<RectTransform>();
    4.         float testheight = objectRectTransform.rect.height / objectRectTransform.rect.width;
    5.         if (testheight < 1.7) {
    6.             gameObject.GetComponent<RectTransform>().sizeDelta = new Vector2 (70, 100);
    7.  
    8.         }
    9.     }
     
  2. UnityFuchs

    UnityFuchs

    Joined:
    Apr 18, 2018
    Posts:
    22
    use panels. you can set up panels pretty well and arrange them automatically, too. Be creative with them and you will find a nice solution that works. You can combine them with "layout-group"-components, too.