Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Seizure · Jun 27, 2013 at 08:30 PM · windowsdraggingrotatearoundpivotdragwindow

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();
     }    
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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);
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Seizure · Jun 28, 2013 at 01:16 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges