当前位置:网站首页>[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多鏡頭(多機比特)切換
- [ue5] two implementation methods of AI random roaming blueprint (role blueprint and behavior tree)
- 【虚幻】武器插槽:拾取武器
- 2021-10-04
- 【虚幻】自动门蓝图笔记
- 阿里云SLS日志服务
- UE illusory engine programmed plant generator setup -- how to quickly generate large forests
- 2021-09-12
- Zlib download and use
- [illusory] weapon slot: pick up weapons
猜你喜欢
Summary of demand R & D process nodes and key outputs
Feature (5): how to organize information
Junit4 runs MVN test test suite upgrade scheme
What is the relationship between realizing page watermarking and mutationobserver?
Pytest learning --base
Brief analysis of edgedb architecture
[Yu Yue education] University Physics (Electromagnetics) reference materials of Taizhou College of science and technology, Nanjing University of Technology
虚幻材质编辑器基础——如何连接一个最基本的材质
Blender多镜头(多机位)切换
Introduction and Principle notes of UE4 material
随机推荐
Matlab generates DSP program -- official routine learning (6)
2837xd code generation module learning (2) -- ADC, epwm module, timer0
虚幻材质编辑器基础——如何连接一个最基本的材质
The primary market project galaxy will conduct public offering on coinlist on February 17
Large neural networks may be beginning to realize: the chief scientist of openai leads to controversy, and everyone quarrels
Junit4 runs MVN test test suite upgrade scheme
虚幻AI蓝图基础笔记(万字整理)
【虚幻】武器插槽:拾取武器
Ue5 - ai Pursuit (Blueprint, Behavior tree)
【UE5】蓝图制作简单地雷教程
How to judge the quality of primary market projects when the market is depressed?
Blender模型导入ue、碰撞设置
Mock Server基本使用方法
UE5——AI追逐(藍圖、行為樹)
Mixed development of uni app -- Taking wechat applet as an example
【JetBrain Rider】构建项目出现异常:未找到导入的项目“D:\VisualStudio2017\IDE\MSBuild\15.0\Bin\Roslyn\Microsoft.CSh
What is the relationship between realizing page watermarking and mutationobserver?
【Lua】常见知识点汇总(包含常见面试考点)
Tee command usage example
Blender camera surround motion, animation rendering, video synthesis