当前位置:网站首页>【Unity3D】无法正确获取RectTransform的属性值导致计算出错
【Unity3D】无法正确获取RectTransform的属性值导致计算出错
2022-07-02 06:36:00 【趁着头发多我想做游戏】
UGUI获取RectTransform的某些属性时坑实在是太多了,以博主踩过的坑记录一下:
1.由于ContentSizeFitter组件导致不能正确获取UI的宽高
参考宣雨松大神的做法:Unity3D研究院之ContentSizeFitter同步立即响应回调
public static Vector2 GetPreferredSize (RectTransform rect, ContentSizeFitter fitter) {
if (rect == null || fitter == null) {
return Vector2.zero;
}
LayoutRebuilder.ForceRebuildLayoutImmediate (rect);
return new Vector2 (HandleSelfFittingAlongAxis (0, fitter, rect), HandleSelfFittingAlongAxis (1, fitter, rect));
}
//获取宽和高
private static float HandleSelfFittingAlongAxis (int axis, ContentSizeFitter fitter, RectTransform rect) {
ContentSizeFitter.FitMode fitting = (axis == 0 ? fitter.horizontalFit : fitter.verticalFit);
if (fitting == ContentSizeFitter.FitMode.MinSize) {
var size = LayoutUtility.GetMinSize (rect, axis);
return size;
} else {
var size = LayoutUtility.GetPreferredSize (rect, axis);
return size;
}
}
2. Canvas上有提示 ‘some values driven by canvas’ 时无法获取Canvas正确的宽高
如果想获取Canvas的RectTransform.rect.width / RectTransform.rect.height出现这个提示时要小心了,rect很有可能宽高都是零,估计也是因为RectTransform需要等一帧才会把正确的宽高设置到RectTransform中,因此博主的做法是:开一个协程,不断检测RectTransform.rect.width / RectTransform.rect.height,当两个值都大于零才执行你的回调
注意,如果有需求把物体的世界坐标转换到某个Canvas下的UI坐标时,也要等到Canvas的rect宽高都大于零,否则也是错的,这里提供一下转换的代码:
public static Vector3 WorldToUGUIPosition (Camera worldCam, Vector3 worldPos, Camera uiCamera, RectTransform uiRect) {
var pos = worldCam.WorldToScreenPoint (worldPos);
Vector2 result;
RectTransformUtility.ScreenPointToLocalPointInRectangle(uiRect, pos, uiCamera, out result);
return result;
}
边栏推荐
- Introduction and prevention of penetration test
- 虚幻AI蓝图基础笔记(万字整理)
- Junit4运行mvn test 测试套件升级方案
- 2837xd code generation module learning (2) -- ADC, epwm module, timer0
- Ue5 - ai Pursuit (Blueprint, Behavior tree)
- 2837xd code generation module learning (3) -- IIC, ECAN, SCI, watchdog, ECAP modules
- Project practice, redis cluster technology learning (16)
- The latest progress and development trend of 2022 intelligent voice technology
- Junit5 支持suite的方法
- [Yu Yue education] University Physics (Electromagnetics) reference materials of Taizhou College of science and technology, Nanjing University of Technology
猜你喜欢

Alibaba cloud SLS log service

阿里云短信服务

ICLR 2022: how does AI recognize "things I haven't seen"?

Skywalking theory and Practice
![[Yu Yue education] University Physics (Electromagnetics) reference materials of Taizhou College of science and technology, Nanjing University of Technology](/img/a9/ffd5d8000fc811f958622901bf408d.png)
[Yu Yue education] University Physics (Electromagnetics) reference materials of Taizhou College of science and technology, Nanjing University of Technology

Sil/pil test of matlab code generation

2.14 is it Valentine's day or Valentine's day when the mainstream market continues to fluctuate and wait for changes?

虚幻材质编辑器基础——如何连接一个最基本的材质

ue虛幻引擎程序化植物生成器設置——如何快速生成大片森林

MySQL index
随机推荐
How does {} prevent SQL injection? What is its underlying principle?
职业规划和发展
A model can do two things: image annotation and image reading Q & A. VQA accuracy is close to human level | demo can be played
Image recognition - data annotation
Project practice, redis cluster technology learning (VIII)
Bugkuctf-web16 (backup is a good habit)
Judging right triangle in C language
go语言入门
Sil/pil test of matlab code generation
Basic notes of illusory AI blueprint (10000 words)
2837xd code generation - Summary
虚幻材质编辑器基础——如何连接一个最基本的材质
Int to string, int to qstring
Project practice, redis cluster technology learning (10)
2837xd code generation module learning (1) -- GPIO module
Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer
2.14 is it Valentine's day or Valentine's day when the mainstream market continues to fluctuate and wait for changes?
How to handle error logic gracefully
Bookmark collection management software suspension reading and data migration between knowledge base and browser bookmarks
2837xd code generation module learning (3) -- IIC, ECAN, SCI, watchdog, ECAP modules