I am using GUI components to render/format text. This text gets looked at by a camera, that camera gets rendered to texture, and the texture applied to a quad. This all happens at runtime, but during a “loading” phase, so no need to be super performant.
I got everything working swimmingly, except for one problem: The Text component does not update its “best fit” parameters and re- render the text until the next frame. This causes the text to be formatted ugly when the texture is built, even though the camera preview shows well formatted text on its first frame:
You can see that the render camera is viewing nicely formatted text, but the generated texture does not match. Here is my code:
textCanvas.GetComponentInChildren<TMPro.TextMeshProUGUI>().text = text;
LayoutRebuilder.ForceRebuildLayoutImmediate(textCanvas.GetComponent<RectTransform>());
Canvas.ForceUpdateCanvases();
//something is missing here!!
renderCamera.Render(); //render the camera before reading the texture
I have been Googling for hours, and keep coming up with the same “solution”: Just wait a frame! I am very hesitant to do that. I REALLY don’t want to wait a full frame. I could potentially be generating hundreds or thousands of these signs and waiting a full frame for each one just feels like a complete hacky way to do this. I have tried both TMP and UGUI Text componenets and they both behave this exact same way. I have also tried RectTransform.ForceUpdateRectTransforms
on every rect transform there with no luck.
I feel like I have to be missing something. Either that or it is completely impossible and I am just wasting my time. Any help would be great. Thanks!!