Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Stretching an object to fill the SafeArea

Discussion in 'Scripting' started by Maple-Senpai, Aug 17, 2020.

  1. Maple-Senpai


    Oct 2, 2018
    Hi, so I have this code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    5. [RequireComponent(typeof(RectTransform))]
    6. public class SafeAreaFilter : MonoBehaviour
    7. {
    8.     private void Awake() {
    9.         var rectTransform = GetComponent<RectTransform>();
    10.         var safeArea = Screen.safeArea;
    11.         var anchorMin = safeArea.position;
    12.         var anchorMax = anchorMin + safeArea.size;
    14.         anchorMin.x /= Screen.width;
    15.         anchorMin.y /= Screen.height;
    16.         anchorMax.x /= Screen.width;
    17.         anchorMax.y /= Screen.height;
    19.         rectTransform.anchorMin = anchorMin;
    20.         rectTransform.anchorMax = anchorMax;
    21.     }
    22. }
    And I just create a new object, attach the script to it and put all the elements as child of that object to filter them so they dont overlap on the safe area..
    And it does this:

    It leaves a blank area above and I want to stretch the Banner above so I can fill the safe area.

    How can I do it? Hope you can help me out, guys.
  2. Yoreki


    Apr 10, 2019
    I'm not really experienced with UI, but you can get the screen size, you know / can get the size of each of your other components, thus you should be able to calculate the size of the 'safe area' and then just resize the last component to fit exactly into it as you like it.