Draw text on a texture at runtime?

Hello everyone,

I’m currently working on a trading card game in Unity. A card is basically a texture displayed on a plane. Now, I’ve a template for those textures and I would like to fill in the stats of the card (numbers and text).

Is there a way to draw text on a texture at runtime? Effectively, this would mean rendering text to the pixels of the texture on the fly. Can this be done?

Greetings,

Alan

Generally you would just render the text separately. For a quick start you can use gui text. Long term there are a number of libraries (such as EZ GUI) that handle bitmap fonts. There’s also a few free/open source examples you can find by searching the forums (or the web).

@JohnnyA: So you basically suggest to use a gui text to “overlay” the actual texture? Interesting… I didn’t think of it that way yet. But I have to say that I like the idea, actually. I would have to use 3Dtext objects instead of gui text because my cards are real 3D objects. Working out an appropriate prefab object with the text at the right positions should not be too difficult here but make displaying the card stats really easy - it would boil down to just alter the text of the 3d text objects - I love that idea :slight_smile:

Thanks,

Alan

you could get what you want here.
http://blog.almostlogical.com/2010/08/20/adding-text-to-texture-at-runtime-in-unity3d-without-using-render-texture/

Hi,

thanks for the reply! However, I’m using multiple (grouped) objects now (as described above) which works really well for my purposes, so I will stick with that solution. It is nice to know, though, that there would be an alternative, if needed :slight_smile:

Thanks,

Alan

Could I know your way to implement it?
I am working with that problem too.
First way is what i mentioned above but it has some problem I think.
And I am thinking of way to draw Text in plugin by using OpenGLES now.
If you did it, could you help me with your solution?

Thank you,
Best Regards,
Adam.

Hi,

I’m not really sure if this fits your needs since it is a pretty simple approach (beginner sitting here :)), but here is what I did:

  • Created the object I wanted to draw text on in my scene
  • Created a 3D Text object
  • Assigned this shader to it: http://www.unifycommunity.com/wiki/index.php?title=3DText
  • Parented the 3D text object to the other object
  • Moved the 3D text until I was satisfied with the position relative to the parent
  • Saved the parent object as a prefab

This is very limited, because I imagine that it will only work well for flat objects (since I’m working on a trading card game, this is no problem ;)). As soon as there are bumps, edges and corners in your object you will most likely have no other way but to paint directly onto the texture… This solution is basically a “fake” in a sense that it positions an entirely different object in front of an existing one, so it just looks as if a text has been painted on the texture, but in fact, the text just visually overlays the texture.

I hope this helps!

Greets,

Alan

You mean you didn’t need to draw text on texture and used another way?
But I wanted to know the way to draw 2D text(3D is ok) on texture.
Thanks for your reply.

Regards,
Adam.