Unity RectTransform强制刷新UI、拖拽坐标转换、设置属性等

RectTransform 是整个每个UI控件必然有的组件,它决定了UI的布局大小等,在开发中我们经常都会使用到,比如拖拽,修改宽高,获取宽高等操作,下面记录一下自己对RectTransform的理解。
RectTransform我经常会使用到下面的方法:

RectTransform. SetSizeWithCurrentAnchors(Axis axis, float size) 设置宽高,Axis代表垂直和水平对应宽和高

RectTransform. GetLocalCorners(Vector3[] fourCornersArray) 以Pivot为原点坐标原点 ,获取rect四个角的本地坐标 第一个为左下角,第二个为左上角,第三个为右上角,第四个为右下角

RectTransform. GetLocalCorners(Vector3[] fourCornersArray) 以Pivot为原点坐标原点 ,获取rect四个角的世界坐标 第一个为左下角,第二个为左上角,第三个为右上角,第四个为右下角

如果要将这些世界坐标转成屏幕上的坐标使用Camera.main.WorldToScreenPoint(worldPos);

RectTransform包含rect,rect经常用到的方法

rect.xMin 以Pivot为原点坐标原点,最左侧距离Pivot的距离(和rect.left一样)
rect.xMax以Pivot为原点坐标原点,最右侧距离Pivot的距离(和rect.right一样)
rect.yMin以Pivot为原点坐标原点,最上侧距离Pivot的距离(和rect.top一样)
rect.yMax 以Pivot为原点坐标原点,最yMax侧距离Pivot的距离(和rect.bottom一样)

rect.width 矩形框的宽度
rect.height矩形框的高度

rect.max 以Pivot为原点坐标原点 rect右下角的本地坐标
rect.min 以Pivot为原点坐标原点 rect左下角的本地坐标
rect.center 以Pivot为原点坐标原点 rect中心的本地坐标

Contains(Vector3 point, bool allowInverse);判断一个点是否在矩形框里面,第二个参数表示是否允许检测宽高为负值的情况;

RectTransformUtility常用的方法
判断屏幕上的点是否落在RectTransform里面,具体情况要看Canvas的渲染方式

(1) canvas Render Mode是Screen Space-Overly
使用RectTransformUtility.RectangleContainsScreenPoint(RectTransform rect, Vector2 screenPoint),带一个参数是Rectransform,第二个是屏幕上的点
(2) canvas Render Mode是Screen Space-Camera
使用RectTransformUtility.RectangleContainsScreenPoint(RectTransform rect, Vector2 screenPoint, Camera cam) 第一个参数是Rectransform ,第二个参数是屏幕上的点,第三个参数是渲染Canvas的照相机

UI拖拽时坐标转换 关键在于Canvas渲染方式,如果采用屏幕渲染就不需要传入渲染Camera就是用第一种方式,如果是第二种就需要传如渲染的照相机进行运算。不出入照相机就没办法计算出当前UI的世界坐标点。

(1)canvas Render Mode是Screen Space-Overly
RectTransformUtility.ScreenPointToWorldPointInRectangle(canvas.GetComponent(), Input.mousePosition, canvas.worldCamera, out worldPos)
(2)canvas Render Mode是Screen Space-Camera
RectTransformUtility.ScreenPointToWorldPointInRectangle(canvas.GetComponent(), Input.mousePosition, null, out worldPos)

计算RectTransform Bounds的大小

以povit为坐标原点,计算Bounds大小
RectTransformUtility.CalculateRelativeRectTransformBounds(rec,transform)

强制刷新UI,在Unity 里面有时候使用自动布局的控件添加内容以后不会刷新,这个时候可以调用下面的方法强制刷新UI
LayoutRebuilder.ForceRebuildLayoutImmediate(rect)

下面是一些测试代码
public class RectTransformTest : MonoBehaviour
{

    public RectTransform rec;
    public Canvas canvas;
    void Update()
    {
        if (Input.GetMouseButtonDown(0)) {
			//判断鼠标点下去的点是否在rec里面  Canvas 渲染模式为Screen Space-Overly的时候使用
			// Debug.Log(RectTransformUtility.RectangleContainsScreenPoint(rec, Input.mousePosition));

			//判断鼠标点下去的点是否在rec里面  Canvas 渲染模式为Screen Space-Camera的时候使用
			//Debug.Log(RectTransformUtility.RectangleContainsScreenPoint(rec, Input.mousePosition, canvas.worldCamera));

			//将屏幕上的位置转换成RectTransform下的本地坐标
			//Vector2 localPos;
			//RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.GetComponent<RectTransform>(), Input.mousePosition, canvas.worldCamera, out localPos);
			//Debug.Log("本地坐标" + localPos);
			//Vector3 worldPos;

			//将屏幕上的位置转换成RectTransform下的本地坐标
			//RectTransformUtility.ScreenPointToWorldPointInRectangle(canvas.GetComponent<RectTransform>(), Input.mousePosition, canvas.worldCamera, out worldPos);
			//rec.position = worldPos;
			//Debug.Log("世界坐标"+worldPos);

			//获取RectTransformBounds  povit为原点
			//Bounds bounds= RectTransformUtility.CalculateRelativeRectTransformBounds(rec,transform);
			//Debug.Log(bounds.center);
			//Debug.Log(bounds.min);
			//Debug.Log(bounds.max);

		}
		if (Input.GetKeyDown(KeyCode.Space)) {

            // Debug.Log("anchoredPosition==" + rec.anchoredPosition);
			// Debug.Log("offsetMax==" + rec.offsetMax);
			// Debug.Log("offsetMin==" + rec.offsetMin);
			// Debug.Log("anchoredPosition3D==" + rec.anchoredPosition3D);
			// Debug.Log("anchorMin==" + rec.anchorMin);
			// Debug.Log("anchorMax==" + rec.anchorMax);
			// Debug.Log("pivot==" + rec.pivot);
			// Debug.Log("sizeDelta==" + rec.sizeDelta);


			//Debug.Log("max" + rec.rect.max);
			//Debug.Log("min" + rec.rect.min);
			//Debug.Log("center" + rec.rect.center);
			//Debug.Log("position" + rec.rect.position);
			//Debug.Log("size" + rec.rect.size);

			//Vector3[] pos = new Vector3[4];
			//rec.GetWorldCorners(pos);

			//Debug.Log(pos[0]);
			//Debug.Log(pos[1]);
			//Debug.Log(pos[2]);
			//Debug.Log(pos[3]);
            // Camera.main.WorldToScreenPoint(pos[0]);

            //Debug.Log("屏幕上的点"+Camera.main.WorldToScreenPoint(pos[0]));


			//Debug.Log("xMin" + rec.rect.xMin);
			//Debug.Log("xMax" + rec.rect.xMax);
			//Debug.Log("yMin" + rec.rect.yMin);
			//Debug.Log("yMax" + rec.rect.yMax);

			//rec.SetSizeWithCurrentAnchors(Axis.Horizontal,1000);
            //rec.SetSizeWithCurrentAnchors(Axis.Vertical, 500);

            //rec.SetInsetAndSizeFromParentEdge(Edge.Left,10, 500);
            //rec.SetInsetAndSizeFromParentEdge(Edge.Top, 10, 500);
        }

    }
}

在这里插入图片描述

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Unity中,RectTransformTransform都可以用来表示游戏对象的位置、旋转和缩放。RectTransform主要用于UI元素在画布上的定位和排版,而Transform则用于3D场景中游戏对象的定位和排版。它们的位置转换可以通过以下方式实现: 1. 从RectTransformTransform的转换 要将RectTransform的位置转换为Transform的位置,需要使用RectTransform的anchoredPosition属性和anchorMin、anchorMax属性,以及Canvas的scaleFactor属性。具体步骤如下: - 获取RectTransform的anchoredPosition属性,并将其转换为Canvas坐标系下的位置。 - 获取RectTransform的anchorMin和anchorMax属性,计算出UI元素的宽度和高度。 - 获取Canvas的scaleFactor属性,计算出Canvas坐标系和世界坐标系之间的缩放比例。 - 将Canvas坐标系下的位置转换为世界坐标系下的位置,即可得到Transform的位置。 2. 从TransformRectTransform的转换 要将Transform的位置转换为RectTransform的位置,需要使用Canvas的RenderMode属性RectTransform的anchoredPosition属性。具体步骤如下: - 获取Canvas的RenderMode属性,判断Canvas的渲染模式是Screen Space还是World Space。 - 如果Canvas的渲染模式是Screen Space,直接将Transform的位置转换为屏幕坐标系下的位置。 - 如果Canvas的渲染模式是World Space,需要将Transform的位置转换为Canvas坐标系下的位置,然后再转换为RectTransform的anchoredPosition属性。具体步骤同从RectTransformTransform的转换。 需要注意的是,RectTransformTransform的坐标系不同,转换时需要注意坐标系的转换。此外,RectTransformTransform的旋转和缩放也需要进行转换。可以使用Transform的localRotation和localScale属性,或使用RectTransform的rotation和sizeDelta属性来实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值