当前位置:网站首页>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
边栏推荐
- tengine 内核参数
- NPM command -- install dependent packages -- Usage / explanation
- How many of the 10 most common examples of istio traffic management do you know?
- Path of class file generated by idea compiling JSP page
- After learning classes and objects, I wrote a date class
- 图应用详解
- Implementation of knowledge consolidation source code 2: TCP server receives and processes half packets and sticky packets
- 关于进程、线程、协程、同步、异步、阻塞、非阻塞、并发、并行、串行的理解
- Understanding of processes, threads, coroutines, synchronization, asynchrony, blocking, non blocking, concurrency, parallelism, and serialization
- How do programmers teach their bosses to do things in one sentence? "I'm off duty first. You have to work harder."
猜你喜欢
During pycharm debugging, the view is read only and pause the process to use the command line appear on the console input
CADD课程学习(7)-- 模拟靶点和小分子相互作用 (柔性对接 AutoDock)
Fedora/rehl installation semanage
Patent | subject classification method based on graph convolution neural network fusion of multiple human brain maps
Easyrecovery靠谱不收费的数据恢复电脑软件
查询mysql数据库中各表记录数大小
lora网关以太网传输
Mysql数据库慢sql抓取与分析
10个 Istio 流量管理 最常用的例子,你知道几个?
How do programmers teach their bosses to do things in one sentence? "I'm off duty first. You have to work harder."
随机推荐
[face recognition series] | realize automatic makeup
2328. 网格图中递增路径的数目(记忆化搜索)
BOM - location, history, pop-up box, timing
满足多元需求:捷码打造3大一站式开发套餐,助力高效开发
绑定在游戏对象上的脚本的执行顺序
10个 Istio 流量管理 最常用的例子,你知道几个?
Global and Chinese markets for endoscopic drying storage cabinets 2022-2028: Research Report on technology, participants, trends, market size and share
. Net interprocess communication
P2022 interesting numbers (binary & digit DP)
Word cover underline
P3033 [usaco11nov]cow steelchase g (similar to minimum path coverage)
Path of class file generated by idea compiling JSP page
Global and Chinese markets for MRI safe implants 2022-2028: technology, participants, trends, market size and share Research Report
PTA tiantisai l1-078 teacher Ji's return (15 points) detailed explanation
The global and Chinese market of negative pressure wound therapy unit (npwtu) 2022-2028: Research Report on technology, participants, trends, market size and share
CADD course learning (8) -- virtual screening of Compound Library
2328. Number of incremental paths in the grid graph (memory search)
P3500 [POI2010]TES-Intelligence Test(二分&离线)
11. Intranet penetration and automatic refresh
flink sql 能同时读多个topic吗。with里怎么写