- Home /
How can I make a drag-and-drop list of buttons fed using an array?
In my scene I have a number of items that are to be labeled by the user. I've created a script that makes a list of RepeatButtons based on the names of the items as I've assigned them in the inspector. I want the user to drag the button to the right item in my scene. So far, the buttons don't drag.
Here is what I have:
var refObj : GameObject[];
private var i : int;
private var buttonWidth : int = 0;
var currentPositionX = new Array();
var currentPositionY = new Array();
var label;
var isDragging: boolean;
var subNum : int;
function Start()
{
Shuffle(refObj);
for (var i = 0; i < refObj.Length; ++i)
{
if (buttonWidth < refObj[i].name.Length * 7)
{
buttonWidth = refObj[i].name.Length * 7;
}
}
}
function OnGUI ()
{
label = new Rect [refObj.Length];
for (var i = 0; i < refObj.Length; ++i)
{
currentPositionX[i] = 10;
currentPositionY[i] = 70 + (i * 30);
label[i] = Rect (currentPositionX[i], currentPositionY[i], buttonWidth, 30);
if(GUI.RepeatButton(Rect(label[i]), refObj[i].name))
{
if(Input.GetMouseButton(0))
{
isDragging = true;
subNum = i;
}
}
}
if (isDragging)
{
label[subNum].x = Event.current.mousePosition.x;
label[subNum].y = Event.current.mousePosition.y;
}
}
function Update()
{
if (Input.GetMouseButtonUp(0))
isDragging = false;
}
function Shuffle(refObj : GameObject[])
{
for (var i = 0; i < refObj.Length; ++i)
{
var r = Random.Range(0, refObj.Length);
var tmp = refObj[i];
refObj[i] = refObj[r];
refObj[r] = tmp;
}
}
Not sure if this will help, but try putting the...
if(Input.Get$$anonymous$$ouseButton(0))
{
isDragging = true;
subNum = i;
}
...inside function Update()
Answer by VoxelBoy · Mar 15, 2013 at 05:21 PM
Below is the fixed code.
The problem in the original code was that in OnGUI, when isDragging is true, label[subNum] is assigned the current mousePosition values but the next time OnGUI is called, those values are overwritten in line 31.
So I moved lines 29,30,31 into the for-loop in Start, as those values only need to be initialized once. I also moved line 26, which initializes the label array, into Start.
#pragma strict
var refObj : GameObject[];
private var i : int;
private var buttonWidth : int = 0;
var currentPositionX = new Array();
var currentPositionY = new Array();
var label : Rect[];
var isDragging: boolean;
var subNum : int;
function Start()
{
Shuffle(refObj);
label = new Rect [refObj.Length];
for (var i = 0; i < refObj.Length; ++i)
{
if (buttonWidth < refObj[i].name.Length * 7)
{
buttonWidth = refObj[i].name.Length * 7;
}
currentPositionX[i] = 10;
currentPositionY[i] = 70 + (i * 30);
label[i] = Rect (currentPositionX[i], currentPositionY[i], buttonWidth, 30);
}
}
function OnGUI ()
{
for (var i = 0; i < refObj.Length; ++i)
{
if(GUI.RepeatButton(Rect(label[i]), refObj[i].name))
{
if(Input.GetMouseButton(0))
{
isDragging = true;
subNum = i;
}
}
}
if (isDragging)
{
label[subNum].x = Event.current.mousePosition.x;
label[subNum].y = Event.current.mousePosition.y;
}
}
function Update()
{
if (Input.GetMouseButtonUp(0))
isDragging = false;
}
function Shuffle(refObj : GameObject[])
{
for (var i = 0; i < refObj.Length; ++i)
{
var r = Random.Range(0, refObj.Length);
var tmp = refObj[i];
refObj[i] = refObj[r];
refObj[r] = tmp;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613094232im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Score display not working 1 Answer
How to change how meany decimals are in a GUI sound slider 2 Answers
Randomize GUI Position 2 Answers
GUI.Lable slowdown problem 0 Answers
Gui label and gui button 1 Answer