UGUI 3d坐标转UI坐标
public class CubePosToGUI : MonoBehaviour
{
public Canvas TheCanvas;
public GameObject CubeObj;
{
public Canvas TheCanvas;
public GameObject CubeObj;
private RectTransform canvasRectTrans;
private RectTransform textRectTrans;
private RectTransform textRectTrans;
void Start()
{
canvasRectTrans = TheCanvas.GetComponent();
textRectTrans = GetComponent();
}
{
canvasRectTrans = TheCanvas.GetComponent();
textRectTrans = GetComponent();
}
void Update()
{
Vector3 screenVec3Pos;
screenVec3Pos = Camera.main.WorldToScreenPoint (CubeObj.transform.position);
{
Vector3 screenVec3Pos;
screenVec3Pos = Camera.main.WorldToScreenPoint (CubeObj.transform.position);
Vector2 screenVec2Pos = new Vector2 (screenVec3Pos.x, screenVec3Pos.y);
Vector2 textPos;
RectTransformUtility.ScreenPointToLocalPointInRectangle (canvasRectTrans,
screenVec2Pos,
TheCanvas.worldCamera,
out textPos);
textRectTrans.anchoredPosition = textPos;
}
}
screenVec2Pos,
TheCanvas.worldCamera,
out textPos);
textRectTrans.anchoredPosition = textPos;
}
}
或者
public class CubePosToGUI : MonoBehaviour
{
public GameObject CubeObj;
{
public GameObject CubeObj;
void Update()
{
Vector3 screenVec3Pos;
screenVec3Pos = Camera.main.WorldToScreenPoint (CubeObj.transform.position);
{
Vector3 screenVec3Pos;
screenVec3Pos = Camera.main.WorldToScreenPoint (CubeObj.transform.position);
transform.position = screenVec3Pos;
}
}
}
}
转:http://blog.sina.com.cn/s/blog_47e8dc5c0102w59o.html