Hello Guest

Author Topic: Layout and SetBounds  (Read 6070 times)

GorillaOne

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 5
    • View Profile
Layout and SetBounds
« on: March 08, 2014, 07:28:12 am »
I have a tk2dlayout script attached to a GameObject which gets centered in the screen. I want to have the layout bounds resize to fit the screen, whatever the resolution is (iPad vs iPhone). However, whenever I set bounds on the layout object it uses the GameObject as the fulcrum in the upper left corner.

How can I go about having the layout center on the GameObject?

Code:
Code: [Select]
       //Resize layout
        tk2dUILayout layout = GetComponent<tk2dUILayout>();

        //Assumes a parent in the screen center.
        Vector3 min = new Vector3(tk2dCamera.Instance.ScreenExtents.xMin, tk2dCamera.Instance.ScreenExtents.yMin, 0);
        Vector3 max = new Vector3(tk2dCamera.Instance.ScreenExtents.xMax, tk2dCamera.Instance.ScreenExtents.yMax, 0);
        layout.SetBounds(min, max);



Attached is a picture showing an outline of the camera, and the pink line being the layout bounds. You can see that the upper left corner of the layout bounds is in the center of the camera, where the GameObject is located.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Layout and SetBounds
« Reply #1 on: March 08, 2014, 01:40:45 pm »
I'm not sure how you have your game object component hierarchy set up, but the tk2duilayout pivot is ALWAYS at the top left. UI Demo #2 - UILayout demo does this exact thing - its probably worth comparing to that to work out why it isn't working for you.

GorillaOne

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Layout and SetBounds
« Reply #2 on: March 08, 2014, 06:07:10 pm »
Ok, so the pivot is always the top left. If the root GameObject is centered in the screen, that means I'll need to create a child GameObject to contain the layout which is positioned at the top-left of the screen in order to get stuff to work correctly. That sound about right?

GorillaOne

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Layout and SetBounds
« Reply #3 on: March 08, 2014, 06:07:41 pm »
For what it's worth, it would be a nice feature to be able to change the pivot of the layout.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Layout and SetBounds
« Reply #4 on: March 08, 2014, 08:55:00 pm »
There is no plan to add an option to change the pivot, as it would add significant complexity to the code due to how things can potentially be organised in Unity. SetBounds should just work, but if you're using an anchor to centre the game object or doing that by some other means, it will interfere with this.

yuhe00

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: Layout and SetBounds
« Reply #5 on: March 10, 2014, 03:52:01 am »
Could you add entering the bounds manually in the inspector? I tried to implement this myself with Reshape(dx, dy, true) but would get weird bugs with the children. I tried looking at the editor code for tk2dUILayout, but couldn't really get my head around it...

Edit: Nevermind, I think it's working now, just had to do CollectDeepHierarchy/SetDirty :P
« Last Edit: March 10, 2014, 04:01:42 am by yuhe00 »