Using Unity in C# I'm working to create a TextMesh Pro with a BoxCollider2D over the Text that is the same size as the generated text (a box around the area works, don't need the exact text shape).
The problem is when I try to use. GetRenderedValues it takes a LONG time for the actual value to be generated.
If I don't delay the BoxCollider2D size update it just comes back with 0.00001.
Is it possible to speed up the generation of the text rendering?
(I already have "ForceMeshUpdate" in my code.)
Code to Create the TMP - the Foreach is just a list of TMP I want to create (currently just 3):
foreach (var programs in uniquePrograms)
{
//Create each Programs from Initial Full data List
GameObject createProgram = (GameObject) Instantiate(ProgramPreFab);
createProgram.name = programs;
//Creates TextMeshPro Object (Should be used for Data Points) as child of First Child Component (should be Vertical layout - will likely need to be more specific in future)
List<DataClass.DataStructure> dataSplit = FullData.Where(x => x.program == programs).ToList();
foreach (var data in dataSplit)
{
//Create TextMeshPro and associated Box Collider Variable & ensure it's sorting order is always in front of it's parent
TextMeshPro dataPoint = (TextMeshPro)Instantiate(DataPointTMP, createProgram.transform.GetChild(0));
dataPoint.sortingOrder = createProgram.GetComponent<Renderer>().sortingOrder + 1;
//Update TMP with Data Point Name (ForceMeshUpdate may be need to ensure the text is there so the BoxCollider will have the correct values)
dataPoint.text = data.dataName;
dataPoint.ForceMeshUpdate();
dataPoint.name = data.dataName;
//Add TMP to createdDataPoint to add Box Collider in StartUpGenerateBoxColliders function (next)
createdDataPoints.Add(dataPoint);
}
}
I did a force wait to update the collider, but it is taking a long while:
IEnumerator WaitToCreateCollider()
{
yield return new WaitForEndOfFrame();
StartUpGenerateBoxColliders();
}
void StartUpGenerateBoxColliders()
{
foreach (var dataPoint in createdDataPoints)
{
BoxCollider2D boxCollider2D = dataPoint.GetComponent<BoxCollider2D>();
Vector2 textSize = dataPoint.GetRenderedValues(false);
boxCollider2D.size = textSize;
Debug.Log(dataPoint.name + "Box Colider Updated");
}
}
I'm calling each of these items from my Start update.
I've completed the items above, but I had to put in the wait function so the render values actually exist, but it is taking too long.