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 SpaceFiveFive · Oct 05, 2014 at 11:01 PM · javascriptgameobjectheightwidth

[UNSOLVED] Get a GameObject width and height in pixels (JS)

I'm trying to put a button directly on a 2d GameObject, and I need to get the pixel width and height of the object that is underneath to properly size the button. Is there any way to get that?

Thanks!

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 Eric5h5 · Oct 05, 2014 at 11:53 PM

Size in Unity is measured with units, not pixels. You can use Mesh.bounds to get the bounds of an object's mesh.

Comment
Add comment · Show 2 · 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 SpaceFiveFive · Oct 05, 2014 at 11:57 PM 0
Share

Sorry, trying to modify the button. It's incorrectly sized.

avatar image SpaceFiveFive · Oct 06, 2014 at 12:03 AM 0
Share

Also, how would I convert it to an x and y coords on the screen for a button?

avatar image
1

Answer by robertbu · Oct 06, 2014 at 01:24 AM

Perspective or Orthographic camera? Can we assume anything about the nature or rotation of the game objects?

In general you can get something close by converting a bounds into screen points and then finding the maximum and minimum X and Y values.

  • Use either the corners of the renderer.bounds of the object or use the mesh.bounds and then convert the eight corners from local to world coordinates.

  • For each of the eight corners, do a WorldToScreenPoint().

  • Cycle through the resulting positions and find the minimum/maximum x and y

  • Subtract min from max to get the width and height

Here is an answer that with just a bit of modification will give you those values. 'width' and 'height' on lines 43 and 44 are what you are looking for:

http://answers.unity3d.com/questions/691066/how-to-calculate-screen-area-coverage-of-a-3d-obje.html

Note for particular setups, you may be able to do the calculations with less work.

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 robertbu · Oct 06, 2014 at 02:11 AM 0
Share

There were a couple of things I did not like about the code I pointed you at, so here is the function based on the link:

 #pragma strict
  
 function Update() {
     var size = CalcScreenSize(gameObject);
     Debug.Log("Object screen size = " + size);
 }
  
 static function CalcScreenSize(go : GameObject) : Vector2 {
  
     var $$anonymous$$X = $$anonymous$$athf.Infinity;
     var $$anonymous$$Y = $$anonymous$$athf.Infinity;
     var maxX = -$$anonymous$$athf.Infinity;
     var maxY = -$$anonymous$$athf.Infinity;
  
     var mf = go.GetComponent.<$$anonymous$$eshFilter>();
     if (mf == null) return Vector3.zero;
     var t = go.transform;
     var bounds = mf.mesh.bounds; 
     var v3Center = bounds.center;
     var v3Extents = bounds.extents;
  
     var corners : Vector3[] = new Vector3[8];
  
     corners[0]  = new Vector3(v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z);  // Front top left corner
     corners[1]  = new Vector3(v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z);  // Front top right corner
     corners[2]  = new Vector3(v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z);  // Front bottom left corner
     corners[3]  = new Vector3(v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z);  // Front bottom right corner
     corners[4]  = new Vector3(v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z);  // Back top left corner
     corners[5]  = new Vector3(v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z);  // Back top right corner
     corners[6]  = new Vector3(v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z);  // Back bottom left corner
     corners[7]  = new Vector3(v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z);  // Back bottom right corner
  
     for (var i = 0; i < corners.Length; i++) {
         var corner = t.TransformPoint(corners[i]);
         corner = Camera.main.WorldToScreenPoint(corner);
         if (corner.x > maxX) maxX = corner.x;
         if (corner.x < $$anonymous$$X) $$anonymous$$X = corner.x;
         if (corner.y > maxY) maxY = corner.y;
         if (corner.y < $$anonymous$$Y) $$anonymous$$Y = corner.y;
     }
  
     return Vector2(maxX - $$anonymous$$X, maxY - $$anonymous$$Y);
 }
avatar image
-1
Wiki

Answer by jmparavicini · Oct 06, 2014 at 12:21 AM

Try this Maybe it wil work and maybe i forgot something

 #pragma strict
 
 var gameObject1 : Transform;
 var gameObject2 : Transform;
 
 var gO1X : float; 
 var gO1Y : float;
 var gO1Z : float;
 
 function Update()
 {
     gO1X = gameObject1.transform.position.x;
     gO1Y = gameObject1.transform.position.y;
     gO1Z = gameObject1.transform.position.z;
 
     if(Input.GetKeyDown(KeyCode.F))
     {
         gameObject2.transform.position.x = gO1X;
         gameObject2.transform.position.y = gO1Y;
         gameObject2.transform.position.z = gO1Z;
     }
 }

in the gameObject1 put the transform of your first gameobject and on the gameObject2 the transform of your second gameObject

cheers skullbeats1

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 Eric5h5 · Oct 06, 2014 at 12:31 AM 0
Share

You're just setting the position of one object to the position of another, so it doesn't answer the question, but if you were going to do that anyway, 90% of that code is unnecessary and should be removed. There's no reason to use 3 separate floats when Vector3 exists.

 var gameObject1 : Transform;
 var gameObject2 : Transform;
 
 function Update () {
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.F)) {
         gameObject2.position = gameObject1.position;
     }
 }
avatar image SpaceFiveFive · Oct 06, 2014 at 12:47 AM 0
Share

I'm trying to put an object over a button, my apologies. Bad wording. I need to find the width and height of a gameobject so I can put the accurate button size

avatar image jmparavicini · Oct 06, 2014 at 01:31 AM 0
Share

you are right Eric5h5 thank you

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

JS file deleting multiple game objects 0 Answers

Get game component type? 1 Answer

JavaScript 3 Arrays questions 1 Answer

Track debug - Object dissapears after collision 0 Answers

Add force to GameObject 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