Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags

Size is not uptade correctly while recttransform size changed in playing

A topic by hsu99 created Feb 02, 2022 Views: 354 Replies: 16
Viewing posts 1 to 17

Hi,it seems that the wrap size of STM text is not update correctly when the recttransform size changed in playing.

I used the horizontal/vertical layoutgroup and checked Control Child Size to auto layout the ui component(STM text).

But turning the gameobject off and on again got the right size.  

Because some object will be reused for better performence. (change text value of the same ui object like scroll view cell...etc)

Currently we used OnRectTransformDimensionsChange() to detect the rect changing and call Rebuild() function in it to avoid this problem.(Maybe there will be other way to solve it.)

Because we always want to keep the STM version to be the newest,it will be very helpful if this problem can be solved in the future update.


Developer

In edit mode, STM will automatically update its internal bounding box when the rectTransform is changed for ease of setting up a UI, but in a build, the bounds need to be manually updated, since the rect sizes will normally only change when enabling a UI, or changing the game's window's size.

Also, try experimenting with adding a "content size fitter" component to the STM object to have it control the horizontal/vertical layout group! It's been a while, but that may produce the results you need. (I could be interpreting the issue incorrectly)

For the window size change, you can call SuperTextMesh.RebuildAll() when changing the window size, and for enabling the UI, make sure the UI gameobject is enabled before your STM object! I tried to write in some automatic fixes for this, but Unity UI can be really fickle about it... You might have to call "Canvas.ForceUpdateCanvases()" before enabling STM if it still remains an issue.


I hope this helps! If these don't work, I'll try to fix it for the next update!

(1 edit)

Yes I tried to add the "content size fitter" to the STM object but it seems not to work properly...


And I made a simple example of this case and hope this could help to figure out the problem. 

Here is my ui setting:


I also recorded the video of "STM Sample"(use STM text) and "Default Text Sample"(use unity default text). 

(The STM version I used  in this sample is 1.12.0, and I comment out the rect update in Editor mode to make sure it works as the build.)

And the result I want is as the demo of "Default Text".

sample video download link(available in 7 days, unzip pw:stm): https://firestorage.jp/download/87217ba5b9b682fa109a2f8eae240cabdc6a2406

Developer

Oh! I understand the issue now, but I'm not 100% sure on the solution just yet.


I'll be able to take a look at this this weekend, but when pressing the "change text" button, before updating STM, please try calling "Canvas.ForceUpdateCanvases()" .

I'm doing a bit more research into this, and it seems that with nested UI elements, the parent element should be set before the child one... so calling layoutGroup.SetLayoutHorizontal(); or layoutGroup.SetLayoutVertical(); before setting STM might also do it. If that works, I'll try to make it so STM checks for this itself when being rebuilt.

I'll keep thinking about the issue, I really want to solve this!

The OnRectTransformDimensionsChange() function of unity UIBehaviour may also help when parent layoutgroup try to change the child size.

It is called before Update().

https://docs.unity3d.com/2018.4/Documentation/ScriptReference/EventSystems.UIBeh...

Wish to see this problem being solved in future version.  Many many thanks! :) 

Developer

...I'm very embarrassed I didn't know about this function, I'll be sure to give it a try soon!


Ok, I tried it out, and I don't think the content size fitter was the solution. Try using the "Layout Element" component on STM. I remade your example scene, and the behaviour seems to be better, even with the code in Update() commented out. But I could still be misunderstanding the issue. (Text is going beyond the UI limit in my gif because I have the cutoff mode set to ignore)



The gif looks not update the rect of the changed text value. (?)


Oh...  sorry I think my title is  a little wrong...

 STM rect is not update properly with the text length changed in playing. (with something like layoutgroup control)     -->  This might be better.

should act like this(use default unity text):



And from the sample of my previous post,I noticed that STM  rect size actually changed. But it seems to have a delay. 

The first time text length changed,the rect of STM text seems to keep the previous size.

From the second time it updates to the right rect size.

(My temporarily action is just like Rebuild() one more time in OnRectTransformDimensionsChange() callback)

here is previous sample(use STM):


Developer

Hmm, what version of Unity are you using? I'm still a bit confused, but maybe I can reproduce the issue in a different version?

Would it be possible to email me this sample project?

Hi, I've sent my sample project to your email.

And my Unity version is 2019.4.23f1.

I hope that can help.

I stumbled upon the same issue recently here over on 2020.3.15f2; let me know if I can be of any help (i.e. after checking out hsu99's sample project).

Developer

I got the test scene, and I believe I've solved it...

Please try editing line 1606 of SuperTextMesh.cs to this:

if(uiMode) LayoutRebuilder.ForceRebuildLayoutImmediate((RectTransform)tr.root);


Using LayoutRebuilder.MarkLayoutForRebuild(tr); almost seems to work here, but when the size changes, you can see it update after a frame... so I think this method might be necessary for now. Hopefully this solves the problem for good?Please let me know, and I'll implement it for the next build!

Hi,I changed the line 1606 from:

if(uiMode) LayoutRebuilder.ForceRebuildLayoutImmediate((RectTransform)tr.parent);

To:

if(uiMode) LayoutRebuilder.ForceRebuildLayoutImmediate((RectTransform)tr.root);

But it look like the problem still exists.  (Did I miss something?)


And about code  if(uiMode) LayoutRebuilder.MarkLayoutForRebuild(tr); 

,the update after one frame. (Maybe this,I used the MarkLayoutForRebuild of line 1604) 

I think that  is because the Rebuild() function called in the Update() function when rect changed.

But it  only woks in UNITY_EDITOR mode, not works in build. 


With the if(uiMode) LayoutRebuilder.MarkLayoutForRebuild(tr); added, 

and add the code below somewhere

void OnRectTransformDimensionsChange() {     
    if (uiMode) Rebuild(); 
}

it seems to work.


(2 edits)

Great, I had been waiting for an update because changing line 1606 did not fix the issue on my side either.

Following @hsu99 instruction by adding a call to Rebuild() in OnRectTransformDimensionsChange(); and adding if(uiMode) LayoutRebuilder.MarkLayoutForRebuild(tr); immediately fixes rectTransform sizing issues.

But because Canvas.ForceUpdateCanvases() will also change the rect and call OnRectTransformDimensionsChange() if rect changed.

In some cases, Rebuild() may be called many times before it reaches its final size.

Although it works but that is not a good solution if there has many objects need to be handled...

Developer

Oh, for some reason I was under the impression that "OnRectTransformDimensionsChange()" wouldn't be available since SuperTextMesh doesn't inherit from monobehaviour? Is it actually being called? I'll be able to check myself in a little bit. Thank you so much for your patience, both of you.


I'm not sure why you're getting different results than me, there... did you revert the changes made before?


But if this works, I'll gladly add it to the next update! Even though calling Rebuild() multiple times is inefficient, for small UI elements, which this would mostly be effecting, it's not too bad, even on lightweight platforms. But I keep wondering if there's something else I'm forgetting here, sorry for all the trouble. I'll do another check on the code shortly, but my results look different than yours, so I'm still a bit confused by it all

I also opened a new one of the sample project and changed the line, but it was the same.(?)  

(I used the sample sent before.)


OnRectTransformDimensionsChange is a function of UIBehaviour,and yes it is be called in SuperTextMesh.

I think this is a way to figure out how to fix the problem or find the cause. 

I’m glad I was able to help.

Developer

I'll be sure to add it for the next update! Thank you so much for the help!