- Home /
Dragging and Locking Buttons.
Hey, good evening everyone on UnityAnswers! :]
I'm currently stuck on a problem that I've been trying to fix since morning, and I haven't found a solution since so I thought I'd pop by and post it up here. Let me post up the codes first:
class ActionButt
{
var toggle: String; //Name of custom style in GUI Skin
var isSelected: boolean; //Is the Toggle button On or Off
var label: String;
var position: Rect; //Where to draw this button on the screen
}
//Variables
var bt1: boolean = false;
var bt2: boolean = false;
var bt3: boolean = false;
//var bt4: boolean = false;
var bt5: boolean = false;
var bt6: boolean = false;
var bt7: boolean = false;
var btGUI: boolean = false; //Boolean to call on the different GUI
var btnTexture: Texture;
var buttonList: ActionButt [];
var toolSkin: GUISkin;
var windowRect : Rect = Rect (20, 20, 120, 50);
var dragWindow : boolean;
var toggleDrag : String;
//Functions
function Start () {
toggleDrag = "Enable";
dragWindow = false;
}
function setMeOnly(): boolean {
bt1 = bt2 = bt3 = bt5 = bt6 = bt7 = false;
return true;
}
function OnGUI() {
//First GUI
if (!btGUI) {
GUI.skin = toolSkin;
//Top Left Button
bt1 = GUI.Toggle (Rect(50,50,100,100), bt1, "");
if (bt1) bt1 = setMeOnly();
//Bottom Right Button
bt2 = GUI.Toggle (Rect(50,275,100,100), bt2, "");
if (bt2) bt2 = setMeOnly();
//Top Button
bt3 = GUI.Toggle (Rect(50,500,100,100), bt3, "");
if (bt3) bt3 = setMeOnly();
//Middle Button
//bt4 = GUI.Toggle (Rect(575,275,100,100), bt4, "");
//if (bt4) bt4 = setMeOnly();
//Top Right Button
bt5 = GUI.Toggle (Rect(1125,50,100,100), bt5, "");
if (bt5) bt5 = setMeOnly();
//Bottom Button
bt6 = GUI.Toggle (Rect(1125,275,100,100), bt6, "");
if (bt6) bt6 = setMeOnly();
//Bottom Left Button
bt7 = GUI.Toggle (Rect(1125,500,100,100), bt7, "");
if (bt7) bt7 = setMeOnly();
//Button to Call
//if (!btnTexture) {
//Debug.LogError("Please assign a texture on the inspector.");
//return;
//}
}
if(GUI.Button (Rect(575,275,100,100), btnTexture, "Button")) {
if (btGUI)
{
print("Call the second GUI.");
btGUI = false;
}
else if (!btGUI)
{
print("Remain in the first GUI.");
btGUI = true;
}
}
//Second GUI
if (btGUI) {
GUI.skin = toolSkin;
for (var i = 0; i < buttonList.length; i++) {
var bt = buttonList[i];
bt.isSelected = GUI.Toggle(bt.position, bt.isSelected, bt.label);
if (bt.isSelected)
{
print("This butt is turned on." + bt.label);
resetOtherButtons(i);
}
windowRect = GUI.Window (0, windowRect, DoMyWindow, btnTexture, toggleDrag);
if (GUI.Button(Rect(80,380,100,30),toggleDrag)) {
if (dragWindow) {
dragWindow = false;
toggleDrag = "Enable";
}
else if (!dragWindow) {
dragWindow = true;
toggleDrag = "Disable";
}
}
}
}
}
function resetOtherButtons (ignoreThisNumber)
{
for (var i = 0; i < buttonList.length; i++) {
if (i !=ignoreThisNumber) {
buttonList[i].isSelected = false;
}
}
}
// Make the contents of the window
function DoMyWindow (windowID : int) {
// Make a very long rect that is 20 pixels tall.
// This will make the window be resizable by the top
// title bar - no matter how wide it gets.
if(dragWindow) {
GUI.DragWindow (Rect (0,0, 10000, 10002)); }
else {
}
}
Firstly, I have two GUI(s) which I could switch in between them. The second GUI contain 7 buttons that are formed in a hexagon manner, which I have done so from the Inspector Menu.
Next, I have a GUIWindow inside the second GUI which I referenced from the Documentations, and can only been seen inside the second GUI, and not the first GUI. There is an Enable/Disable to lock the GUIWindow from moving.
What I'm trying to achieve is to implement this feature upon my hexagon-patterned button so that when the enable/disable button is clicked, it will be able to either move or be locked respectively. Any ideas how I can achieve that?
Thank you for taking your time off to read this! :]
Answer by SilverTabby · Jul 19, 2011 at 04:23 PM
Here is how I would implement the ability to drag GUI.Buttons around the screen:
Don't hard code the rects into the GUI function. Store them (in an array?) at the top of the file.
In Update/LateUpdate, check if the user pressed the mouse down, and then go through the rects that you want to drag and see if any of the rects.Contains(Input.mousePosition)
Then track the change in the mouse position and feed that change into the first 2 values of the rect you are dragging
Stop updating the rect's position when the user releases the mouse button.
Now just feed the rects into your buttons and they should move around the screen as you desire.
Hope this helped
For the first pointer, do I create two arrays for the two GUI(s) since I have to switch in between them? What I've done thus far is creating two arrays for the two GUI(s), but still leaving the GUI.Button (the button that switches between the two GUI) as its own. Is that correct?
For the second pointer, does Update/LateUpdate refer to the function OnGUI, or do I have to set up a new function?
Thanks for the answer, btw! :]
-It looks cleaner if you have 2 arrays for 2 GUIS
-You CAN put that in the OnGUI, but it's not optimal. OnGUI is called at least twice per frame while Update/LateUpdate is called only once per frame. It comes down to a matter of preference and/or optimization.
Hmm. The original second GUI is considered an array, right? So all I have to do is re-implement the exact same codes right?
"The granule of reuse is the granule of release."
Just make sure you're not changing the first gui twice when you re-implement it ;)
Sorry for the awfully late reply! What do you mean changing the first GUI twice? It's only been a month since I started using Unity, and I'm really slow at grasping things.
And I realize an array is useful, but what if I had buttons with different symbols in them. Six buttons with six different symbols to be exact. Won't the first GUI be more useful ins$$anonymous$$d?