Search Unity

How to scale gameobject to pixel width and height

Discussion in 'Scripting' started by lofwyre, Apr 1, 2012.

  1. lofwyre

    lofwyre

    Joined:
    Apr 19, 2007
    Posts:
    174
    Hi

    I have an ortho camera and I want to position a game object at a fixed screen position top left of screen. I can use;

    Code (csharp):
    1. myGuiCamera.ScreenToWorldPoint(25, Screen.height - 25, 10)
    And it positions it correctly, but... if the gameobject by default is too big, I want to scale it so that it will fit a certain pixel space.

    Any thoughts on how this might be done?

    Cheers
     
  2. lofwyre

    lofwyre

    Joined:
    Apr 19, 2007
    Posts:
    174
    I guess the camera type is not really important, just the ability to get the x and y size in pixels, perhaps from the renderer component of a gameobject.
     
  3. abbc

    abbc

    Joined:
    Mar 12, 2011
    Posts:
    52
    Display a GUI texture on screen as a guide, adjust the size of object by trial and error.
     
  4. lofwyre

    lofwyre

    Joined:
    Apr 19, 2007
    Posts:
    174
    Thanks for the idea abbc. In the end I needed something a little more dynamic (I probably should have explained better).

    I have most of what I want now with a solution like this; (c#)

    Code (csharp):
    1.  
    2. Vector3 posStart = myCamera.WorldToScreenPoint(new Vector3(mesh.bounds.min.x, mesh.bounds.min.y, mesh.bounds.min.z));
    3. Vector3 posEnd = myCamera.WorldToScreenPoint(new Vector3(mesh.bounds.max.x, mesh.bounds.max.y, mesh.bounds.min.z));
    4.  
    5. int widthX = (int)(posEnd.x - posStart.x);
    6. int widthY = (int)(posEnd.y - posStart.y);
    7.  
    This gives me the width and height of the mesh in pixels.

    I'll use a similar method to convert the handle to screen coords so I know what offset I need when positioning it.

    Cheers
     
    unity_8R3gaxreN6dMTA likes this.
  5. abbc

    abbc

    Joined:
    Mar 12, 2011
    Posts:
    52
  6. lofwyre

    lofwyre

    Joined:
    Apr 19, 2007
    Posts:
    174
    Thanks abbc, that will help i think. I'll need to work out how to convert the pixel size I want to the local scale.

    Cheers
     
  7. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    i would cast a ray with a certain length through both of the pixels. then you get the endpoints of the rays and can calculate the distance between them. then you can apply the formula linked above to calculate the required scale. then you could place the object in the mid between the two points.
    what you should consider:
    when the rays hit something you have a problem. maybe you should set the flags to exclude any layers.
    when the object is not symmetrical you cant place it in the middle.
    when the object should rotate you may want to use the axis aligned bounding box to determine the width.
    the object still yould be occluded by stuff.

    maybe consider using a second camera only rendering this object on top. this would help with the occlusion thing.
     
    Last edited: Apr 2, 2012
  8. lofwyre

    lofwyre

    Joined:
    Apr 19, 2007
    Posts:
    174
    There are some good ideas in there, thanks exiguous, I'll do some looking today and see how I go.

    Cheers