i’m writing a visual novel and i’ve noticed an issue. My script is BIG (has a lot of text i mean) and the more process of game i get the more time i need to wait to render the text. it just appears slower and slower.
here is an image. it shows that i reserve about 2GB. 2GB for visual novel!! can’t beleive in it. here is the text rendering fuction:
private IEnumerator Build_Fade()
{
int minRange = pre_text_len;
int maxRange = minRange + 1;
byte alphaThreshold = 15;
TMP_TextInfo textInfo = tmpro.textInfo;
Color32[] vertexColors = textInfo.meshInfo[textInfo.characterInfo[0].materialReferenceIndex].colors32;
float[] alphas = new float[textInfo.characterCount];
while (true)
{
float fadeSpeed;
if (hurryup)
{
fadeSpeed = character_per_cycle * 5 * speed * 4f;
}
else fadeSpeed = character_per_cycle * speed * 4f;
for (int i = minRange; i < maxRange; i++ )
{
TMP_CharacterInfo characterInfo = textInfo.characterInfo[i];
if (!characterInfo.isVisible) continue;
int vertexIndex = textInfo.characterInfo[i].vertexIndex;
if(i >= alphas.Length)
{
alphas = new float[textInfo.characterCount];
}
alphas[i] = Mathf.MoveTowards(alphas[i], 255, fadeSpeed);
for (int v = 0; v < 4; v++)
{
vertexColors[characterInfo.vertexIndex + v].a = (byte)alphas[i];
}
if (alphas[i] >= 255)
{
minRange++;
}
}
tmpro.UpdateVertexData(TMP_VertexDataUpdateFlags.Colors32);
bool lastCharacterInvisible = !textInfo.characterInfo[maxRange - 1].isVisible;
try
{
if (alphas[maxRange - 1] > alphaThreshold || lastCharacterInvisible)
{
if (maxRange < textInfo.characterCount)
{
maxRange++;
}
else if (alphas[maxRange - 1] >= 255 || lastCharacterInvisible)
{
DIALOGUE.ConversationManager.userPrompt = true;
break;
}
}
}
catch
{
Clear();
}
yield return new WaitForEndOfFrame();
}
}
the try-catch block is used in very special cases – sometimes when i tap really quickly(tap but don’t skip the text like i can do in novels) this error occurs (index was out of range). but this doesn’t happen every time and the issue happened when there was no try-catch block.
i don’t know much about memory usage and the GC role in C# so I have clearly no idea what to do
I used profiler in Unity to track the memory usage so here is the result, check the image