当前位置:网站首页>[unity3d] cannot correctly obtain the attribute value of recttransform, resulting in calculation error
[unity3d] cannot correctly obtain the attribute value of recttransform, resulting in calculation error
2022-07-02 10:24:00 【I want to play games while my hair is heavy】
UGUI obtain RectTransform There are too many pits in some attributes of , Record the pits that bloggers have stepped on :
1. because ContentSizeFitter Component causes incorrect acquisition UI The width and height of
Refer to the practice of xuanyusong great God :Unity3D Research Institute of ContentSizeFitter Synchronous immediate response callback
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));
}
// Get width and height
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 Prompt on ‘some values driven by canvas’ Can't get Canvas Correct width and height
If you want to get Canvas Of RectTransform.rect.width / RectTransform.rect.height Be careful when this prompt appears ,rect It is likely that both width and height are zero , I guess it's also because RectTransform It takes a frame to set the correct width and height RectTransform in , So what bloggers do is : Start a collaborative process , Keep checking RectTransform.rect.width / RectTransform.rect.height, When both values are greater than zero, execute your callback
Be careful , If there is a need to convert the world coordinates of the object to a Canvas Under the UI Coordinates , We have to wait Canvas Of rect Both width and height are greater than zero , Otherwise, it is also wrong , Here is the conversion code :
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;
}
边栏推荐
- ue虚幻引擎程序化植物生成器设置——如何快速生成大片森林
- 【虚幻4】从U3D到UE4的转型之路
- [ue5] two implementation methods of AI random roaming blueprint (role blueprint and behavior tree)
- Merge ordered sequence
- Blender模型导入ue、碰撞设置
- 【MySQL】连接MySQL时出现异常:Connection must be valid and open
- 【UE5】AI随机漫游蓝图两种实现方法(角色蓝图、行为树)
- Translation d30 (with AC code POJ 28:sum number)
- 网络通信学习
- [IDL] Research
猜你喜欢

Blender石头雕刻

Bookmark collection management software suspension reading and data migration between knowledge base and browser bookmarks

pytest框架实现前后置

【Unity3D】嵌套使用Layout Group制作拥有动态子物体高度的Scroll View

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

2837xd code generation module learning (3) -- IIC, ECAN, SCI, watchdog, ECAP modules

Blender体积雾

虚幻——动画蓝图、状态机制作人物走跑跳动作

Blender multi lens (multi stand) switching

测试--面试题总结
随机推荐
VLAN experiment
MySQL index
go语言入门
Post disaster reconstruction -- Floyd thought
What wires are suitable for wiring on bread board?
Leetcode -- the nearest common ancestor of 236 binary tree
两数之和,求目标值
Blender ocean production
Basic notes of illusory AI blueprint (10000 words)
MPLS experiment
2021-10-04
allure--常用配置项
About the college entrance examination
Blender多镜头(多机位)切换
What is the relationship between realizing page watermarking and mutationobserver?
Project practice, redis cluster technology learning (IX)
Project practice, redis cluster technology learning (11)
【Unity3D】制作进度条——让Image同时具有Filled和Sliced的功能
UE4 night lighting notes
【虚幻4】从U3D到UE4的转型之路