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
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.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Recommended sprite drawing resolution 1 Answer

Mapping 2d texture onto another 2d texture 0 Answers

Display UI Text on a 2D Sprite 1 Answer

Basic: Text sub-component of sprite prefab not showing 0 Answers

Sprites not drawing, why? 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