Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 TaliDen · Jan 19, 2014 at 09:27 PM · 2djavascriptiosprefabguitexture

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

Comment
Add comment · Show 1
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 robertbu · Jan 19, 2014 at 09:30 PM 0
Share

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.

4 Replies

· Add your reply
  • Sort: 
avatar image
4

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.

alt text

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);
     }
 }

capture.png (38.9 kB)
Comment
Add comment · Show 2 · 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 roccosaya · Aug 10, 2016 at 06:47 AM 0
Share

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!

avatar image youblistermypaint roccosaya · Aug 10, 2016 at 01:23 PM 0
Share

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.

avatar image
3

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.

Comment
Add comment · Show 1 · 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 TaliDen · Jan 20, 2014 at 02:31 PM 0
Share

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?

alt text

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?

example.png (34.6 kB)
avatar image
1

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.

Comment
Add comment · 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
1

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.

Comment
Add comment · Show 1 · 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 corpsinheretoo · Sep 09, 2017 at 06:16 AM 0
Share

I had searched many threads on this subject - most had very complicated answers; this was so simple and just worked :)

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.

Follow this Question

Answers Answers and Comments

23 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 avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges