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

Question RectTransform sizeDelta not updating fast enough

Discussion in 'UGUI & TextMesh Pro' started by Toack, Apr 7, 2024.

  1. Toack

    Toack

    Joined:
    Dec 18, 2006
    Posts:
    109
    I'm instantiating a prefab that calculates its own size depending on the context (basically the description text on it), because of some animations I can't use the VerticalLayoutGroup to position them because I have some animations in place and it gets in the place.

    upload_2024-4-7_15-47-12.png

    Everything is fine with this setup except for 1 thing, the sizeDelta of the objects doesn't update in the same frame, but it takes 2-3 frames to give the final value even if the visuals are fine since instantiated.

    Object 1:
    Frame:44 - Instantiate - RectTransform.sizeDelta:(500.00, 0.00)
    Frame:45 - RectTransform.sizeDelta:(315.76, 19.00)
    Frame:46 - RectTransform.sizeDelta:(315.76, 31.93)
    Frame:47 - RectTransform.sizeDelta:(315.76, 31.93)
    Frame:48 - RectTransform.sizeDelta:(315.76, 31.93)
    Frame:49 - RectTransform.sizeDelta:(315.76, 31.93)

    Object 2:
    Frame:115 - Instantiate - RectTransform.sizeDelta:(500.00, 0.00)
    Frame:116 - RectTransform.sizeDelta:(315.76, 19.00)
    Frame:117 - RectTransform.sizeDelta:(315.76, 31.93)
    Frame:118 - RectTransform.sizeDelta:(315.76, 55.86)
    Frame:119 - RectTransform.sizeDelta:(315.76, 55.86)
    Frame:120 - RectTransform.sizeDelta:(315.76, 55.86)


    This is the logs for 2 objects instantiated by the system. When instantiated the sizeDelta is the one at the prefab even though I already set the text before logging. Then it takes several frames to reach the stable sizesDelta, the Y is the automatically calculated value, X is set directly after instantiation.

    Any ideas how I can have that deltaSize at the same time I instantiate the objects? because I need that value to position them properly before any animation on them.

    Cheers.
     
  2. Toack

    Toack

    Joined:
    Dec 18, 2006
    Posts:
    109
    So nobody...
     
  3. j1mmie

    j1mmie

    Joined:
    Oct 31, 2013
    Posts:
    33
    This is crazy nobody has a solution. I try to do any of the following after setting sizeDelta:

    1. Call
    Canvas.ForceUpdateCanvases()


    2. Call ForceRebuildLayoutImmediate on each child LayoutGroup:

    Code (CSharp):
    1. var groups = root.GetComponentsInChildren<T>();
    2. if (groups == null) return;
    3.  
    4. // Assume groups are returned in order of hierarchy. Rebuild from the bottom up.
    5. int i = groups.Length;
    6. while (i --> 0) {
    7.   LayoutRebuilder.ForceRebuildLayoutImmediate((RectTransform)groups.transform);
    8. }

    But this doesn't even work all the time, and I end up having to wait a frame anyway. Curious if you've found a better solution