当前位置:网站首页>【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;
}
边栏推荐
- 高考那些事
- Project practice, redis cluster technology learning (6)
- 职业规划和发展
- UE illusory engine programmed plant generator setup -- how to quickly generate large forests
- [200 Shengxin literatures] 95 multiomics exploration TNBC
- Tee command usage example
- 【leetcode】33. Search rotation sort array
- Project practice, redis cluster technology learning (11)
- Ue5 - AI pursuit (blueprint, behavior tree)
- ERROR 1118 (42000): Row size too large (> 8126)
猜你喜欢

Off grid control of three-phase inverter - PR control
![[illusory] weapon slot: pick up weapons](/img/a7/1e395fc9cdfd0359e7ae4d2313290d.png)
[illusory] weapon slot: pick up weapons

Large neural networks may be beginning to realize: the chief scientist of openai leads to controversy, and everyone quarrels

Unreal material editor foundation - how to connect a basic material
![[ue5] animation redirection: how to import magic tower characters into the game](/img/a0/084381493cbc3992b61ef1b8ffce36.png)
[ue5] animation redirection: how to import magic tower characters into the game

Applet development summary

Blender石头雕刻

Summary of demand R & D process nodes and key outputs

阿里云短信服务

Tee command usage example
随机推荐
How to achieve the top progress bar effect in background management projects
The latest progress and development trend of 2022 intelligent voice technology
Remember the use of add method once
Image recognition - data annotation
QT qlabel style settings
[illusory] automatic door blueprint notes
2837xd code generation - Supplement (1)
Mobile mall app solution: how much is it to make an app? Detailed explanation of APP mall development content
渗透测试的介绍和防范
Configuration programmée du générateur de plantes du moteur illusoire UE - - Comment générer rapidement une grande forêt
2837xd代码生成模块学习(2)——ADC、ePWM模块、Timer0
虛幻AI藍圖基礎筆記(萬字整理)
Alibaba cloud SLS log service
High level application of SQL statements in MySQL database (II)
Inverter Simulink model -- processor in the loop test (PIL)
2837xd code generation - stateflow (4)
Ue5 - ai Pursuit (Blueprint, Behavior tree)
Applet development summary
C language: making barrels
Image recognition - Data Cleaning