- Home /
Text To Texture/Image
I need to take text from a GUI.TextArea, with word wrapping and all the style, and apply it to some kind of image or texture that I can then save to file.
I have tried to create a RenderTexture and draw the GUI Text area to it, but as I have learnt, that GUI elements aren't processed to RenderTextures.
I have also tried this solution to no avail. It doesn't help that the Mapper is OSX only.
My other searches usually dug up answers for Unity 2.x or below. I'm starting to run out of ideas.
So is there any new solutions I'm missing, or is this really not possible without manually grabbing each bitmap font and pasting it on individually? Because I'd really like to avoid that if possible.
Please and thank you.
Answer by Valeour · Aug 13, 2013 at 01:45 PM
Okay, found my own answer. Will post for people looking for a similar answer:
Basically I just grabbed the pixel data from what I assume is the frame buffer. I tried this before, but I would always get the wrong area of the image. Turns out I forgot that the Y axis is reversed, which is where my issues came from. Would have saved me a lot of time if I knew/remembered that.
Anyway, hope this helps someone.
private string textData = "";
private Rect TextAreaRect = new Rect( 25, 25, 250, 100);
void Update() {
//Whatever condition you want to start the capture.
if(capture_condition) {
StartCoroutine(CaptureTextArea());
}
}
void OnGUI() {
//What we want to convert to image/texture
textData = GUI.TextArea(TextAreaRect, textData);
}
IEnumerator CaptureTextArea() {
//Run at the end of frame so we get the most recent data
//(otherwise Unity kicks up a fuss about buffers).
yield return new WaitForEndOfFrame();
int width = Mathf.FloorToInt(TextAreaRect.width);
int height = Mathf.FloorToInt(TextAreaRect.height);
Texture2D text_texture = new Texture2D(width, height, TextureFormat.RGB24, false);
Rect tempRect = TextAreaRect;
//There's probably a tidier way to calculate this:
//Flip to match the Y axis properly.
tempRect.y = Screen.height - TextAreaRect.y - TextAreaRect.height;
//Grab the pixels from the system buffer.
text_texture.ReadPixels(tempRect, 0, 0);
text_texture.Apply(); //Apply the read.
//...
}
Hey there! Just wanted to say, from 7 years in the future, thank you! This did indeed help me.
It's not as good a solution as I would have hoped (without the immediacy of a straightforward text-to-Texture2D conversion I'm going to need to do some listening, and in my case I never want to display this stuff, so I have to just kind of hope it doesn't get noticed...), but it seems Unity has very poor support for turning text into textures that can be saved to disk, and this is the least-bad option I could find.
So, from 2020, thanks again. Also, when you get to around October of 2019, you'll want to buy a lot of hand sanitizer and cloth masks, and store them away safely for a few months. Just trust me.
Wow! Thank you for that blast from the past. I'm glad it helped you, seeing your reply made my history as a unity programmer fly past my eyes. I was a student when I wrote this working on a freelance project! Very humbling. :)
Thinking what I would do differently now; I would render a canvas/GUI component to a disabled camera, and call Camera.Render() manually, with a render texture target. This would be far more manageable to use, with must better quality control. (This might be tricky if you're using URP however, due to how multiple cameras can be... painful.)
Thank you again, and I wish I could heed your warnings oh-sacred time traveller!
Hello, I tried to use it but nothing changed. What do I miss?