当前位置:网站首页>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
边栏推荐
- VNCTF2022 WriteUp
- P2022 interesting numbers (binary & digit DP)
- The global and Chinese market of negative pressure wound therapy unit (npwtu) 2022-2028: Research Report on technology, participants, trends, market size and share
- Implementation of knowledge consolidation source code 1: epoll implementation of TCP server
- When debugging after pycharm remote server is connected, trying to add breakpoint to file that does not exist: /data appears_ sda/d:/segmentation
- Tengine kernel parameters
- Practical development of member management applet 06 introduction to life cycle function and user-defined method
- 2/13 review Backpack + monotonic queue variant
- Etcd database source code analysis -- etcdserver bootstrap initialization storage
- Mixed development of QML and QWidget (preliminary exploration)
猜你喜欢
10 exemples les plus courants de gestion du trafic istio, que savez - vous?
How many of the 10 most common examples of istio traffic management do you know?
满足多元需求:捷码打造3大一站式开发套餐,助力高效开发
MLAPI系列 - 04 - 网络变量和网络序列化【网络同步】
About some basic DP -- those things about coins (the basic introduction of DP)
Canal synchronizes MySQL data changes to Kafka (CentOS deployment)
Solutions: word coverage restoration, longest serial number, Xiaoyu buys stationery, Xiaoyu's electricity bill
综合能力测评系统
Slow SQL fetching and analysis of MySQL database
10個 Istio 流量管理 最常用的例子,你知道幾個?
随机推荐
HotSpot VM
Path of class file generated by idea compiling JSP page
Recommendation | recommendation of 9 psychotherapy books
P3033 [usaco11nov]cow steelchase g (similar to minimum path coverage)
After learning classes and objects, I wrote a date class
Understanding of processes, threads, coroutines, synchronization, asynchrony, blocking, non blocking, concurrency, parallelism, and serialization
Etcd database source code analysis -- etcdserver bootstrap initialization storage
绑定在游戏对象上的脚本的执行顺序
VPP性能测试
NPM command -- install dependent packages -- Usage / explanation
[network] channel attention network and spatial attention network
2/13 qaq~~ greed + binary prefix sum + number theory (find the greatest common factor of multiple numbers)
满足多元需求:捷码打造3大一站式开发套餐,助力高效开发
P2102 地砖铺设(dfs&贪心)
Certbot failed to update certificate solution
Mysql database storage engine
Global and Chinese markets for endoscopic drying storage cabinets 2022-2028: Research Report on technology, participants, trends, market size and share
Solve the compilation problem of "c2001: line breaks in constants"
Coreldraw2022 new version new function introduction cdr2022
Global and Chinese markets for fire resistant conveyor belts 2022-2028: Research Report on technology, participants, trends, market size and share