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 NutellaDaddy · May 16, 2014 at 02:06 AM · c#guibuttondraganddrop

Getting almost nowhere with my buttondrag script

I am trying to make it so that you can drag a gui button using script ,but my script isn't working so far. I tried to make it so that when you click the button it then follows the position of your mouse from the position it started in...that didn't work.What did I do wrong? I need to detect if the mouse is staying down on the button and then how far the mouse has moved. I commented some lines in the script to show you what I'm trying to do. Any help for me. Thanks!

 using UnityEngine;
 using System.Collections;
 
 public class InventorySystem : MonoBehaviour 
 {
     //RANDOM VARIABLES
     private bool inventoryOn;
     public Texture emptyTex;
 
     //INVENTORY VARIABLES
     public GUIStyle buttonStyle;
     private int buttonSize = 70;
     private int sBB = 4;
 
     //SPACES IN THE INVENTORY
     private int cols = 4;
     private int inventorySpaces = 16;
 
     //DRAGGING THE SPACES
     private Vector2 slot;
     private bool dragging;
     private Vector2 mousePos;
 
     void Start ()
     {
         inventoryOn = false;
         dragging = false;
         mousePos = Input.mousePosition;
     }
     void Update () 
     {
         //INVENTORY ON AND OFF
         if(Input.GetKeyDown(KeyCode.Tab))
         {
             if (inventoryOn == false) 
             {
                 inventoryOn = true;
             }
             else if(inventoryOn == true)
             {
                 inventoryOn = false;
             }
         }
         Vector2 mousePos = Input.mousePosition;
     }
     void OnGUI()
     {
         InventoryLogic ();
     }
     void InventoryLogic()
     {
         int cnt = 0;
         int x = 0;
 
         //IF THE INVENTORY IS ON
         if (inventoryOn == true)
         {
             for( x = 0; x < inventorySpaces; x++)
             {
                 //STARTING POSITIONS OF THE SLOTS
                 slot = new Vector2((x % cols) * (buttonSize + sBB) , (int)x / cols * (buttonSize + sBB));
 
                 if(cnt < Player.Inventory.Count)
                 {
                     //SLOTS WITH ITEMS INSIDE OF THEM
                     if(GUI.Button(new Rect(slot.x,slot.y,buttonSize,buttonSize),Player.Inventory[cnt].icon,buttonStyle))
                     {
                         dragging = true;
                     }
                     
                 }
                 else
                 {
                     //EMPTY SLOTS
                     //CREATE THE SLOTS HERE
                     if(GUI.Button(new Rect(slot.x,slot.y,buttonSize,buttonSize),string.Empty, buttonStyle))
                     {
                         //TESTING PURPOSES SET DRAGGING TO TRUE
                         dragging = true;
                     }
                 }
                 cnt++;
             }
         }
 
         if (dragging == true) 
         {
             // IF DRAGGING IS TRUE THAN TAKE THE ORIGINAL  X AND Y VECTORS FOR THE SLOT AND + mousePos
             slot = new Vector2((x % cols) * (buttonSize + sBB) + mousePos.x , (int)x / cols * (buttonSize + sBB) + mousePos.y);
         }
         if (Input.GetMouseButtonUp (0))
         {
             //MAKE DRAGGING FALSE WHEN YOU LET GO OF THE BUTTON
             dragging = false;
         }
     }
 }
 
Comment
Add comment
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

1 Reply

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

Answer by robertbu · May 16, 2014 at 02:53 AM

A quick read spots two problems. First your mousePos is only being set in Start(). On line 44 you have:

  Vector2 mousePos = Input.mousePosition;

Because you have 'Vector2' at the beginning of the line, you are declaring a local variable to the Update() function and assigning Input.mousePosition. This variable goes away when you return from Update(), and it hide the variable by the same name at the top of the file.

But beyond that problem, you should not be using Input.mousePosition inside OnGUI(). Input.mousePosition uses screen coordinates which start in the lower left of the screen. GUI uses GUI Coordinates which start in the upper left of the screen. There is a conversion but the right thing to do is to use 'Event.current.mousePosition'. This will be in GUI coordinates.

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 NutellaDaddy · May 16, 2014 at 03:06 AM 0
Share

I think I'm going to try NGUI it seems like something that most people recommended. Your solution did work out my problem though so write your answer and I will check it as correct Thank you.

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

19 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

Related Questions

Using GUI Button to activate script instances 1 Answer

C# Why Won't My GUI Layout Button Appear? 1 Answer

Unity GUI multiple grids? 0 Answers

Having trouble moving item from one list to another! 1 Answer

controlling GUI.Button state 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