• 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
0
Question by omatase · Aug 29, 2014 at 05:29 AM · prefabunitygui

Positioning a GUI.Label inside a prefab

I have a prefab that, at times, will be on the screen multiple times. Each prefab needs text positioned on it. I'm using GUI.Label for showing the text, how can I show it at an offset from wit$$anonymous$$n the prefab? I have created a script that is attached to the prefab itself and I'm drawing the label in the OnGui event. I was really hoping drawing it at 0,0 would put it at the top-left of the prefab but no such luck :(.

Any guidance here would be appreciated. I'm sure it's simple, but I didn't want to jury rig a half-measure and I couldn't find t$$anonymous$$s specific information from searc$$anonymous$$ng.

  • edit --

I'm not sure I have my prefab set up correctly, it feels like it's incomplete. I don't know if I'm supposed to put a collider on it? But I don't t$$anonymous$$nk it technically has a size.

Here's a screenshot. The rectangular box at the top is my "team view" and the label that is painted near the bottom of the window is supposed to be showing in the dark gray bar at the upper portion of the teamview prefab.

alt text

team view prefab.jpg (264.4 kB)
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
Best Answer

Answer by zharik86 · Aug 29, 2014 at 06:17 AM

Simple way, it's use function WorldToScreenPoint(), w$$anonymous$$ch transforms position from world space into screen space. As your script is attached to prefab, for example(write on CSharp):

  void OnGUI() {
   //Use Main Camera and get position current object, but point position is pivot point
   Vector3 screenPos = Camera.main.WorldToScreenPoint(t$$anonymous$$s.transform.position);
   //If pivot point current object at center, we offset it a little. For example 10
   //And size label, for example 100x50
   GUI.Label(new Rect(screenPos.x, Screen.height - screenPos.y, 100, 50), "myLabel");
  }

I hope that it will help you.

Comment
Add comment · Show 5 · 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 omatase · Aug 29, 2014 at 07:42 PM 0
Share

Love it! That sounds like exactly what I need. I'm going to try it when I get home. Thanks @zharik86.

avatar image omatase · Aug 29, 2014 at 09:42 PM 0
Share

K, I tried it and something isn't working. The first thing I wanted to mention was that "this.position" doesn't exist, but this.transform.position does. I think that might be what you were meaning to put in your example. But aside from that when I use that code I get a place that is pretty dang far away from my prefab. I'm not sure if it's because my prefab doesn't have a box collider? I don't think my prefab has a size. Without a size, maybe that's the problem? I've updated my post to include a screenshot of what I'm seeing. Thanks.

avatar image zharik86 · Aug 30, 2014 at 07:42 AM 0
Share

Thanks, of couse, this.transform.position. It's my mistake. I use Unity 4.1.2 and Text show right. Maybe, in later versions a little on another. From your picture I see that, most likely, Screen.height didn't need to use. I update answer. Try this. Also I will check once again a code later.

P.S. Where is pivot point your gray box? Center of box or somewhere else.

avatar image omatase · Aug 30, 2014 at 01:53 PM 0
Share

@zharik86, Thanks for your continued help. The pivot point is in the center of the image. I tried your code but without offsetting. So I did new Rect(screenPos.x, screenPos.y, 150, 60) expecting the label to appear in the center of my box, but it appears where you see in my image in my post.

avatar image omatase · Aug 30, 2014 at 01:57 PM 0
Share

OH I figured it out! The y position is inverted. The y value I get needs to be Screen.height - y. so new Rect(screenPos.x, Screen.height - screenPos.y, 100, 50) positions the label at my pivot point. I can take it from that point. If you update your answer with this new information I will mark it as the correct answer. Thanks again @zharik86

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

22 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

Related Questions

getting prefabs form project to another 3 Answers

instantiate within OnTriggerEnter, c# question. 1 Answer

Instantiating Prefab error C# 1 Answer

How do I re-link an object that has unintentionally become detached from prefab? 0 Answers

Objects flickering between dark and light 7 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