Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Zoom code for Layout Group Decreasing Each time?

Discussion in 'UGUI & TextMesh Pro' started by SceneForgeStudio, Jan 13, 2022.

  1. SceneForgeStudio

    SceneForgeStudio

    Joined:
    Feb 28, 2017
    Posts:
    470
    I realize the title is kinda confusing, so hopefully this clears it up:

    I'm trying to create a zoom in/out functionality for a layout group by going through each child and changing its size. This works pretty well, but I want to limit it to a min and max number of steps.
    For some reason, this code seems like it's working fine, but after each full zoom in and out, the max seems to be decreasing, and I'm not sure why. Meaning that fully zoomed-in equates to smaller and smaller each time.

    Here's the script, any help would be appreciated!

    Code (CSharp):
    1.  
    2. // Assumes all children are the same size;
    3.      public float zoomValue = 0.2f;
    4.      public int maxSteps;
    5.      [SerializeField] int zoomSteps = 0;
    6.      public bool skipFirst;
    7.      private void Start()
    8.      {
    9.          zoomSteps = maxSteps;
    10.      }
    11.      public void ZoomIn()
    12.      {
    13.          if (zoomSteps >= maxSteps)
    14.              return;
    15.          RectTransform[] rects = transform.GetComponentsInChildren<RectTransform>();
    16.          foreach(RectTransform t in rects)
    17.          {
    18.              if (skipFirst && t.GetSiblingIndex() == 0)
    19.                  continue;
    20.              t.sizeDelta *= (1 + zoomValue);
    21.          }
    22.          zoomSteps++;
    23.      }
    24.      public void ZoomOut()
    25.      {
    26.          if (zoomSteps <=1)
    27.              return;
    28.          RectTransform[] rects = transform.GetComponentsInChildren<RectTransform>();
    29.          foreach (RectTransform t in rects)
    30.          {
    31.              if (skipFirst && t.GetSiblingIndex() == 0)
    32.                  continue;
    33.              t.sizeDelta *= (1 - zoomValue);
    34.          }
    35.          zoomSteps--;
    36.      }
    37.  
    Thanks!