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*;*

> *
> 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
.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! :]

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