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();
	}	
}

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);
	}
}