描述:3d场景中有一人物target, ui界面上有一血条blood ,让血条跟随人物移动。
两个相机变量:3d_camera 、 ui_camera
步骤:
(1) 将3d物体坐标转换为屏幕坐标
var screenPos = 3d_camera .WorldToScreenPoint(target.Position);
(2)将屏幕坐标转为ui坐标
Vector2 localPos;
if(RectTransformUtility.ScreenPointToLocalPointInRectangle(parentRect, screenPos, ui_camera , out localPos))
{
blood.LocalPosition = localPos;
}
ScreenPointToLocalPointInRectangle的参数描述:
参数1:血条blood的父物体
参数2:第(1)步转换的屏幕坐标
参数3:ui相机
参数4:输出的ui的坐标,即你想要得值!