• 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
Question by jaja1 · Dec 02, 2014 at 06:01 AM · c#guiwindow

Elements in draggable window do not remain fixed [SOLVED]

I have the following code implemented in the draggable window function. The idea is that a player can open their inventory and move around the textures in various "slots" that are defined int he function. However, when the window is s$$anonymous$$fted the slots move along the y-axis. The x-axis seems to be fine and remains fixed. The code block is for the first inventory slot:

 if(GUI.Button(new Rect(myBagWindowSize.width*0.97f, 2, 20, 15), " "))
         {
             checkMyBag = false;
         }
 
         GUI.DrawTexture(new Rect(0, 0, myBagWindowSize.width, myBagWindowSize.height), bagTexture);
 
         #region main bag display
         float sizex = myBagWindowSize.width*(64f/1024f);
         float sizey = myBagWindowSize.height*(64f/640f);
         Vector2 invertedMousePos = GUIUtility.ScreenToGUIPoint(Input.mousePosition);
         invertedMousePos.y = myBagWindowSize.height - invertedMousePos.y;
 
         float pos0x = myBagWindowSize.width*(64f/1024f);
         float pos0y = myBagWindowSize.height*(352f/640f);
         Rect slot0 = new Rect(pos0x, pos0y, sizex, sizey);
         if(main_bag[0] != "")
         {
             #region ...code
             GUI.DrawTexture(slot0, _Icons.GetIcon(main_bag[0]));
 
             if(!global_item_selected && slot0.Contains(invertedMousePos))
             {
                 main_bag_item_hover[0] = true;
                 Cursor.SetCursor(grab_ico, new Vector2(grab_ico.width/2, grab_ico.height/2), CursorMode.Auto);
             }
             else if(!global_item_selected && !slot0.Contains(invertedMousePos) && main_bag_item_hover[0])
             {
                 main_bag_item_hover[0] = false;
                 Cursor.SetCursor(main_cursor, new Vector2(0,0), CursorMode.Auto);
             }
 
             if(!global_item_selected && slot0.Contains(invertedMousePos) && Input.GetMouseButtonUp(0))
             {
                 w$$anonymous$$leSelecting = true;
                 Cursor.SetCursor(_Icons.GetIcon(main_bag[0]), new Vector2(sizex/2, sizey/2), CursorMode.Auto);
                 main_bag[0] = "";
                 global_index_from = 0;
                 global_item_selected = true;
             }
             #endregion
         }
         else if(global_item_selected && slot0.Contains(invertedMousePos) && Input.GetMouseButtonUp(0) && !w$$anonymous$$leSelecting)
         {
             #region ...code
             global_item_selected = false;
             item_replaced_bagType = "main";
             global_index_to = 0;
             if(main_bag[0] != "")
             {
                 Cursor.SetCursor(_Icons.GetIcon(main_bag[0]), new Vector2(sizex/2, sizey/2), CursorMode.Auto);
             }
             item_replaced = true;
             #endregion
         }
Comment

People who like this

0 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 jaja1 · Dec 02, 2014 at 05:25 PM 0
Share

bump...........

2 Replies

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

Answer by jaja1 · Dec 03, 2014 at 05:55 PM

Solved:

Answer is to use Event.current.mousePosition in place of Input.mousePosition. Using that I can avoid the lines : Vector2 invertedMousePos = GUIUtility.ScreenToGUIPoint(Input.mousePosition); invertedMousePos.y = myBagWindowSize.height - invertedMousePos.y;

and simply use: Vector2 invertedMousePos = Event.current.mousePosition;

Comment

People who like this

0 Show 0 · 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

Answer by unimechanic · Dec 03, 2014 at 02:45 PM

Make its coordinates relative to the window, currently you only do that for the X axis:

new Rect(myBagWindowSize.width*0.97f, 2, 20, 15)

Comment

People who like this

0 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 jaja1 · Dec 03, 2014 at 05:36 PM 0
Share

Got excited for a second there...sorry the button is actually to close the window and I'm not having problems with that. The "slot" rect is what moves along the y-axis when the window is shifted.

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

26 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

Related Questions

How do I tell my code which window the game runs in? 0 Answers

Having a GUILayout Window position immediately at screen center 5 Answers

Distribute terrain in zones 3 Answers

HOW TO: Sliding GUI Window? 1 Answer

Multiple Cars not working 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