当前位置:网站首页>[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;
}
边栏推荐
- Blender海洋制作
- Alibaba cloud ack introduction
- How to achieve the top progress bar effect in background management projects
- Project practice, redis cluster technology learning (VIII)
- What is call / cc- What is call/cc?
- Summary of demand R & D process nodes and key outputs
- 虛幻AI藍圖基礎筆記(萬字整理)
- [illusory] automatic door blueprint notes
- ue4材质的入门和原理笔记
- Following nym, the new project Galaxy token announced by coinlist is gal
猜你喜欢

【JetBrain Rider】构建项目出现异常:未找到导入的项目“D:\VisualStudio2017\IDE\MSBuild\15.0\Bin\Roslyn\Microsoft.CSh

VLAN experiment

Blender模型导入ue、碰撞设置

Mysql索引
![[unreal] animation notes of the scene](/img/97/dafde0377b7c4337e1775db64ba6a4.png)
[unreal] animation notes of the scene

Blender多鏡頭(多機比特)切換

Pytest framework implements pre post

【MySQL】连接MySQL时出现异常:Connection must be valid and open

SAP Spartacus express checkout design

Junit4 runs MVN test test suite upgrade scheme
随机推荐
Network real-time video streaming based on OpenCV
【leetcode】33. Search rotation sort array
pytest学习--base
Alibaba cloud SMS service
MySQL index
Blender multi lens (multi stand) switching
Career planning and development
Remember the use of add method once
2837xd code generation module learning (2) -- ADC, epwm module, timer0
[unreal] animation notes of the scene
JS reduce accumulator
2021-10-02
【虚幻】自动门蓝图笔记
Blender camera surround motion, animation rendering, video synthesis
【避坑指南】Unity3D项目接入腾讯Bugly工具时遇到的坑
Blender stone carving
判断数组中是否存在重复元素
网络通信学习
【UE5】AI随机漫游蓝图两种实现方法(角色蓝图、行为树)
2021-09-12