Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by Blitzerine · Aug 04, 2011 at 04:10 PM · guipositionmouseboxget

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

hey i was thinking about the mouse position

  var e : Event = Event.current;
  var xMousePosition = e.mousePosition.x;
  var yMousePosition = e.mousePosition.y;
         
         function OnGUI () {
             if (Input.GetMouseButton(1))
             GUI.Box (Rect (xMousePosition,yMousePosition,120,120), "");
         }

i want when i right click it gets the current mouse position at which right clicked and show the GUI box there ..can any one help me

Comment
Add comment · Show 1
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 rizqiaws · Sep 05, 2014 at 05:34 PM 0
Share

Anyone can help me to change this script in c#? I need help :(

2 Replies

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

Answer by aldonaletto · Aug 04, 2011 at 04:22 PM

You've almost got it:

function OnGUI () {
    if (Input.GetMouseButton(1))
        GUI.Box (Rect (Input.mousePosition.x,Input.mousePosition.y,120,120), "");
}
But the box will be open only while you're holding the right button down - and it will follow the mouse pointer if it moves. If you want the box to appear and remain openned, do the following:

var boxOpen: boolean = false; var boxPos: Vector3;

function Update () { if (Input.GetMouseButtonDown(1)){ boxPos.x = Input.mousePosition.x; // Y runs up to down in GUI, so let's invert it boxPos.y = Screen.height - Input.mousePosition.y; boxOpen = true; } }

function OnGUI () { if (boxOpen) GUI.Box (Rect (boxPos.x,boxPos.y,120,120), ""); } You must set boxOpen to false to close it.

EDITED: Y runs upside down in GUI system, so it must be inverted (0 is Screen.height)

Comment
Add comment · Show 3 · 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 Blitzerine · Aug 05, 2011 at 07:22 AM 0
Share
 var boxOpen: boolean = false;
 var boxPos: Vector3;
 
 function Update () {
     if (Input.Get$$anonymous$$ouseButtonDown(1)){
         boxPos = Input.mousePosition;
         boxOpen = true;
     }
 }
     
 function OnGUI () {
     if (boxOpen)
         GUI.Box (Rect (boxPos.x,boxPos.y,120,120), "");
 }



this all i wanted but its open the gui at correct X position ..Y position is notcorect i mean it should open where i clicked the Xpos is correct

avatar image aldonaletto · Aug 05, 2011 at 10:15 AM 0
Share

@Blitzerine, you're right! I forgot this: Y is inverted in the GUI system - 0 is at the top, while the bottom is Screen.height. I've edited my answer to correct this (only changed the line where boxPos was set to mousePosition)

avatar image Blitzerine · Aug 05, 2011 at 11:07 AM 0
Share

Yay thanks now it opens at correct position

avatar image
0

Answer by Blitzerine · Aug 05, 2011 at 12:16 PM

and to close with Left click i made this for others ^^

 var boxOpen: boolean = false;
 var boxPos: Vector3;
 
 function Update () {
     if (Input.GetMouseButtonDown(1)){
         boxPos.x = Input.mousePosition.x;
         boxPos.y = Screen.height - Input.mousePosition.y;
         boxOpen = true;
     }
     if (Input.GetMouseButtonDown(0)){
         boxOpen = false;
     }
 }
     
 function OnGUI () {
     if (boxOpen)
         GUI.Box (Rect (boxPos.x,boxPos.y,140,180), "");
 }
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 Blitzerine · Aug 05, 2011 at 12:17 PM 0
Share

This Question is CO$$anonymous$$PLETE NOW ^^

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Setting the mouse position to specific coordinates 5 Answers

How to set the mouse x and/or z position as a variable 1 Answer

Locking cursor without changing position 1 Answer

GUI.Button on MouseHover 1 Answer

Get Mouse Position GUI 3 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