Learn from the Unity Plugin's Ngui (2), create a UI root, create a texture as a background image under UI root, set the picture, resize it under Wiget, and then create a panel under UI root.
In the Hierarchy window, select Panel, then select Menu component->ngui->interaction->table, view the inspector window, and add uitable to the panel's properties.
Columns indicates that each line has several elements
Direction indicates the direction of the element after each column is added
Sorting how elements are sorted
Hide Inactive Default check, all invisible child nodes are ignored when arranging
Padding the x, y direction of the interval, where the value is based on the Ngui
To create a level prefab, under the Hierarchy window, add multiple level, and then select Panel, in Inspector, right-click Uitable and execute execute to see the arranged table interface in the preview window.
Because the coordinates of the Panel in the inspector window can be adjusted, but its size is also based on Ngui, in order for the entire Tabel interface to be centered, I have to calculate its size and then adjust the position of the entire panel.
There is a problem here, level is created with uisprite, so the height and width of the obtained is based on Ngui, not based on the world coordinate system, uitable padding is the same.
The crux of the problem is that all Ngui controls are based on Uiwidget, so starting with the source code, you will find:
public override vector3[] Worldcorners
{
Get
{
Vector2 offset = Pivotoffset;
float x0 =-offset.x * mwidth;
float y0 =-offset.y * mheight;
Float x1 = x0 + mwidth;
Float y1 = y0 + mheight;
Transform wt = Cachedtransform;
Mcorners[0] = wt. Transformpoint (x0, y0, 0f);
MCORNERS[1] = wt. Transformpoint (x0, y1, 0f);
MCORNERS[2] = wt. Transformpoint (x1, y1, 0f);
MCORNERS[3] = wt. Transformpoint (x1, y0, 0f);
return mcorners;
}
}
Its coordinates and dimensions are transformed by the Transform.transformpoint () method-the transformation position from its own coordinates to the world coordinates. OK, here's the tabletest script I created, which hangs on the panel.
Using Unityengine;
Using System.Collections;
public class Tabletest:monobehaviour {
Private Uisprite level;
Private uitable table;
Private float tablewidth, tableheight;
Use this for initialization
void Start () {
Table = Getcomponent<uitable> ();
Level = Getcomponentinchildren<uisprite> ();
int columns = Table.columns;
Calculate row for table
int count = Transform.childcount;
int rows = count% columns = = 0? Count/columns:count/columns + 1;
Calculates the width and height of the table, based on Ngui, which is its own original size
tablewidth = Columns * (level.width + 2 * table.padding.x);
Tableheight = rows * (level.height + 2 * table.padding.y);
Debug.Log ("Level width=" + Level.width + ", height=" + level.height);
Debug.Log ("Table width=" + Tablewidth + ", height=" + tableheight);
Converts the width height of a table to the offset of the world coordinate system
Vector3 Delta = transform. Transformpoint (New Vector3 (-TABLEWIDTH/2, TABLEHEIGHT/2, 0));
Transform. Translate (delta);
}
Update is called once per frame
void Update () {
}
}
If you run the game again, you can see that the entire table is centered throughout the preview window.
Unity Plugin Ngui Learning (8)--table and Ngui dimensions converted to world coordinate system dimensions