当前位置:网站首页>【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;
}
边栏推荐
- 渗透测试的介绍和防范
- 职业规划和发展
- Matlab生成dsp程序——官方例程学习(6)
- Junit4运行mvn test 测试套件升级方案
- Tinyxml2 reading and modifying files
- Illusion -- Animation blueprint, state machine production, character walking, running and jumping action
- Minimum number of C language
- Project practice, redis cluster technology learning (13)
- The road is blocked and long, and the line is coming
- Brief analysis of edgedb architecture
猜你喜欢

Bugkuctf-web21 (detailed problem solving ideas and steps)

2837xd code generation - stateflow (3)

ESLint 报错

UE4夜间打光笔记

Vs+qt set application icon

Tools used for Yolo object recognition and data generation

It is the most difficult to teach AI to play iron fist frame by frame. Now arcade game lovers have something

2837xd code generation - stateflow (1)

【虚幻】过场动画笔记

【UE5】动画重定向:如何将幻塔人物导入进游戏玩耍
随机推荐
ICLR 2022: how does AI recognize "things I haven't seen"?
Matlab generates DSP program -- official routine learning (6)
虚幻材质编辑器基础——如何连接一个最基本的材质
2837xd code generation - Summary
Mysql索引
Bugkuctf-web24 (problem solving ideas and steps)
ue4材质的入门和原理笔记
2837xd 代碼生成——補充(1)
Judging right triangle in C language
2837xd code generation - Supplement (3)
Tools used for Yolo object recognition and data generation
Applet development summary
【UE5】AI随机漫游蓝图两种实现方法(角色蓝图、行为树)
[ue5] two implementation methods of AI random roaming blueprint (role blueprint and behavior tree)
It is the most difficult to teach AI to play iron fist frame by frame. Now arcade game lovers have something
[ue5] animation redirection: how to import magic tower characters into the game
C language programming problems
Ue5 - ai Pursuit (Blueprint, Behavior tree)
About the college entrance examination
Basic notes of illusory AI blueprint (10000 words)