2D Toolkit Forum
2D Toolkit => Support => Topic started by: GorillaOne 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:
//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);
(http://i60.tinypic.com/10xd9n9.jpg)
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.
-
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.
-
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?
-
For what it's worth, it would be a nice feature to be able to change the pivot of the layout.
-
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.
-
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