Search Unity

Converting a Rect.height to a vector3 position?

Discussion in 'Editor & General Support' started by tpainton, Sep 30, 2014.

  1. tpainton

    tpainton

    Joined:
    Jun 4, 2014
    Posts:
    16
    I'm stuck... I have a rect that represents the GetScreenRect() of a GUIText. I am trying to figure out a way to lay out GUITexts with dynamic texts down the screen, much like text messages.

    If I know the rect of the GUIText, how do I figure out how far down the screen the next instantiation should be assuming I want 20 pixels between GUITexts?

    I know I probably use ScreenToWorldPoint() but what has me confused is the parameter is a Vector3, and I'm starting with a Rect. If the Rect is 200 pixels tall, then I know the next GameObject which holds the GUIText needs to be created 220 pixels down the screen. I can't figure out how to translate this information to the Vector3 I'll use in the next instantiation.

    So to illustrate.

    If my first GUIText which is attached to a GameObject is,
    Code (CSharp):
    1. Rect template = new Rect (0f, 0f, 120f, 120f);
    2.  
    3. // so next object should be instantiated 140 pixels below...
    All help appreciated.
     
  2. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    Um, if the position for the GUIText is set to (0.5,0.5,0.0) then the tex is rendered in the centre of the screen. You can then use the pixel offset to move the text around the screen. A negative y pixelOffset moves the text down the screen. So, if you add -140 each time, you can lay the messages out. Alternatively, I think the GUIText position is meant to use x and y in the 0..1 range. So if you know the height of the screen, you can compute 140/height which tells you how to move the y position. Put another way, think of the transform position of the GUIText as being in some kind of 2d space and not 3d.
     
  3. tpainton

    tpainton

    Joined:
    Jun 4, 2014
    Posts:
    16
    So I have a GameObject at 0,0,0. This has a GUIText Component that has pixeloffset set to exactly where I need it. I can figure out how tall this GUItext is in pixels, X. When a new message is created, it will be a new GameObject with a GUIText component. I need to shift the new GameObject down by X pixels. I guess the problem is, how do you move an object down X pixels?
     
  4. tpainton

    tpainton

    Joined:
    Jun 4, 2014
    Posts:
    16
    Okay I got it working.. I just shifted the pixel Offset instead of worrying about moving the parent GameObject. Now everything should scroll fine by simply attaching a script to the prefab game object that moves it up the screen with finger drag. Bravo.