• 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
4
Question by Freaking-Pingo · Nov 22, 2012 at 10:17 PM · gameobjectscreenspacewidth

GameObject width in screen space

Hi there Uniteers o/ I am in need of some help. I am trying to get the width (and possibly height) of an object, when it has been rendered (Screen space).

My initial thought would be to access all vertices in the model, and use WorldToScreenPoint(); on each vertice position, followed by iterating through them to see w$$anonymous$$ch of the vertices have the $$anonymous$$ghest/lowest x values. To me, t$$anonymous$$s sounds very computational heavy, and I am sure there is a more efficient way than that.

Anyone have an idea?

Comment
Add comment · Show 3
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 whydoidoit · Nov 22, 2012 at 10:19 PM 1
Share

Well you could call GetBounds on the renderer and then you'd only have 8 points to consider... Less accurate though. Especially as the result is an AABB.

avatar image Freaking-Pingo · Nov 22, 2012 at 10:26 PM 0
Share

Well, that clearly is an approach that could work, and I actually think it yields the necessary information I need. I'll sit tight and see if others have some great ideas.

avatar image Freaking-Pingo · Nov 23, 2012 at 01:34 PM 0
Share

I am in the middle of trying to implement this, though I am having some issues. I'll post a solution if I find one.

1 Reply

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

Answer by Freaking-Pingo · Nov 23, 2012 at 08:02 PM

Okay I'll go ahead and answer my own post: I made an attempt as "Whydoidoit" suggested

  1. Find the desired GameObject you wish to measure

  2. Identify its boundries

  3. Identify the eight coordinates w$$anonymous$$ch forms the boundries

  4. Convert these eight coordinates into Screen space coordinates

  5. Locate the max/min values

  6. Find the distance between max/min and you will get Width/Height

The output of it works fine when the object is properly fitted inside the viewport, but as soon the object begins to wander out of screen, the width and height goes crazy. The exact reason for that I don't know, I presume it is because of the shape of the camera.

I posted the example code w$$anonymous$$ch I tried out, hope it might be useful for someone else.

     private void FindCenterOnScreen(){
         _TargetPosition = camera.WorldToScreenPoint(_Target.transform.position);
         _TargetPosition.y = Screen.height-_TargetPosition.y;
     }
     
     // Don't be fooled by the method name, it also calculates height.
     private float FindWidthInScreen(GameObject _TargetGameObject){
         Vector2 _ObjectScreenCord, _Xmin = new Vector2(Screen.width,0), _Xmax = Vector2.zero, _Ymin = new Vector2(Screen.height,0), _Ymax = Vector2.zero;
         float _Height, _Width;    
         
 
         for(int i = 0; i != 8; i++){
             //FindBoundCord() locates the eight coordinates that forms the boundries, followed by converting the coordinates to screen space.
             // The entire script starts in FindBoundCord
             _ObjectScreenCord = camera.WorldToScreenPoint(FindBoundCord(i, _TargetGameObject));
             
             /* After gathering the coordinates and converting them to screen space
              we try to locate w$$anonymous$$ch of these have the $$anonymous$$ghest/minimum x and y values.
             The difference between $$anonymous$$ghest/minimum x and y must correspond to
              width and height.*/
             
                 if(_ObjectScreenCord.x > _Xmax.x){
                     _Xmax.x = _ObjectScreenCord.x;
                 }
                  else if(_ObjectScreenCord.x < _Xmin.x){
                     _Xmin.x = _ObjectScreenCord.x;
                 }    
                 if(_ObjectScreenCord.y > _Ymax.x){
                     _Ymax.x = _ObjectScreenCord.y;
                 }
                 else if(_ObjectScreenCord.y < _Ymin.x){
                     _Ymin.x = _ObjectScreenCord.y;
                 }            
         }
         
         
         
         //Too measure the distance between them, I use the Vector2 method Distance.
         _Height = Vector2.Distance(_Ymax, _Ymin);
         if (_Height > Screen.height || _Height < 0){
             _Height = 0;
         }
         
         _Width = Vector2.Distance(_Xmax, _Xmin);
         if (_Width > Screen.width || _Width < 0){
             _Width = 0;
         }
         
         // Here we simply make a check on w$$anonymous$$ch of the height or the 
         // width is the biggest. It was a necessary part for my project.
         if (_Height > _Width ){
             return _Height;
         } else {
             return _Width;
         }
         /*
         The method appears to work, though there are som serious issues when 
         Screen coordinates gets into negative values.
         When screencoordinates get negative the width/height 
         explodes, and I t$$anonymous$$nk it is because of the 
         Trapez formed viewport. */
 
     
     }
 
     private Vector3 FindBoundCord (int i, GameObject _GameObject){
          /*T$$anonymous$$s is basically where the code starts. It starts out by creating a 
           * bounding box around the target GameObject. 
          It calculates the 8 coordinates forming the bounding box, and 
          returns them all to the for loop.
          Because there are no real method w$$anonymous$$ch returns the coordinates 
          from the bounding box I had to create a switch/case w$$anonymous$$ch utillized 
          Bounds.center and Bounds.extents.*/
 
         Bounds _TargetBounds = _GameObject.renderer.bounds;
         Vector3 _TargetCenter = _TargetBounds.center;
         Vector3 _TargetExtents = _TargetBounds.extents;
 
         
         switch(i){
         case 0:
             return _TargetCenter + new Vector3(_TargetExtents.x, _TargetExtents.y, _TargetExtents.z); 
         case 1:
             return _TargetCenter + new Vector3(_TargetExtents.x, _TargetExtents.y, _TargetExtents.z*-1);
         case 2:
             return _TargetCenter + new Vector3(_TargetExtents.x, _TargetExtents.y*-1, _TargetExtents.z);
         case 3:
             return _TargetCenter + new Vector3(_TargetExtents.x, _TargetExtents.y*-1, _TargetExtents.z*-1);
         case 4:
             return _TargetCenter + new Vector3(_TargetExtents.x*-1, _TargetExtents.y, _TargetExtents.z);
         case 5:
             return _TargetCenter + new Vector3(_TargetExtents.x*-1, _TargetExtents.y, _TargetExtents.z*-1);
         case 6:
             return _TargetCenter + new Vector3(_TargetExtents.x*-1, _TargetExtents.y*-1, _TargetExtents.z);
         case 7:
             return _TargetCenter + new Vector3(_TargetExtents.x*-1, _TargetExtents.y*-1, _TargetExtents.z*-1);
         default:
             return Vector3.zero;
         }
 
     }

Sorry for the horrible formatting, not used to post such long code snippits :)

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 whydoidoit · Nov 23, 2012 at 08:18 PM 0
Share

You probably want to "dot" the (coordinate you find - the camera's position) with camera.main.transform.forward and check that it is on the same side of the camera.

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

11 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

Related Questions

Is it possible to get the width (in unity units) of a GameObject including all of its children? 1 Answer

screen space reflection 0 Answers

full screen wide GUI.Box 3 Answers

screen size problem 1 Answer

So much problem with screen.width camera.pixelwidth and responsive design 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