当前位置:网站首页>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
边栏推荐
- Unity中几个重要类
- How to realize automatic playback of H5 video
- tengine 内核参数
- Global and Chinese markets for endoscopic drying storage cabinets 2022-2028: Research Report on technology, participants, trends, market size and share
- Etcd database source code analysis -- etcdserver bootstrap initialization storage
- Sentinel sliding window traffic statistics
- Basic explanation of turtle module - draw curve
- hashlimit速率控制
- 【HBZ分享】ArrayList的增删慢查询快的原因
- How to solve the problem of slow downloading from foreign NPM official servers—— Teach you two ways to switch to Taobao NPM image server
猜你喜欢
Mysql数据库慢sql抓取与分析
Solve the compilation problem of "c2001: line breaks in constants"
1291_Xshell日志中增加时间戳的功能
One question per day (Mathematics)
捷码赋能案例:专业培训、技术支撑,多措并举推动毕业生搭建智慧校园毕设系统
About some basic DP -- those things about coins (the basic introduction of DP)
Introduction to hashtable
Fedora/rehl installation semanage
查询mysql数据库中各表记录数大小
How do programmers teach their bosses to do things in one sentence? "I'm off duty first. You have to work harder."
随机推荐
食品行业仓储条码管理系统解决方案
NPM command -- install dependent packages -- Usage / explanation
CADD课程学习(8)-- 化合物库虚拟筛选(Virtual Screening)
Easyrecovery靠谱不收费的数据恢复电脑软件
2327. 知道秘密的人数(递推)
POI add border
Solutions: word coverage restoration, longest serial number, Xiaoyu buys stationery, Xiaoyu's electricity bill
Lora gateway Ethernet transmission
Several important classes in unity
How does vs change the project type?
P3500 [POI2010]TES-Intelligence Test(二分&离线)
P3033 [usaco11nov]cow steelchase g (similar to minimum path coverage)
Mysql数据库慢sql抓取与分析
In depth MySQL transactions, stored procedures and triggers
脚本生命周期
Dry goods collection | Vulkan game engine video tutorial
npm命令--安装依赖包--用法/详解
1291_Xshell日志中增加时间戳的功能
Practical development of member management applet 06 introduction to life cycle function and user-defined method
2/12 didn't learn anything