Unity Plugin Ngui Learning (8)--table and Ngui dimensions converted to world coordinate system dimensions

Source: Internet
Author: User

Based on the Unity plugin's Ngui Learning (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 form, select Panel, and then select Menu component->ngui->interaction->table, view the Inspector form, and add uitable to the properties of the panel.


Columns indicates that each line has several elements

Direction indicates that elements are added in the direction of the element after each column

Sorting how elements are sorted

Hide Inactive default tick, 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 form, add multiple level, and then select Panel, in Inspector, right-click Uitable and run execute to see the arranged table interface in the Preview form.



Since the coordinates of the panel in the Inspector form 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 point is that all Ngui controls are based on Uiwidget, so starting with the source code, you'll 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 is hanging 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 () {

}

}


This way, you can see that the entire table is centered throughout the preview form.


Unity Plugin Ngui Learning (8)--table and Ngui dimensions converted to world coordinate system dimensions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.