unity中坐标、尺寸相关内容

unity中,设置父子关系后,子对象坐标转为相对坐标

调整canvas的宽和高

1. 先在Hierarchy面板选择Canvas对象
2. 然后在Inspector找到Canvas
3. 最后找到 Render Mode ,在下拉列表中选择 World Space 选项;即可更改画布的大小。

获取UI的宽和高

//
RectTransform rect = transform.GetComponent<RectTransform>(); Debug.Log("获取UI的宽:"+rect.rect.width); Debug.Log("获取UI的高:"+rect.rect.height);

  

Canvas canvas = GetComponentInParent<Canvas>();

float canvasWidth = canvas.GetComponent<RectTransform>().rect.width;
float canvasHeight = canvas.GetComponent<RectTransform>().rect.height;

Debug.Log(string.Format("Canvas 宽={0},高={1}", canvasWidth, canvasHeight));

  

posted @ 2023-07-15 12:06  牛大胆V5  阅读(75)  评论(0)    收藏  举报