- Home /
Render text on sprite prefab - 2D iOS
Hello all,
I'd like to ask whether it would be possible to render/draw text dynamically on prefabs. Specifically, what I would like to do is have planets falling from the top of the screen (that would be generated from a sprite prefab) and on top of each one's surface to display a different word, which is taken from a JSON file. Additionally I'd like the text to follow the downwards motion of the planet it's drawn on. Is this something that would be possible with the free version of Unity? And what would be the workflow for building something like that?
I would hope this could be done via GuiTextures or something, but I haven't found a way to build it yet or any relevant tutorials. The only alternative I can think of is creating a sprite for each one of the words separately and loading them all to the scene, but I'm afraid this will soon create a mesh.
Any suggestions would really help! Thank you
Not sure of all of your parameters here. For text in world space you can use Text$$anonymous$$eshes (3D Text). You can also track world space object using GUIText() or GUI.Label(). I'm not sure about the nature of your falling planets, but you might consider making the planets GUITextures ins$$anonymous$$d of Sprites and then using a GUIText for the label.
Answer by youblistermypaint · Sep 27, 2015 at 05:17 PM
To expand on Eric5h5's answer. You would want to set up a GameObject with a TextMesh component as a child of your Sprite.
And then attach the following script to it:
using UnityEngine;
public class SpriteText : MonoBehaviour
{
void Start()
{
var parent = transform.parent;
var parentRenderer = parent.GetComponent<Renderer>();
var renderer = GetComponent<Renderer>();
renderer.sortingLayerID = parentRenderer.sortingLayerID;
renderer.sortingOrder = parentRenderer.sortingOrder;
var spriteTransform = parent.transform;
var text = GetComponent<TextMesh>();
var pos = spriteTransform.position;
text.text = string.Format("{0}, {1}", pos.x, pos.y);
}
}
Hi, I did this, and it "kinda" works. I've done what you suggested (thank you)
However, the text is behind the sprite ins$$anonymous$$d of in front of it. I have a sprite as a background assigned to a Sorting Layer named _background, and the sprite (button) in which I added the text mesh to, is on a Sorting Layer named_foreground.
The text is rendering over the background, but it is behind the foreground. I see no options unfortunately for the GameObject to assign it a particular sorting layer.
The GameObject is a child of the button.
Any ides how I could fix this? Thank you so much!
It's been awhile since I did anything with this script but I imagine you would need to mess with your Z position of your sprite and / or the text to get it to layer the way you want.
Answer by Eric5h5 · Jan 19, 2014 at 10:01 PM
The simplest way would be to have a TextMesh that's a child of the sprite. Use renderer.sortingLayerID and renderer.sortingOrder (in code) to get the text to draw over the sprite as necessary.
Thank you both for the suggestions, I have just started building this, so I'm gonna try testing both workflows GUITextures+GUIText and Text$$anonymous$$esh to see which one I can implement, and report back.
Not sure if it will make things clearer, but I'm posting an image of what I'm trying to do. I have already made the sprite prefab (yellow icon), which is dynamically created on screen, but I'm not sure what kind of element the word should be (e.g. a GUItext?) to be able to dynamically render on-top of the icon (as I'd like to draw a different word on each falling yellow icon). I guess the word should be part of the same prefab?
Eric5h5, could you maybe give me an example of making the Text$$anonymous$$esh a child of the sprite or code using Renderer.sortingLayerID and renderer.sortingOrder?
Answer by roccosaya · Aug 10, 2016 at 07:54 PM
thanks for the quick reply. Oddly, setting the offset Z to -1, fixed the problem. The -1 is what I find strange. I have no idea why -1 works, just tried a negative number, after trying a few positive ones.
Answer by DrMike91 · Oct 07, 2016 at 04:58 PM
I'm doing a 2D game and needed text to follow a moving Sprite. I just added a GameObject child to the Sprite and added a Text Mesh as a component and changed the Offset Z value to -1 as suggested and all is well. I did not need to do anything with scripting for the mesh. When the sprite move, the text moves.
I had searched many threads on this subject - most had very complicated answers; this was so simple and just worked :)
Your answer
Follow this Question
Related Questions
Destroy Object On Collision? 3 Answers
2D Sprite moving outside screen edges (side bumpers) 0 Answers
Instantiate prefabs before it comes into view 0 Answers
How would I make a route that shows where player moves beforehand? 0 Answers
Changing material orientation dependant on mouse location? 0 Answers