• 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
2
Question by SkyRamon · Feb 15, 2015 at 09:02 PM · 2dspritetextdrawdrawing

2D Draw text at sprite position with script only

I would like to know how i can draw text at the X and Y position of an sprite when i clicked on it. But i realy dont know how to draw text onto the location of the Sprite.

So how can i do that without adding a GUIText to the object. Also I`m using C#

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
0

Answer by SkyRamon · Feb 16, 2015 at 10:12 AM

After doing some research i finally figured out how to draw text on the object i was looking for.

because i encoutered problems with camera is not attach to object i tried using a camera variable, and that worked. also im using a vector3 variable so i can use that everywhere on my script. instead of drgging your object onto the script variable you could also use something called FindObject by Tag or name but im not sure how that works

     public Transform Planet;
     public Camera cameraobj;
     public Vector3 screenPos;

to update and keep track of the objects location i used this code:

     void Update () {
         screenPos = cameraobj.WorldToScreenPoint(Planet.position);
         print("target is " + screenPos.x + " pixels from the left");
     }


and to view the text i used this code: im using screenPos.x-48 to set it exactly in the center of my object also i used MyStyle as GUIStyle to center my text at MiddleCenter then i calculated what the difference was between where the text is supposed to be and where is was now.

e.x : the object is 123px from the left but when i set it manualy in the center it is 103px from the left. so i need to grab the ScreenPos.X wich is equal to 123px and minus it with 20px to center is perfectly...

and i used some colors and sizes...

code:

     void Update () {
         screenPos = cameraobj.WorldToScreenPoint(Planet.position);
         print("target is " + screenPos.x + " pixels from the left");
             OnGui();
     }
 
 
     private void OnGUI() {
         GUI.Label(new Rect(screenPos.x-48,screenPos.y-23, 100, 50), "<color=white><size=40>" + Ships + "</size></color>", MyStyle);
     }



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

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

2 People are following this question.

avatar image avatar image

Related Questions

Pixel Perfect Text 1 Answer

how to render a sprite font with new native 2d commands? 0 Answers

Mapping 2d texture onto another 2d texture 0 Answers

Recommended sprite drawing resolution 1 Answer

Make TextMeshPro text appear over sprite? 1 Answer

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