• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Valeour · Aug 12, 2013 at 01:26 PM · guirendertexturefonttextarea

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
5
Best Answer

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.
  
     //...
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image GarrickWinter · Jul 02, 2020 at 09:03 PM 1
Share

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.

avatar image Valeour GarrickWinter · Jul 03, 2020 at 03:03 PM 1
Share

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!

avatar image Mystheman · Nov 10, 2020 at 05:52 AM 0
Share

Hello, I tried to use it but nothing changed. What do I miss?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is there a way to measure the pixel with/height of a string with a given font? 2 Answers

Why does the font selected for my GUI Skin not display correctly? 1 Answer

Can I set a specific font for a specific font style? 1 Answer

Changing Font Size 3 Answers

Cropped Label Problem - Big Font - Unity3D 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges