- Home /
Using GUIUtility.RotateAroundPivot With GUI.Window and GUI.DragWindow()
Hello Everyone,
I have been searching for this problem and have not come across any solutions as of yet, but as described in the title, is there a way to allow a window to rotate as well as be draggable. I have been able to do either very successfully but as soon as you get the window around 30 degrees off its normal axis it starts to go crazy. Here is what I have for code so far: (you should be able to add it into an empty gamescene and just replace the "window" gamestyle to recreate the issue)
using UnityEngine;
using System.Collections;
public class rotatingWindow : MonoBehaviour {
public GUISkin gameStyle;
public Vector2 windowSize;
public Rect windowRect;
public float rotAngle = 0;
// Use this for initialization
void Start () {
windowSize = new Vector2 (1024, 1024);
windowRect = new Rect (0, 0, windowSize.x, windowSize.y);
windowRect.center = new Vector2 (Screen.width/2, Screen.height/2);
}
void OnGUI ()
{
Vector2 pivotPoint = windowRect.center;
if (GUI.RepeatButton(new Rect(Screen.width / 2 - 25, Screen.height / 2 - 25, 50, 50), "Rotate"))
rotAngle += 10;
rotAngle = Mathf.Clamp(rotAngle,0, 360);
if (rotAngle == 360)
{
rotAngle = 0;
}
GUIUtility.RotateAroundPivot(rotAngle, pivotPoint);
GUIUtility.ScreenToGUIPoint(Input.mousePosition);
windowRect = GUI.Window(1, windowRect,windowFunc, "", gameStyle.GetStyle("window"));
GUI.matrix = Matrix4x4.identity;
}
public void offanFunc (int id)
{
GUI.BringWindowToFront(id);
GUI.DragWindow();
}
}
Answer by Jamora · Jun 27, 2013 at 10:23 PM
You need to create your own window-moving function. I think there is something wrong with the built-in one regarding rotated GUIs...
I made you a crude mover function to get you started...
using UnityEngine;
using System.Collections;
public class rotatingWindow : MonoBehaviour {
public GUISkin gameStyle;
public Vector2 windowSize;
public Vector2 pivotPoint;
Vector2 mouseClick;
public Rect windowRect;
public float rotAngle = 0;
public bool moving = false;
Matrix4x4 oldMatrix;
// Use this for initialization
void Start() {
windowSize = new Vector2(100, 100);
windowRect = new Rect(0, 0, windowSize.x, windowSize.y);
oldMatrix = GUI.matrix;
}
void OnGUI() {
if(GUI.RepeatButton(new Rect(Screen.width / 2 - 25, Screen.height / 2 - 25, 50, 50), "Rotate")) {
rotAngle += 10;
rotAngle = Mathf.Clamp(rotAngle, 0, 360);
if(rotAngle == 360) {
rotAngle = 0;
}
}
GUIUtility.RotateAroundPivot(rotAngle, pivotPoint);
MoveWindow();
pivotPoint = new Vector2(windowRect.x, windowRect.y);
windowRect = GUI.Window(1, windowRect, offanFunc, "", "window");
GUI.matrix = oldMatrix;
}
private void MoveWindow(){
//Is mouse clicked inside the rect?
if(Input.GetMouseButtonDown(0) && windowRect.Contains(GUIUtility.ScreenToGUIPoint(new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y)))) {
mouseClick = new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y);
moving = true;
}
if(moving) {
//calculate difference of previous mousepos to current
Vector2 clickDif = new Vector2((Input.mousePosition.x - mouseClick.x), (Screen.height - Input.mousePosition.y - mouseClick.y));
//move window
windowRect.center = new Vector2(windowRect.center.x + clickDif.x, windowRect.center.y + clickDif.y);
if(Input.GetMouseButtonUp(0))
moving = false;
mouseClick = new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y);
}
}
public void offanFunc(int id) {
GUI.BringWindowToFront(id);
}
}
This works perfectly!!! Thank you, all I needed next was a way to offset the pivot point but was able to work that out on my own.
pivotPoint = new Vector2((windowRect.x + 227), (windowRect.y + 830));
Your answer
![](https://koobas.hobune.stream/wayback/20220613105014im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why does my GUI.Window dragging behaviour act erratically on a rotated window? 1 Answer
Keep Draggable UI window within Canvas? 1 Answer
FunkyGlowingThingsEffect fix for inverted y on Windows 1 Answer
Windows Standalone Crashes in -batchmode , how do I read the strack from the log? 1 Answer
MonoDevelop not Interfacing with Unity 2 Answers