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
3
Question by DaveA · Apr 02, 2011 at 01:05 AM · guiwindowboundsoffscreendragwindow

How do you prevent GUI.DragWindow from letting the window go offscreen?

GUI.DragWindow will happily let me drag my entire window offscreen (and leave it there). I'm guessing I could check the current window rect and make sure it's not going offscreen, but wouldn't that mean checking it on the next frame? Seems like it would get jittery. I'm assuming I can check for mouse in bounds of screen and not call drag, might help a little. What's the best way to do this?

Comment
Add comment · Show 2
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 Joshua · Apr 02, 2011 at 01:12 AM 0
Share

Stopping your mouse from going too near the edge sounds like a clever workaround. There might be a real solution though. :)

avatar image DaveA · Apr 02, 2011 at 01:15 AM 0
Share

Yeah, I'm actually getting close to it already. I'll leave this open because it might help someone else.

2 Replies

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

Answer by Bunny83 · Apr 02, 2011 at 01:25 AM

GUI.Window or GUILayout.Window returns the changed Rect. If you wouldn't assign it back to your windowrect variable the window wouldn't even move 1 pixel. The movement happens next frame when you call Window again with the new coordinates. So limiting the returned Rect to your desired area should be enough.

// C#

Rect windowRect = new Rect(10,10,100,100);

void OnGUI() { windowRect = GUI.Window(0,windowRect,MyWindowFunc,"Title"); windowRect.x = Mathf.Clamp(windowRect.x,0,Screen.width-windowRect.width); windowRect.y = Mathf.Clamp(windowRect.y,0,Screen.height-windowRect.height); }


edit

I think that would be a nice job for a seperate function ;) ClampToScreen()

Rect ClampToScreen(Rect r) { r.x = Mathf.Clamp(r.x,0,Screen.width-r.width); r.y = Mathf.Clamp(r.y,0,Screen.height-r.height); return r; }

void OnGUI() { windowRect = ClampToScreen(GUI.Window(0,windowRect,MyWindowFunc,"Title")); }

Comment
Add comment · Show 3 · 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 DaveA · Apr 02, 2011 at 01:29 AM 0
Share

That's where I was heading, but forgot about Clamp which cleans things up. I haven't tried your code yet, but looks good.

avatar image paulo_kether · Apr 02, 2012 at 11:51 PM 0
Share

i was fighting with a similar problem for 3 hours!! thanks a lot!! very useful!!

avatar image Shiggsy · Feb 14, 2014 at 08:54 AM 0
Share

This works a treat, I had been trying to come up with a way of doing this myself but hit a dead end. I would throw you a +1 but I'm not allowed :(

avatar image
0

Answer by PaoloAbela · Jun 04, 2020 at 03:37 PM

To build on top of @Bunny83's answer, this works perfectly in Unity 2019.3 and gives you the exact size of the window:

 static Rect ClampRectToSceneView(Rect rect)
 {
             const int SceneViewTopBarHeight = 15;
             const int DistanceFromBorders = 10; //you can remove this if you want
             Rect sceneViewArea = SceneView.lastActiveSceneView.position;
             rect.x = Mathf.Clamp(rect.x, DistanceFromBorders, sceneViewArea.width - rect.width - DistanceFromBorders);
             rect.y = Mathf.Clamp(rect.y, DistanceFromBorders + SceneViewTopBarHeight, sceneViewArea.height - rect.height - DistanceFromBorders);
             return rect;
 }
Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

GUI DragWindow only working for a single window. 0 Answers

Why does my GUI.Window dragging behaviour act erratically on a rotated window? 1 Answer

Dragging and Locking Buttons. 1 Answer

how to get Gui in OnGUI function to resize with the window? 1 Answer

Move one GUI window when another is moved? 0 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