0
\$\begingroup\$

I have a parent gameobject (Empty) with a content size fitter with horizontal and vertical fit set to preferred size which is also a Horizontal Layout Group with Control Child size width and height enabled:

enter image description here

It has got a TextMeshPro text as child, and now, as the text expands the parent also expands. The problem is, I want to get the height of this parent. And if I use this:

var rect = parent.gameObject.GetComponent<RectTransform>.rect
Debug.Log($"Width: {rect.width}, height: {rect.height}");

or even,

var mValue = parent.gameObject.GetComponent<RectTransform>.sizeDelta;
Debug.Log($"sizeDeltax: {mValue.x}, sizeDeltaY: {mValue.y}");

they both give 0.

I require some way to extract the expanded width and height. How can I do that?

NOTE: The content of the child i.e. text is being assigned dynamically, also the parent/text is a prefab being instantiated dynamically i.e. via Instantiate, and this is not done in the start method.

Okay, here's more detail about the hierarchy and stuff:
The hierarchy

enter image description here

Parent:
enter image description here

Interface
enter image description here

MessageField
enter image description here

OutputSection
enter image description here

Viewport
enter image description here

ChatPanel
enter image description here

So, eventually chat bubbles are supposed to stack on top of one another, they get parented to ChatPanel, and each child is a prefab instantiated as mentioned above, the prefab detail are:

hierarchy
enter image description here

stackItem
enter image description here

ItemParent
enter image description here

Tag
enter image description here

TextItem (Note the MessageTextItem is basically not used, it's there for some old code reasons) enter image description here

ChatTextBubble
enter image description here

enter image description here
These are expanded below:
enter image description here enter image description here

When I send message, I log the width and height of the white part of the chat bubble which is basically the **chatTextBubble above, using the code: **

   var expRect = chatTextBubble.GetComponent<RectTransform>();
            Debug.Log($"dvz:: expRect width is: {expRect.rect.width} and height is: {expRect.rect.height}");
  

and it logs 0. Please tell me what am I doing wrong. It looks similar to below example but, not working.

This is what it looks like during gameplay: enter image description here

EDIT: I just noticed, the chatTextBubble has this during gameplay:

enter image description here but, logs 0 when I log with the above mentioned code.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Can you share a screenshot of your project hierarchy while the game is running and the child UI objects are spawned? \$\endgroup\$ Commented Oct 28, 2021 at 16:29

2 Answers 2

2
\$\begingroup\$

Something is amiss with your setup, because RectTransform.rect will contain correct updated values as ContentSizeFitter manipulates its dimensions.

Here is a minimum verifiable example. Project hierarchy: project heirarchy

Code, attached to Test Controller GameObject:

using UnityEngine;
using UnityEngine.UI;

public class GetParentSize : MonoBehaviour
{

    public RectTransform parent;
    public GameObject childPrefab;
    public Text readout;
    float timer = 0;

    void Update()
    {
        timer += Time.deltaTime;
        if(timer >= .5f)
        {
            var child = Instantiate(childPrefab);
            child.transform.SetParent(parent.transform);

            timer = 0;
        }
        readout.text = string.Format("Parent Width: {0} units",parent.rect.width.ToString());
    }
}

Code instantiates a UI prefab every .5s and sets the parent of the instanced gameobject to the gameobject with the ContentSizeFitter and HorizontalLayout components. Every frame it updates the UI readout text with the width of the gameobject with the ContentSizeFitter.

This is how the parent gameobject is configured in the inspector: Parent in inspector

This is how the Test Controller is wired up:enter image description here

And here it is in action: Demo

\$\endgroup\$
9
  • \$\begingroup\$ Hello, thanks for the answer, sorry I replied so late, let me check this out. \$\endgroup\$
    – BumbleBee
    Commented Nov 1, 2021 at 17:50
  • 1
    \$\begingroup\$ If you still can't get it to work, share more details of your set up -- what you are trying to do should work... \$\endgroup\$ Commented Nov 1, 2021 at 18:02
  • \$\begingroup\$ I'll post the entire thing \$\endgroup\$
    – BumbleBee
    Commented Nov 2, 2021 at 3:12
  • \$\begingroup\$ I posted the entire hiearchy and all related content, could you please take a look? \$\endgroup\$
    – BumbleBee
    Commented Nov 2, 2021 at 3:33
  • 1
    \$\begingroup\$ @BumbleBee Thanks for adding all the extra information -- I'll have a closer look later today... \$\endgroup\$ Commented Nov 2, 2021 at 15:21
2
\$\begingroup\$

I just had the same problem and I found the solution was to use ForceRebuildLayoutImmediate (2018 version):

LayoutRebuilder.ForceRebuildLayoutImmediate(go.GetComponent<RectTransform>());

I can then get the dimensions with: go.GetComponent<RectTransform>().sizeDelta.x (Width) or .y (Height)

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .