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 · 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 shifted 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))
             {
                 whileSelecting = 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) && !whileSelecting)
         {
             #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
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 jaja1 · Dec 02, 2014 at 05:25 PM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
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
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
0

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
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 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.

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 TO: Sliding GUI Window? 1 Answer

GUI.Window function not called anymore 1 Answer

using c# script to open/close GUI window 1 Answer

Distribute terrain in zones 3 Answers

C# Rows and Columns GUILayout.Window Issues 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