RectTransform

1.top

GetComponent<RectTransform>().offsetMax = new Vector2(GetComponent<RectTransform>().offsetMax.x, top);



2.bottom
GetComponent<RectTransform>().offsetMin = new Vector2(GetComponent<RectTransform>().offsetMin.x, bottom);



3.width,height
GetComponent<RectTransform>().sizeDelta = new Vector2(width, height);



4.pos
GetComponent<RectTransform>().anchoredPosition3D = new Vector3(posx,posy,posz);
GetComponent<RectTransform>().anchoredPosition = new Vector2(posx,posy);


  • Text中的可以单独指定某些文字的颜色,只需将想要变色的文本放在<color=**></color>之间即可,如“吃<color=#ff7a38>橙色物品</color>有机会获得<color=red>红色宝石</color>”,同样适用于NGUI。
  • 两张图片,如头像框,头像在背景图之上,头像会挡住背景的点击事件,要实现全框的点击效果,只需要将头像作为背景的子物体就可以了。当然头像上不能有Button组件。
  • 自带的CanvasGroup组件可以实现屏蔽其下所有UI的点击等事件触发,也可以实现整休改变所有子UI的alpha透明度。
  • RectMask2D,类似Mask,但:
    the limitations of RectMask2D control are:
  • It only works in 2D space
  • It will not properly mask elements that are not coplanar

The advantages of RectMask2D are:

  • It does not use the stencil buffer
  • No extra draw calls
  • No material changes
  • Fast performance
  • UI和3D场景同时都需要响应触摸事件,在判断3D响应之前要先判断手指是否点击在UI上:
void Update ()
{
    if (EventSystem.current.IsPointerOverGameObject ())
        Debug.Log ("当前触摸在UI上");
    else
        Debug.Log ("当前没有触摸在UI上");
}

unity设置不了锚点_锚点

另外PhysicsRaycaster可以做MASK层次过滤,可以屏蔽你不想要的层触发点击事件,不过貌似对于2d物体没有作用。

   Graphics Raycaster的Raycast是个虚函数,可以写个Graphics Raycaster的派生类,在默认的Raycast操作执行完以后,用自定义的layer进行筛选,把不需要响应的gameobject去掉。这样就可以实现只响应某个layer的需求了。新手引导中只要把需要响应的gameobject设置为特定layer就行了。

  •  Content Size Fitter组件:自动改变RectTransfrom的size大小。如Text根据文字内容的长短自动缩放控件本身的大小,如一个ScrollRect下的Grid中的元素超时Rect时拖动无效的原因便是Grid未加上该组件它自身的大小未跟随元素的增多而增大。
    注意自动变化宽高时的方向是根据Pivot的设置,若Pivot在中心时则会上下左右同时都会有缩放,若Pivot在顶部时则大小只会从底部放大或缩小。
  • Grid的使用:若元素(子物体)是大小一样的则优先使用Grid Layout Group组件;若元素是大小不一的则选用Horizontal/Vertical layout Group并配合Layout Element组件。
    通过灵活组合ScrollRect、ContentSizeFitter、GridLayoutGroup、HorizontalLayoutGroup/VerticalLayoutGroup、LayoutElement、可以实现很复杂的动态增删组合UI效果。
  • RectTransform的大小改变时有一个回调函数:OnRectTransformDimensionsChange。当ContentSizeFitter来自动调整RectTransform的大小时并不能立马生效,而是需要到下一帧才能得到正确的大小,此时该回调便有用武之地了。
  • RectTransform的几个API说明:
  • * public void GetLocalCorners(Vector3[] fourCornersArray):传入4个长度的Vector3数组,得到该RectTransform从左下开始到右下的四个角在父物体下的局部坐标。该坐标与该物体及父物体的中心点及锚点有关,经测试即将该RectTransform的anchoredPosition设置为0(父物体的0位置下)时四个角所在位置。
    * public void GetWorldCorners(Vector3[] fourCornersArray):得到该RectTransform的四个角的世界坐标,不用关心当前及父对象的中心点及锚点的设置。
    * public void SetSizeWithCurrentAnchors(Axis axis, float size):设置RectTransform的宽或高,可以不用关心当前及父对象的中心点及锚点的情况而直接设置宽高。当anchor的四角合一时该函数便跟设置sizeDelta效果一样。
    * public void SetInsetAndSizeFromParentEdge(Edge edge, float inset, float size):设置RectTransform相对父物体的某一边缘的距离和宽高,同样可以不关心当前及父对象的中心点及锚点的情况而直接设置宽高。edge父物体的上下左右某一边,inset为到edge边的距离,size为要将RectTransform设置的宽度或高度,当edge为左右时size为宽度,当edge为上下时size为高度。注意该函数会根据edge改变RectTransform的anchor。
    如:rectTrans.SetInsetAndSizeFromParentEdge (RectTransform.Edge.Top, 200, 400);rt.SetInsetAndSizeFromParentEdge (RectTransform.Edge.Left, 100, 300);该两次调用后rectTrans的高为400,上边距离父物体的上边为200,宽为300,左边距离父物体的左边为100。





在代码中动态改变RectTransform大小的方法如下所示:

1:直接对sizeDelta属性进行赋值,其中X和Y可以对应理解成width和height。sizeDelta的具体含义:若achors是一个点的话则代表宽高,否则为到锚点的距离

var rt = gameObject.GetComponent<RectTransform>();
rt.sizeDelta = new Vector2(100, 30);

2:使用SetSizeWithCurrentAnchors函数来进行设定,其中Horizontal和Vertical分别对应宽和高。此函数受当前锚点和中心点的影响。

var rt = gameObject.GetComponent<RectTransform>();
rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 100);
rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 30);

3:使用SetInsetAndSizeFromParentEdge函数来进行设定。此函数不受锚点和中心的影响,其中第一个参数代表对齐方式,第二个参数为距离边界的距离,第三个参数为宽度。

var rt = gameObject.GetComponent<RectTransform>();
rt.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Right, 0, 100);
rt.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Bottom, 0, 30);