Printing a string of text to a Texture2D
I feel like I've scoured the internet on this, but I haven't found anything useful.
I have some strings in my code that I want to add to a Texture2D that's used entirely for debug purposes. The Texture2D is printed to several (dozens or more) files on disk. It's never displayed on-screen. I have no cameras, no objects, no GUI, no renderers in this entire area of my code at all; I'm just building some underlying data processing and debugging systems, and I want intermediate snapshots of certain information throughout the process.
Most of the information is non-textual, but I want to add a bit of text under the main image to help make sense of the debug information. Specifically, I want to create a color-coded legend, so that I can basically stick a series of squares of a given colour along side debug information describing what elements the colors represent. Right now, though, I can't figure out how to take a string of text and print that to the Texture2D I'm exporting as a PNG.
Every answer I find starts with telling the person asking to set up a camera and objects in a scene and a GUI, and that feels like massive, high-maintenance overhead for this situation. Again, I don't have any of those things active during the entire time this stuff is running, and I don't want them. This is essentially a bunch of pre-processing. How would I just take the text and a font I want to print it in, and get a 2D texture straight from that that I can directly add to my Texture2D?
Any information on this would be greatly appreciated!
Answer by GarrickWinter · Jul 02, 2020 at 09:10 PM
So I've still yet to find somethind truly satisfactory, but so far this is the least cumbersome or irritating solution I've found: https://answers.unity.com/questions/513338/text-to-textureimage.html
It's still not ideal because
Displaying the text on the screen means I can't produce a transparent image with ONLY the text, which I could then happily stamp onto other images; I need a solid background colour to hide whatever's happening in the background.
Displaying the text on the screen, even for just one frame, obviously means the player might see it, so this isn't great for any situation where I'd want to do this during the game.
It doesn't provide me an immediate response, because I need to display the text with OnGUI and then wait till the end of the frame. This means whatever is trying to convert text to image needs to make a request and then stop and wait for the request to be fulfilled, which is a bit more code overhead than I'd like.
Doing multiple prints of text to textures requires either doing them one after the other, which would take several frames, which increases the risk of #2 happening; or batching them. Batching them requires spacing the requested text across the screen, which is more code to write, and increases the risk of #2 being severe when it does happen.
Because this relies on screenspace, there's an inherent limit to how much I can convert at once. This shouldn't be a problem unless doing a lot of text at once, but it could certainly become an issue if you're trying to print whole paragraphs.
So if anyone has a better solution, please let me know.
Your answer
Follow this Question
Related Questions
How to properly display debug messages on UI 0 Answers
Text to Texture2d / png 0 Answers
Text UI looks different (same font) ?? 1 Answer
Using Japanese in my game 1 Answer
How to detect the length of an input field [C# noob] 1 Answer