- Home /
Question by
Sobe459 · Dec 01, 2013 at 11:13 PM ·
mouseinventorymouse-dragdraganddrop
Drag and Drop GUI.Button not working in for loop.
I'm trying to make my inventory drag and drop. But when i use a for loop to create my buttons it does not work. I'm just trying to get the buttons draggable at this moment Please pardon my messy code.
using UnityEngine;
public class TestClass : MonoBehaviour
{
bool buttonPressed = false;
// square width of each button in pixels
public int buttonSize = 35;
// number of pixels between the buttons
private int buttonSpacing = 1;
// top-left position of the button grid
private int xOffset = (int)((Screen.width / 3) * 2.55f);
private int yOffset = (int)((Screen.height / 2) * 1.48f);
// number of columns
private int numCols = 5;
// number of buttons
int numButtons = 25;//inventory.Count;
public Rect r = new Rect(0,0,35, 35);
void OnGUI()
{
if(r.Contains(Event.current.mousePosition))
{
if(Event.current.type == EventType.MouseDown)
{
buttonPressed = true;
}
if(Event.current.type == EventType.MouseUp)
{
buttonPressed = false;
}
}
#region invetorybutton
for (int i = 0; i < numButtons; i++)
{
r.x = xOffset + (i % numCols) * (buttonSize + buttonSpacing); // column position
r.y = yOffset + (int)Mathf.Floor((float)i/numCols) * (buttonSize + buttonSpacing); // row position
if (GUI.Button(r, "" + i))
{
// button was clicked
Debug.Log ("Button " + i + " was pressed.");
}
}
if(buttonPressed && Event.current.type == EventType.MouseDrag)
{
r.x += Event.current.delta.x;
r.y += Event.current.delta.y;
}
#endregion
}
}
Comment
What does not work? Do you get any errors? Unexpected behaviour?
This is what I've come up with after fighting it all night. I can now drag the buttons kinda... I can drag the first button but every button after that moves it and the button that came before it. $$anonymous$$y attempt was trying to store the button positions in an array. I've probably done something funky along the way. Please help. Thank you!
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class HUD2 : $$anonymous$$onoBehaviour {
public int counter = 0;
public Rect buttonRect = new Rect(10, 10, 35, 35);
private bool buttonPressed = false;
public ItemClass[] loot;
public List<ItemClass> actionBar = new List<ItemClass>(); //List of actionbar items
public List<ItemClass> inventory = new List<ItemClass>(); //List of inventory items
public List<Rect> buttonPOS = new List<Rect>();
void Start() {
}
void AddLoot() {
for(int x = 0; x < loot.Length; x++) {
if(inventory.Count < 25) {
inventory.Add(loot[x]);
}
else {
return;
}
}
}
void OnGUI()
{
//Additems
if(GUI.Button(new Rect(300,30,120,80),"Add Items")) {
AddLoot();
}
for(int h = 0; h < inventory.Count; h++){
//buttonRect.x += + h * 36;
if(buttonPOS.Count < 25) {
buttonPOS.Add(buttonRect);
}
if(buttonPOS[h].Contains(Event.current.mousePosition))
{
if(Event.current.type == EventType.$$anonymous$$ouseDown)
{
buttonPressed = true;
}
if(Event.current.type == EventType.$$anonymous$$ouseUp)
{
buttonPressed = false;
}
}
if(buttonPressed && Event.current.type == EventType.$$anonymous$$ouseDrag)
{
Rect v = buttonPOS[h];
v.x += Event.current.delta.x;
v.y += Event.current.delta.y;
buttonPOS[h] = v;
//buttonPOS[h].x += Rect buttonRect = new Rect(0, 0, 35, 35); //Event.current.delta.x;
//buttonPOS[h].y += Event.current.delta.y;
}
GUI.Button(buttonPOS[h], "Draggable Button");
}
}
}