现在的位置: 首页 > 综合 > 正文

Unity3D【脚本】把一个ui物体,对准世界坐标中的物体,可以实现例如血条等

2018年08月27日 ⁄ 综合 ⁄ 共 612字 ⁄ 字号 评论关闭

 

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;
	}
	
}

 

 

 

抱歉!评论已关闭.