using UnityEngine; using System.Collections; public class NewBehaviourScript : MonoBehaviour { public GameObject TargetObject; //世界坐标中,需要对准的物体 public Camera worldcamera; //世界摄像机 public Camera guiCamera; //ui摄像机 //public GameObject obj; // Use this for initialization void Start () { // worldcamera = NGUITools.FindCameraForLayer (TargetObject.layer); guiCamera = NGUITools.FindCameraForLayer (this.gameObject.layer); } void Update () { } void LateUpdate() { //世界坐标到ui坐标的转换 Vector3 pos = worldcamera.WorldToScreenPoint (TargetObject.transform.position); pos = guiCamera.ScreenToWorldPoint (pos); pos.z = 0; //改变ui上坐标的时候,C#中不能直接赋值x、y、z的值,需要给一个Vector3 transform.position = pos; } }