• 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
4
Question by yatagarasu · Nov 14, 2013 at 11:21 AM · spritetextunity4.3

How to draw text over sprite

How can I draw text label over Sprite (new Unity 4.3) feature.

I have sprite hirarchy

 Screen (base background sprite: order in layer = 0)
 -> LoadingProgress (progress bar background sprite: order layer = 1)
  +--> Progress (progress bar bar sprite: order layer = 2)
  +--> Text (progress bar text)

I have progress bar script (on LoadingProgress GameObject) which automatically takes Progress and Text objects if they exist and updates Progress scale and Text text to current progress values (ex. 10/100)

The problem is Text is not visible.

Have tried to make Text as TextMesh - than it is only visible if progress is empty - Progress sprite is drawn on top of Text.

Have tried to make Text a GUIText - but have a problem converting world coordinates to GUI coordiantes. GUIUtility.ScreenToGUIPoint(Camera.main.WorldToScreenPoint(text.transform.position)) always returns (0, 0) so text is drawn in lower left corner of the screen.

upd: Figure out that GUIUtility.ScreenToGUIPoint is only working in OnGUI() function.

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

8 Replies

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

Answer by mugathur · Nov 18, 2013 at 11:06 AM

Currently, all non-sprites seem to go into the "Default" sorting layer. So if you put any sprites in a Sorting Layer or Order in Layer that is in front of Default/0, no amount of Z offsetting is going to get the non-sprite in front of the sprite. For now, I'm putting my UI Sprites back in my "Default" sorting layer and using position.z and the Offset Z field in TextMesh to push it in front of my sprite.

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 yatagarasu · Nov 18, 2013 at 11:21 AM 0
Share

Ha, yes this works. Also I found that it is possible to use negative Order in Layer. To pity I already wrote my text to sprite renderer.

avatar image isteffy · May 09, 2016 at 08:48 AM 0
Share

If you have a good reason for changing EVERY gameobjects sorting layer to Default, then go ahead, but this is a huge change for something as small as text rendering.

avatar image isteffy · May 09, 2016 at 09:08 AM 0
Share

That being said, none of the other answers have worked. So this answer takes the win!

avatar image
8

Answer by Neakiir · Mar 12, 2014 at 01:32 PM

Another option I have found is to put the textmesh entity's renderer to be the same layer as another entity's.

I have put my textmesh as a child of an entity with a sprite renderer and set the sorting layer to be that of the parent's.

 void Start () 
 {
     this.renderer.sortingLayerID =   
     this.transform.parent.renderer.sortingLayerID;
 }
Comment
Add comment · Show 4 · 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 Marc-Uberstein · Aug 02, 2014 at 03:13 PM 0
Share

Works great! Thanks!

avatar image matshofman · Aug 21, 2014 at 08:43 AM 0
Share

This worked for me aswell :)

avatar image taxvi · Dec 28, 2015 at 09:16 AM 0
Share

I think this should be accepted as the correct answer

avatar image squidish2 · Jan 19, 2016 at 12:05 AM 0
Share

This is a nice solution, thanks.

avatar image
2

Answer by lordpaladin44 · Oct 23, 2015 at 12:20 PM

Textmesh components are Renderer components as noted in a previous answer. Its easy to add your own component that sets the renderer SortingLayerID or SortingOrder to match the sprite you want the text to sit on top of (to be Z based). See sample.

 public class RendererSortingLayer : MonoBehaviour {
     public int SortLayer = 0;
     public int SortingLayerID = SortingLayer.GetLayerValueFromName("Default");
     // Use this for initialization
     void Start () {
         Renderer renderer = this.gameObject.GetComponent<Renderer>();
         if(renderer != null)
         {
             renderer.sortingOrder = SortLayer;
             renderer.sortingLayerID = SortingLayerID;
         }
     }
     
 }
 
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 shopguy · Jul 21, 2015 at 05:20 AM

Another option is to place the "Default" default sorting layer in front of all other layers (drag/drop in editor).

Of course none of these answers are perfect, but just another option that might work for some. Would be nice if we could get a sorting layer for a TextMesh, that we can set in the editor.

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
0

Answer by yatagarasu · Nov 14, 2013 at 12:14 PM

Ok, I figured out that I should scale screen coordinates.

 Vector2 screenPosition = Camera.main.WorldToScreenPoint(text.transform.position);
 Vector2 guiPosition = new Vector2(screenPosition.x / Screen.width, screenPosition.y / Screen.height);
 text.transform.position = GUIUtility.ScreenToGUIPoint(guiPosition);

Works ok. But one question still remains - Is it possible to draw TextMesh over sprite?

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 kelvinc1024 · Nov 17, 2013 at 02:46 PM 0
Share

have same question, any one have solution?? i want to add text in front of my sprite button

gui text seem not what i'm looking for

  • 1
  • 2
  • ›

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

Setting up text and images on the surface of a plane? 1 Answer

How do you add text to a sprite? 2 Answers

SpriteRenderer look not clear in unity4.3? 0 Answers

How to attach text to a sprite? 0 Answers

Background sprite in TextMeshPro 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