In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "how to convert 3D coordinates into UGUI coordinates in Unity". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to convert 3D coordinates into UGUI coordinates in Unity".
0. Preface
In actual development, we often encounter problems that require 3D and 2D coordinate conversion, such as following the movement of a character synchronously, the number of damage appears on the character's head, and dragging a UI from a 2D backpack to a 3D object, which are essentially problems of 3D coordinate and UI coordinate conversion.
1. Core thought
To convert 3D coordinates to UI coordinates is to convert them into screen coordinates first, and then to convert them into UI coordinates.
So the coordinate transformation is divided into three parts.
3D coordinate
Screen coordinates
UI coordinates
The screen coordinate system of Unity stipulates that the lower left corner is the origin, the X axis is positive to the right, and the Y axis is positive.
As an additional knowledge point, the coordinate system of GUI stipulates that the upper left corner is the origin, the X axis is positive to the right, and the Y axis is positive to the right, which is different from the screen coordinates, but we don't need this knowledge point in this article.
Here the UI coordinate refers to the UGUI coordinate, so we need to use the Canvas-related parameters when calculating.
two。 Code example
Here, it takes the blood strip to follow the character movement as the requirement, and completes the core code of coordinate transformation.
Using UnityEngine
Public class HPFollow: MonoBehaviour {/ / public Transform heroHeadTrans; / / camera on top of a character. Camera.main is not used because of the high performance consumption of public Camera mainCamera.
/ / Blood strips public RectTransform hpTrans
/ / Canvas public RectTransform canvasTrans
/ / Update is called once per frame void Update () {/ / convert 3D coordinates to screen coordinates Vector2 screenPoint = RectTransformUtility.WorldToScreenPoint (mainCamera, heroHeadTrans.position) first
/ / then convert the screen coordinates to UGUI coordinates Vector2 localPoint
If (RectTransformUtility.ScreenPointToLocalPointInRectangle (canvasTrans, screenPoint, mainCamera, out localPoint)) {hpTrans.anchoredPosition = localPoint;}}
Don't forget to modify Canvas's RenderMode.
These are all the contents of the article "how to transform 3D coordinates into UGUI coordinates in Unity". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.