UGUI world coordinates to local coordinates (game Hud implementation) UI

Realization of the principle of the world coordinates are: UGUI world coordinates and the coordinates of belonging to two coordinate systems, they can not be converted between the need to be converted by the screen coordinate system (because the screen is fixed coordinates), that is, first game world coordinates scene by scene game Camera converted into screen coordinates (Camera.main.WorldToScreenPoint (point)), then by UICamera the UI screen coordinates into local coordinates (RectTransformUtility.ScreenPointToLocalPointInRectangle (canvas, screenPoint, uiCamera, out localPoint) )

doubt?
So who beginner white (bloggers also white) UGUI may be wondering why there UICamera, because of the Canvas UGUI RenderMode has three modes, the default is usually Overlay (full coverage), the other for the Camera.

When RenderMode to Overlay, UI coordinate system and the screen coordinate system is the same, you do not need to convert by UICamera, directly assign the first step to get the screen coordinates of Hud's localPosition it.
General game 3D objects to be achieved in the above UI, can not be used to cover the whole canvas model, but again create a Camera to render the UI alone, Clear Flags to Depth Only. When RenderMode as Camera, is required to obtain the above two local coordinates.
Note: The last assignment to the local coordinates Hud is not the position localPosition

Development: world coordinates, local coordinates, viewport coordinates, various coordinate systems to understand clearly, in learning shader is also useful

The following is a script to follow to achieve hud 3D objects, just a test, not a development code, script objects hanging on any game demo download

using UnityEngine;

SceneFollowUI class public: MonoBehaviour
{
public RectTransform HUD; // Hud
public RectTransform Canvas; // the UI parent
public Transform parent; // 3D objects follow
public Camera uiCamera; // UICamera

Vector3 offset; //hud偏移量
Vector3 cachePoint;
float originalDistance;
float factor = 1;
bool visiable = true;

void Start()
{
offset = hud.localPosition - WorldPointToUILocalPoint(parent.position);
cachePoint = parent.position;
originalDistance = GetCameraHudRootDistance();
UpdateVisible();
}

LateUpdate void ()
{
IF (! = cachePoint parent.position)
{
a float curDistance GetCameraHudRootDistance = ();
factor = originalDistance / curDistance;
updatePosition (); // update Hud position
UpdateScale (); // update Hud size
UpdateVisible () ; // update Hud is visible, provided on demand: factor or according to the distance and the camera settings, visible, the visible range of the camera field of view within a certain range, etc.
}
}


private void UpdateVisible()
{

}

private void UpdatePosition()
{
hud.localPosition = WorldPointToUILocalPoint(parent.position) + offset * factor;
cachePoint = parent.position;
}

private void UpdateScale()
{
hud.localScale = Vector3.one * factor;
}

private float GetCameraHudRootDistance(http://www.my516.com)
{
return Vector3.Distance(Camera.main.transform.position, parent.position);
}

private Vector3 WorldPointToUILocalPoint(Vector3 point)
{
Vector3 screenPoint = Camera.main.WorldToScreenPoint(point);
Vector2 localPoint;
RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas, screenPoint, uiCamera, out localPoint);
return localPoint;
}
}

--------------------- 

Guess you like

Origin www.cnblogs.com/ly570/p/11007559.html