- Home /
GUI DragWindow only working for a single window.
Goal: To have draggable windows that only drag when a specific region of the window has been clicked.
Issue: When multiple windows are on the scene, only the first one to be moved will be able to move until control has left the unity debugger.
Code:
using UnityEngine;
using System.Collections;
public class Window : MonoBehaviour
{
private static int windowsCreated = 0;
public Rect ClientRect;
public GUIContent Content;
public GUISkin Skin;
private int ID;
private bool canDrag = false;
private Rect bounds = new Rect(0, 0, 100, 18);
void Start()
{
ID = ++windowsCreated;
}
void OnGUI()
{
ClientRect = GUI.Window(ID, ClientRect, Draw, Content, Skin.window);
}
void Draw(int id)
{
if(bounds.Contains(Event.current.mousePosition))
{
if(Input.GetMouseButton(0))
{
canDrag = true;
}
}
if(Input.GetMouseButton(0) == false)
{
canDrag = false;
}
if(canDrag) GUI.DragWindow();
}
}
Steps to reproduce: - Create an empty game object.
Attach the script from the code snippet written above.
Repeat steps 1 and 2 until you have created the desired number of windows (3 windows is preferred for this test).
Create a default GUISkin and attach it to the scripts.
Run the debugger.
Click the upper region of a window and drag it.
Attempt to do the same for the other windows.
Click outside of the focus of the UnityDebugger.
Click inside the UnityDebugger, on a different window and drag.
Attempted Solutions:
Rewriting the script.
Writing logs to verify that the bools are being set, and that the mouse is in collidable bounds.
Black magic.
If anyone has any ideas on why this could be happening, or know of a solution, it would be greatly appreciated if you could post your thoughts.
Kind Regards,
Tyler R. Kendrick
Well composed. However, I am not able to reproduce the Issue indicated (I can move any of them, not just the first, and they move fine after co$$anonymous$$g back). Also, note that GUI.DragWindow has an overload that takes a Rect to do the mouse checking logic you've got there.