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
1
Question by Youngapprentice · Dec 17, 2011 at 01:59 AM · guipositionmouse

Get Mouse Position GUI

Hi, all! I would like to do this: I have a GUITexture titled 'arrow', and I want it to set its transform.position.y to the transform.position.y of whatever GUILabel the mouse is currently hovering over. Is this possible? How so?

EDIT I have changed the code... The only problem I have now is that when the event fires, the arrow disappears from view, like the y value is off... What's up with that?

The latest bit of code (Edit #3)

 @script ExecuteInEditMode;
 var skin : GUISkin;
 var Choice3 : Rect;
 var Choice2 : Rect;
 var Choice1 : Rect;
 var Answer : Rect;
 var Arrow : GUITexture;
 var mousePos_2D : Vector2;
 function OnGUI(){
     if(skin){
         GUI.skin = skin;
         }else{
         Debug.Log("You Forgot To Add A Skin!");
         }
         
     GUI.Label(Answer,"How are you today?");
     GUI.Label(Choice1,"Well, Thank You");
     GUI.Label(Choice2,"Do I know you?");
     GUI.Label(Choice3,"I am sad.");
     if(Choice1.Contains(mousePos_2D)){
         Arrow.transform.position.y = Choice1.y;
         Debug.Log("Switched");
         }
     
 
 }
 function Update(){
  mousePos_2D = Vector2(Input.mousePosition.x, (Screen.height - Input.mousePosition.y));
  }
Comment
Add comment · Show 2
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 4illeen · Dec 17, 2011 at 12:06 PM 0
Share

you want a custom mouse cursor so that your arrow follows mouse or you want the arrow to 'highlight' current dialog option?

avatar image Youngapprentice · Dec 17, 2011 at 03:16 PM 0
Share

I most definitely want to highlight

3 Replies

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

Answer by Youngapprentice · Dec 17, 2011 at 07:18 PM

Alright. I got the script running quite well! I just had to change the transform into pixelInset.

Thanks for the help guys! -YA

The completed script

 @script ExecuteInEditMode;
 var skin : GUISkin;
 var Choice3 : Rect;
 var Choice2 : Rect;
 var Choice1 : Rect;
 var Answer : Rect;
 var Arrow : GUITexture;
 var makeup : float;
 var mousePos_2D : Vector2;
 function OnGUI(){
     if(skin){
         GUI.skin = skin;
         }else{
         Debug.Log("You Forgot To Add A Skin!");
         }
         
     GUI.Label(Answer,"How are you today?");
     GUI.Label(Choice1,"Well, Thank You");
     GUI.Label(Choice2,"Do I know you?");
     GUI.Label(Choice3,"I am sad.");
     if(Choice1.Contains(mousePos_2D)){
         Arrow.pixelInset = Rect(108,Screen.height - Choice1.y - makeup,16,16);
         Debug.Log("Switched");
         }
     if(Choice2.Contains(mousePos_2D)){
         Arrow.pixelInset = Rect(108,Screen.height - Choice2.y - makeup,16,16);
         Debug.Log("Switched");
         }
     if(Choice3.Contains(mousePos_2D)){
         Arrow.pixelInset = Rect(108,Screen.height - Choice3.y - makeup,16,16);
         Debug.Log("Switched");
         }
     
 
 }
 function Update(){
  mousePos_2D = Vector2(Input.mousePosition.x, (Screen.height - Input.mousePosition.y));
  }

I ended up having to make the 'makeup' variable to keep the arrow aligned with the text

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 fafase · Dec 17, 2011 at 03:35 PM

I am not sure I get this right so I will say considering your "I most definitely want to highlight". So this is going to highlight or change the color of the text when you hover the mouse on top of it like you would get on the menu of a game. I would use a 3DText for your words then add a box collider to each of them and then finally:

 function OnMouseEnter(){
    renderer.material.color = Color.blue;
 }
 functionOnMouseExit(){
    renderer.material.color = Color.white;
 }

This consider that the original color is white and it turns blue when you place the cursor on top of it and gets back to white when you leave. Once again, I am not sure of what you want, just trying to help though.

Comment
Add comment · Show 2 · 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 Youngapprentice · Dec 17, 2011 at 03:41 PM 0
Share

Haha. A bit of a hack solution, but it would work... Still, it is not versatile enough for what I would want to do... Please check my main post, and maybe you'll get a better idea of what I'm talking about. Thanks man!- YA

avatar image fafase · Dec 17, 2011 at 03:43 PM 0
Share

Yep the picture wasn't there (or did not show up) when I posted this.

avatar image
0

Answer by 4illeen · Dec 17, 2011 at 12:19 PM

Ok I know what you mean now. You are almost there.

 if(Choice1.Contains(Input.mousePosition))

This line means that if your mouse cursor is somewhere in the area of Rect called Choice1 it will do something. Since you want it to work for your labels, I suggest to define them all, so you don't have to write the same stuff again and again. So add (160,300,350,70) coords to your Choice1 variable - same for all the others (Choice2, Choice3 etc)

Then it should look like this

 GUI.Label(Choice1,"How are you today?");
     GUI.Label(Choice2,"Well, Thank You");
     GUI.Label(Choice3,"Do I know you?");
     GUI.Label(Choice4,"I am sad.");
     if(Choice1.Contains(Input.mousePosition)){
        Arrow.transform.position.y = Choice1.Y;
        Debug.Log("Switched");
        }

Tell me if this works because I'm not a java guy

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 Youngapprentice · Dec 17, 2011 at 03:34 PM 0
Share

Although this does not work, I makes my life easier, because I get to set the values in the inspector like Lo0Nuhti$$anonymous$$ said. SO $$anonymous$$UCH EASIER THAN$$anonymous$$ YOU!

Sadly, this did not fix my problem. But we are much closer now. The debug.Log is triggering, just not in the expected area. I will edit my main post, add the updated code (see Code#2) and supply a picture about what is happening. thanks for the help so far, guys! - YA

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Setting the mouse position to specific coordinates 5 Answers

Locking cursor without changing position 1 Answer

How To Get Current Mouse Position and have a GUI Box on right clicked 2 Answers

detect mouseover with grid buttons? 1 Answer

Can't get Gui Pos on top of Enemy ingame! 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