具体说明都在代码中,同学们可以直接拷过去配合测试理解,代码如下:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class API_RectTransform : MonoBehaviour { /// <summary> /// UGUI /// </summary> public RectTransform m_text = null; // Use this for initialization void Start () { Test_RectTransform(); } // Update is called once per frame void Update () { } /// <summary> /// RectTransform组件下各成员以及成员函数的使用 /// </summary> private void Test_RectTransform() { //如果锚框是该UGUI的中心点表示的就是宽度和高度,如果水平的锚点分开了,y还是高度,x变成了-(left+right).如果垂直的锚点分开了,x还是宽度,y变成了-(top+bottom) Debug.Log("SizeDelta :" + m_text.sizeDelta); //这个是针对锚点的,锚点时相对于父容器定义的,所以这两个属性也是相对于父容器的.分别指锚点占父容器尺寸的百分比位置. Debug.Log("Anchor Min :" + m_text.anchorMin); Debug.Log("Anchor Max :" + m_text.anchorMax); //分别指左下角相对于左下角锚点的距离以及右上角相对于右上角锚点的距离 Debug.Log("Offset Min :" + m_text.offsetMin); Debug.Log("Offset Max :" + m_text.offsetMax); //中心点相对于四个锚点中点的坐标 Debug.Log("AnchoredPosition :" + m_text.anchoredPosition); Debug.Log("AnchoredPosition3D :" + m_text.anchoredPosition3D); //RectTransform实际的宽高,不受锚点和中心点的影响 Debug.Log("Size : (" + m_text.rect.width + ", " + m_text.rect.height + ")"); //获取图元的四个角在世界坐标系的坐标(声明一个四个长度的Vector3数组作为参数传进去,方法执行完毕该数组会被赋值好,依次顺序从左下到右下) //m_text.GetWorldCorners(Vector3[] _fourCornersArray); //获取图元的四个角在父物体坐标系的坐标(声明一个四个长度的Vector3数组作为参数传进去,方法执行完毕该数组会被赋值好,依次顺序从左下到右下) //m_text.GetLocalCorners(Vector3[] _fourCornersArray); //此函数根据Pviot进行设定RectTransform的大小,其中Horizontal和Vertical分别对应宽和高。且不受Anchor的影响。 //m_text.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 200); //m_text.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 200); //此函数来进行设定RectTransform的大小。此函数不受锚点和中心的影响,其中第一个参数代表对齐方式,第二个参数为距离边界的距离,第三个参数为宽度。 //m_text.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 100, 300); //m_text.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, 100, 300); //m_text.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Right, 100, 300); //m_text.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Bottom, 100, 300); } }