[code=javascript]
//比较实用的一段代码,单击鼠标并在单击位置创建物体的功能
var distanceFromCamera : float;
var go:GameObject;
private var X : int;
private var Y : int;
function Update(){
X = Input.mousePosition.x;
Y = Input.mousePosition.y;
if(Input.GetMouseButtonDown(0)){
var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.AddComponent(Rigidbody);
cube.transform.position = camera.main.ScreenToWorldPoint(Vector3(X,Y,distanceFromCamera));
}
}
//这段代码是动态载入库中物体
var distanceFromCamera : float;
var cube : Transform;
private var X : int;
private var Y : int;
private var clickP:Vector3;
function Update(){
X = Input.mousePosition.x;
Y = Input.mousePosition.y;
clickP= camera.main.ScreenToWorldPoint(Vector3(X,Y,distanceFromCamera));
if(Input.GetMouseButtonDown(0)){
//此处的var不可省略,省略后虽效果不变,但似乎有其它问题 、
//static function Instantiate (original : Object, position : Vector3, rotation : Quaternion) : Object
var cube = Instantiate(cube, clickP, Quaternion.identity);
}
}
[/code]
|