当前位置:网站首页>[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;
}
边栏推荐
- Alibaba cloud SLS log service
- 【Lua】常见知识点汇总(包含常见面试考点)
- Ue5 - ai Pursuit (Blueprint, Behavior tree)
- Operator exercises
- 【JetBrain Rider】构建项目出现异常:未找到导入的项目“D:\VisualStudio2017\IDE\MSBuild\15.0\Bin\Roslyn\Microsoft.CSh
- ICLR 2022: how does AI recognize "things I haven't seen"?
- What is call / cc- What is call/cc?
- Pytest framework implements pre post
- Eslint reports an error
- Blender模型导入ue、碰撞设置
猜你喜欢

Vscode set JSON file to format automatically after saving

UE5——AI追逐(藍圖、行為樹)

Blender stone carving

How to judge the quality of primary market projects when the market is depressed?

虚幻材质编辑器基础——如何连接一个最基本的材质
![[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

Ctrip starts mixed office. How can small and medium-sized enterprises achieve mixed office?

Pytest learning --base

Blender volume fog
![[unreal] key to open the door blueprint notes](/img/28/f2b00d84dc05183ce93e2a8ee3555f.png)
[unreal] key to open the door blueprint notes
随机推荐
【Unity3D】无法正确获取RectTransform的属性值导致计算出错
Leetcode -- the nearest common ancestor of 236 binary tree
Test -- Summary of interview questions
How does {} prevent SQL injection? What is its underlying principle?
Ctrip starts mixed office. How can small and medium-sized enterprises achieve mixed office?
2837xd code generation module learning (1) -- GPIO module
[unreal] animation notes of the scene
Blender ocean production
【UE5】动画重定向:如何将幻塔人物导入进游戏玩耍
【虚幻】自动门蓝图笔记
合并有序数列
Sum the two numbers to find the target value
Illusion -- Animation blueprint, state machine production, character walking, running and jumping action
What is call / cc- What is call/cc?
A model can do two things: image annotation and image reading Q & A. VQA accuracy is close to human level | demo can be played
Centos7 one click compilation and installation of PHP script
webUI自动化学习
Pytest learning --base
【虚幻】按键开门蓝图笔记
MySQL -- time zone / connector / driver type