Topic Author
Posts: 17
Joined: 21 Feb 2014, 22:59

[Unity] Forcing a layout update

24 Sep 2014, 19:18

Hey all, I'm trying to figure out how to force a layout update. I've got a StackPanel that contains TextBlocks that I want centered over a point on the screen. Think chat bubbles or banter text over characters heads. The problem I am running into is that the StackPanel is inside of a Canvas so I can position it from the bottom left corner, but that aligns the StackPanel to the left and not centered over the point.

So what I need to do is get the actual width of the stack panel after the content is set and then subtract half of that from the Canvas.Left value to center it. The catch is that by the time the actual width value is updated, the content has already been rendered to the screen, so there is a pop where it was left aligned over the point and then becomes center aligned.

Reading through stack overflow, there should be an UpdateLayout() method on each UIElement that will force the layout to be updated and should allow me to properly calculate the offset before it gets rendered the first time. However I do not see this method in the API for the NoesisGUI Unity Plugin. Am I just missing something or is there a better way to do this?
User avatar
Site Admin
Posts: 3027
Joined: 22 Dec 2011, 19:20

Re: [Unity] Forcing a layout update

25 Sep 2014, 21:13

The way you are trying is correct, and calling UpdateLayout() will be enough to obtain the dimensions of your panel. This function just wraps a Measure and an Arrange of the element (if needed), so you can directly call these methods instead:
Noesis.Size UpdateLayout(Noesis.UIElement element)
    element.Arrange(new Noesis.Rect(panel.GetDesiredSize());
    return element.GetRenderSize();

Who is online

Users browsing this forum: Bing [Bot] and 10 guests