当前位置:网站首页>[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虚幻引擎程序化植物生成器设置——如何快速生成大片森林
- How to judge the quality of primary market projects when the market is depressed?
- Postman--使用
- Eslint reports an error
- Following nym, the new project Galaxy token announced by coinlist is gal
- Deep understanding of redis cache avalanche / cache breakdown / cache penetration
- 合并有序数列
- Project practice, redis cluster technology learning (13)
- The primary market project galaxy will conduct public offering on coinlist on February 17
- Project practice, redis cluster technology learning (VIII)
猜你喜欢

Blender multi lens (multi stand) switching

Blender海洋制作

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

ue4材质的入门和原理笔记

webUI自动化学习

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

Notes de base sur les plans illusoires d'IA (triés en 10 000 mots)
![[unreal] animation notes of the scene](/img/97/dafde0377b7c4337e1775db64ba6a4.png)
[unreal] animation notes of the scene

2021-10-04

Delivery mode design of Spartacus UI of SAP e-commerce cloud
随机推荐
Tee command usage example
Basic notes of illusory AI blueprint (10000 words)
A model can do two things: image annotation and image reading Q & A. VQA accuracy is close to human level | demo can be played
pytest--之测试报告allure配置
虚幻AI蓝图基础笔记(万字整理)
Sil/pil test of matlab code generation
[200 Shengxin literatures] 96 joint biomarkers of immune checkpoint inhibitor response in advanced solid tumors
【UE5】AI随机漫游蓝图两种实现方法(角色蓝图、行为树)
Vscode auto format
Remember a simple Oracle offline data migration to tidb process
Database -- acid of transaction -- introduction / explanation
Nonlinear optimization: establishment of slam model
[unreal] animation notes of the scene
Webui automated learning
The latest progress and development trend of 2022 intelligent voice technology
Operator exercises
虚幻材质编辑器基础——如何连接一个最基本的材质
Blender多鏡頭(多機比特)切換
Project practice, redis cluster technology learning (IX)
Mobile mall app solution: how much is it to make an app? Detailed explanation of APP mall development content