Greetings,
I’m working on creating a game console prefab in my project using the new Unity UI. I seem to have come across a limitation that I am not sure the best way to deal with. Any insight would be very much appreciated.
Here are the details of my setup:
The console in action:
The hierarchy:

The GameConsoleScrollView object has a mask:

The GameConsoleContent panel uses a Vertical Layout Group and Content Size Fitter:

I’ve created a message prefab that uses a Layout Element and is added to the GameConsoleContent panel during runtime:

And finally, the GameConsole object in the hierarchy has my custom GameConsole component attached:
using System;
using UnityEngine;
using UnityEngine.UI;
public class GameConsole : MonoBehaviour
{
public GameObject ContentPanel;
public GameObject MessagePrefab;
#region Unity Events
private void Start()
{
for(int i = 0; i < 75; ++i)
{
WriteLine("I've constructed a fairly long sentence that will be output to the console to show that after a certain amount of content has been generated, an exception will be thrown saying that there are too many vertices in the parent canvas.");
}
}
#endregion
#region Private Methods
private GameObject GenerateTextObject(string content)
{
GameObject messagePrefab = Instantiate(MessagePrefab);
Text textComponent = messagePrefab.GetComponent<Text>();
textComponent.text = content;
messagePrefab.transform.SetParent(ContentPanel.transform, false);
return messagePrefab;
}
#endregion
public void WriteLine(string content, params object[] args)
{
WriteLine(string.Format(content, args));
}
public void WriteLine(string content)
{
GenerateTextObject("<" + DateTime.Now.ToShortTimeString() + "> " + content);
}
}
I love how easy it is to set this up using the new UI system. Everything works as expected until too much content has been added. In this case, outputting the message 50 times works, but 75 is too much. Of course that would directly relate to the size of the strings being generated.
I understand why there is a vertex limitation for a canvas, but I’m not sure what the best approach is here to deal with the situation. I believe I would need to limit the amount of text objects generated to ensure the vertex limit is not hit, but in doing so wouldn’t that require a bunch of extra code to monitor the scroll position, etc? I feel like doing that would defeat the purpose of the simplicity of the current UI system. Is there an easier way?
I’ve done quite a bit of searching around on this topic and haven’t been able to find much. Any thoughts or suggestions would be very much appreciated.
Thank you for your time,
Jimmy
