• 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
1
Question by Yasantha · Jul 01, 2012 at 01:50 PM · bounds

Screen Space Rectangle Of Given Object.

Hi want to Draw a Rectangle of (GUI.BOX) in screen By given Object (Ex : BOX). Simply What want is Take Screen Shot of Given Object. I already know how to take screen shot of given object. But what i need to Know is How to Draw Screen bounding Box of Given Object. I came up with sample solution and that is work fine with Orthographic camera but when i change the camera to Perspective its not given correct result for some reason.

public Rect ScrRect;

Update()
{

     Bounds b = GObj.renderer.bounds; // reference object ex Simple
         points[0] = new Vector3( b.min.x, b.min.y, b.min.z );
         points[1] = new Vector3( b.max.x, b.min.y, b.min.z );
         points[2] = new Vector3( b.max.x, b.max.y, b.min.z );
         points[3] = new Vector3( b.min.x, b.max.y, b.min.z );
         points[4] = new Vector3( b.min.x, b.min.y, b.max.z );
         points[5] = new Vector3( b.max.x, b.min.y, b.max.z );
         points[6] = new Vector3( b.max.x, b.max.y, b.max.z );
         points[7] = new Vector3( b.min.x, b.max.y, b.max.z );
         
         Bounds screenBounds = new Bounds();
         for( int i = 0; i < 8; i++ )
         {
             screenPos[i] = Camera.main.WorldToScreenPoint( points[i] );
             
             if( i == 0 )
                 screenBounds = new Bounds( screenPos[0], Vector3.zero);            
             
             screenBounds.Encapsulate( screenPos[i] );
         }
         
         Debug.Log(screenBounds.ToString());
         
         ScrRect.xMin = screenBounds.min.x;
         ScrRect.yMin = screenBounds.min.y;
         ScrRect.xMax = screenBounds.max.x;
         ScrRect.yMax = screenBounds.max.y;
     }
 
 
     OnGUI()
     {
        GUI.Box(ScrRect,"Area");
     }

I think there is some thing wrong with Screen space Conversation. When I convert to Viewpot position seems ok.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by chaosrau · Dec 02, 2015 at 10:02 AM

@Yasantha

I'm not sure if you ended up solving this problem but I managed to do something similar and had the issue of the y co-ordinate being reversed for OnGUI. So I changed the code that you had:

      ScrRect.yMin = screenBounds.min.y;
      ScrRect.yMax = screenBounds.max.y;

to

      ScrRect.yMin = Screen.height - screenBounds.max.y;
      ScrRect.yMax = Screen.height - screenBounds.min.y;

which allowed for the GUI.Box code to render the screen bounds in the correct location.

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
avatar image
0

Answer by hathol · Jul 01, 2012 at 02:41 PM

I guess it's drawn wrong on the y axis? For some reason unity usually has its zero position in the bottom left corner, but for OnGUI functions they decided to go with top left. Try

 OnGUI()
 {
    ScrRect.y = Screen.height - (ScrRect.y+ScrRect.height);
    GUI.Box(ScrRect,"Area");
 }
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
avatar image
0

Answer by Yasantha · Jul 01, 2012 at 02:55 PM

@Alex Zeitler - thanks for Reply but that wasnt the Problem. I tried what you have suggest :(

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

How do I keep an Ortho camera in a specific range when the ortho changes? 3 Answers

Checking if ground is 'level' 1 Answer

How to get correct position after RecalculateBounds is called? 1 Answer

Issue with bounds.SetMinMax assigning incorrectly. 1 Answer

How to stop view from scrolling when edge of view collides with a game object? 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