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
0
Question by jaja1 · Nov 27, 2014 at 03:13 AM · cursormousepositionwindow

Issues with mouse hovering over GUI element [SOLVED]

I am trying to implement some code that changes the cursor texture when the mouse hovers over a particular portion of the player's inventory. Also, when the player clicks the item with that space, the cursor changes to the item's texture. If none of these conditions are met the cursor simply defaults to the main cursor texture. Here's what I have so far:

 if(main_bag[0] != "")
         {
             float posx = myBagWindowSize.width*(64f/1024f);
             float posy = myBagWindowSize.height*(352f/640f);
             float sizex = myBagWindowSize.width*(64f/1024f);
             float sizey = myBagWindowSize.height*(64f/640f);
             Rect slot = new Rect(posx, posy, sizex, sizey);
             GUI.DrawTexture(slot, _Icons.GetIcon(main_bag[0]));
 
             #region this does not work
             if(slot.Contains(Input.mousePosition))
             {
                 Cursor.SetCursor(grab_ico, new Vector2(grab_ico.width/2, grab_ico.height/2), CursorMode.Auto);
             }
             else Cursor.SetCursor(main_cursor, new Vector2(0,0), CursorMode.Auto);
 
             if(slot.Contains(Input.mousePosition) && Input.GetMouseButtonUp(0))
             {
                 main_bag_item_click = true;
             }
 
             if(main_bag_item_click)
             {
                 Cursor.SetCursor(_Icons.GetIcon(main_bag[0]), new Vector2(sizex/2, sizey/2), CursorMode.Auto);
             }
             #endregion
         }

What happens is that only the default cursor texture is shown. The code block with the region is the issue. Trying to do this in the Update function proves some what tedious because the rectangle I am using only seems to 'work' when in the window's function. Otherwise, the position of the rectangle changes if I move the window. Can anyone provide a possible solution?

EDIT: Use Event.current.mousePosition in place of Input.mousePosition

Comment
Add comment · Show 6
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 jaja1 · Nov 27, 2014 at 05:24 PM 0
Share

bump..........

avatar image Radivarig · Nov 27, 2014 at 05:31 PM 0
Share

Call it from inside the window, that way position will stay local

avatar image jaja1 · Nov 27, 2014 at 06:17 PM 0
Share

It actually is being called within the window function. Sorry I should have mentioned this earlier. Here is the full code essentially:

 void myBagWindow(int id)
     {
         if(GUI.Button(new Rect(myBagWindowSize.width*0.97f, 2, 20, 15), " "))
         {
             check$$anonymous$$yBag = false;
         }
 
         GUI.DrawTexture(new Rect(0, 0, myBagWindowSize.width, myBagWindowSize.height), bagTexture);
 
         #region main bag display
         if(main_bag[0] != "")
         {
             float posx = myBagWindowSize.width*(64f/1024f);
             float posy = myBagWindowSize.height*(352f/640f);
             float sizex = myBagWindowSize.width*(64f/1024f);
             float sizey = myBagWindowSize.height*(64f/640f);
             Rect slot = new Rect(posx, posy, sizex, sizey);
             GUI.DrawTexture(slot, _Icons.GetIcon(main_bag[0]));
 
             #region this does not work
             if(slot.Contains(Input.mousePosition))
             {
                 Cursor.SetCursor(grab_ico, new Vector2(grab_ico.width/2, grab_ico.height/2), Cursor$$anonymous$$ode.Auto);
             }
             else Cursor.SetCursor(main_cursor, new Vector2(0,0), Cursor$$anonymous$$ode.Auto);
 
             if(slot.Contains(Input.mousePosition) && Input.Get$$anonymous$$ouseButtonUp(0))
             {
                 main_bag_item_click = true;
             }
 
             if(main_bag_item_click)
             {
                 Cursor.SetCursor(_Icons.GetIcon(main_bag[0]), new Vector2(sizex/2, sizey/2), Cursor$$anonymous$$ode.Auto);
             }
             #endregion
         }
         if(main_bag[1] != "")
         {
             float posx = myBagWindowSize.width*(128f/1024f);
             float posy = myBagWindowSize.height*(352f/640f);
             float sizex = myBagWindowSize.width*(64f/1024f);
             float sizey = myBagWindowSize.height*(64f/640f);
             GUI.DrawTexture(new Rect(posx, posy, sizex, sizey), _Icons.GetIcon(main_bag[1]));
         }
 }
avatar image jaja1 · Nov 28, 2014 at 08:33 PM 0
Share

bump again...

avatar image Radivarig · Dec 04, 2014 at 05:58 PM 0
Share

Did you resolve this?

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

27 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Is it possible to not allow the in-game cursor through certain sprites? 1 Answer

How do I send my cursor to a sprite 2D 1 Answer

Glue mouse to object 2 Answers

Move toward Mouse Cursor (RaycastHit), without following? 1 Answer

How To Check Which Side of The Screen Your Cursor Is On? 1 Answer


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