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 cidmodder · Jun 11, 2013 at 12:42 AM · c#guionmouseovertextbox

How to get a pop-up window floating next to a gameobject?

I'm trying to get a GUI popup window with selectable options to pop up next to NPCs when you mouse over them and press e and be used as a way to talk to them. Something like Skyrim I suppose. I've been away from programming a while and I've remembered how to get the mouse over working but not the GUI stuff. I know it's not much but here is what I have so far:

 using UnityEngine;
 using System.Collections;
 
 public class TalkingTestScript : MonoBehaviour {
     public Rect windowRect = new Rect(0,0,0,0);
     public int lookingat = 0;
     
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     
     }
     
     void OnMouseOver() {
         lookingat = 1;

         //does GUI stuff go here or later?
     }
         

         }

I've been playing around with the GUI stuff from the manual but I'm not getting very far and I'm not sure what to do instead. Thanks a lot for any help!

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
2

Answer by TonyLi · Jun 11, 2013 at 01:41 AM

As a first pass, add another method called OnGUI(). GUI controls use screen coordinates. You'll probably want to make the GUI appear near the NPC. To do this, you can use use Camera.WorldToScreenPoint():

 void OnGUI() {
     // Bail out immediately if not moused over:
     if (!lookingat) return;

     // Get the screen position of the NPC's origin:
     Vector3 screenPos = Camera.main.WorldToScreenPoint(transform.position);

     // Define a 100x100 pixel rect going up and to the right:
     Rect menuRect = new Rect(screenPos.x, screenPos.y - 100, 100, 100);

     // Draw a label in the rect:
     GUI.Label(menuRect, "Menu Goes Here");
 }

As a first pass, just try to get a label or a button to pop up, using something like the code above. (Warning: I didn't test it; I just typed it into the answer box.)

Then you swap in a GUI.Window. Windows are more complicated, so just make sure you get basic GUI elements showing where you want first. Info on windows are at the bottom of this page: http://docs.unity3d.com/Documentation/Components/gui-Controls.html

Down the road, you may want to put the GUI stuff in a separate script that you enable only when the player is mousing over the NPC. But I recommend just getting it working now, and read up on GUI optimization later.

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

15 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

Related Questions

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer

Distribute terrain in zones 3 Answers

How can I clear a textbox and repeat the text with my effect? 0 Answers

Multiple Cars not working 1 Answer

How to dynamically change the text in Unity(Augmented Reality + NYARtoolkit(C#)) ? 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