- Home /
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;
}
}
}
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.
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
Follow this Question
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