当前位置:网站首页>Unity screen coordinates ugui coordinates world coordinates conversion between three coordinate systems
Unity screen coordinates ugui coordinates world coordinates conversion between three coordinate systems
2022-07-06 04:30:00 【lifereset】
First of all, understand a concept
UGUI Coordinate system , Take the center of the screen as the origin
Screen Coordinate system , Take the lower left corner of the screen as the origin
For example, you have a 3d object You want to add a blood bar to him And this blood bar is ugui The level in is very deep Then you can do this
var screenPoint = mainCamera.WorldToScreenPoint(this.hero.transform.position);
var localPosition = new Vector2();
UnityEngine.RectTransformUtility.ScreenPointToLocalPointInRectangle(this.blood.transform.parent as RectTransform,
screenPoint, canvas.worldCamera, out localPosition);
this.blood.transform.localPosition = localPosition;
Camera.WorldToScreenPoint
This is yours position That is to say World coordinate system Convert to screen coordinates
And then call ScreenPointToLocalPointInRectangle Namely localPosition Coordinates
Transformation of three coordinate systems You can refer to this figure
There are two classes WorldToScreenPoint Method
Camera Class has this method The return is Vector3
RectTransformUtility What comes back in the is Vector2
If you need h5 Inside globalToLocal perhaps localToGlobal
Then the following satisfies you
remember The camera is canvas Of
Vector2 localToGlobal(Transform transform , Vector2 vec2)
{
var screenPoint = RectTransformUtility.WorldToScreenPoint(canvas.worldCamera, transform.position);
var GlobalPosition = new Vector2();
RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.transform as RectTransform,
screenPoint, canvas.worldCamera, out GlobalPosition);
return GlobalPosition + vec2;
}
Vector2 globalToLocal(Transform transform , Vector2 vec2 )
{
var screenPoint = RectTransformUtility.WorldToScreenPoint(canvas.worldCamera, canvas.transform.position);
var localPosition = new Vector2();
UnityEngine.RectTransformUtility.ScreenPointToLocalPointInRectangle(transform as RectTransform,
screenPoint, canvas.worldCamera, out localPosition);
return localPosition + vec2;
}
By the way
img9 Put it in canvas On the root of And set it to take the lower left corner as the origin
Now
this.img9.transform.localPosition = new Vector2(0, 0);
So the picture is still canvas In the middle of the
because localPosition It doesn't matter where you set the origin
If you use the following sentence
this.img9.rectTransform.anchoredPosition = new Vector2(0, 0);
that img9 Just go to the lower left corner of the screen
边栏推荐
- 绑定在游戏对象上的脚本的执行顺序
- NPM command -- install dependent packages -- Usage / explanation
- Cross domain and jsonp details
- 2/12 didn't learn anything
- Crawler notes: improve data collection efficiency! Use of proxy pool and thread pool
- [tomato assistant installation]
- Dry goods collection | Vulkan game engine video tutorial
- Recommendation system (IX) PNN model (product based neural networks)
- Complete list of common functions of turtle module
- 729. My schedule I (set or dynamic open point segment tree)
猜你喜欢
Fedora/rehl installation semanage
How to solve the problem of slow downloading from foreign NPM official servers—— Teach you two ways to switch to Taobao NPM image server
How to estimate the population with samples? (mean, variance, standard deviation)
Basic explanation of turtle module - draw curve
解决“C2001:常量中有换行符“编译问题
How does computer nail adjust sound
Implementation of knowledge consolidation source code 1: epoll implementation of TCP server
View workflow
Guitar Pro 8.0最详细全面的更新内容及全部功能介绍
View 工作流程
随机推荐
颠覆你的认知?get和post请求的本质
Mixed development of QML and QWidget (preliminary exploration)
SharedPreferences 源码分析
Implementation of knowledge consolidation source code 1: epoll implementation of TCP server
Basic explanation of turtle module - draw curve
10 exemples les plus courants de gestion du trafic istio, que savez - vous?
11. Intranet penetration and automatic refresh
Fedora/REHL 安装 semanage
SharedPreferences source code analysis
Path of class file generated by idea compiling JSP page
729. My schedule I (set or dynamic open point segment tree)
综合能力测评系统
[HBZ share] reasons for slow addition and deletion of ArrayList and fast query
Data processing methods - smote series and adasyn
[face recognition series] | realize automatic makeup
2328. Number of incremental paths in the grid graph (memory search)
Redis —— Redis In Action —— Redis 实战—— 实战篇一 —— 基于 Redis 的短信登录功能 —— Redis + Token 的共享 session 应用— 有代码
Recommendation system (IX) PNN model (product based neural networks)
VPP performance test
POI add border