预设体和操作游戏物体的基本函数
using UnityEngine;
using System.Collections;
public class yusheti : MonoBehaviour {
void Start () {
}
public GameObject a;//预设体。
void Update () {
if(Input.GetKeyDown(KeyCode.R))
{
Vector3 pos = new Vector3();
//pos.y = 5.6f;//高度为定值 5.6。
pos.y = Random.Range(0f, 100f);
pos.x = Random.Range(150, 320);// x 位置在 150 到 320 之间的整数。
pos.z = Random.Range(150f, 320f);// y 位置在 150 到320 之间的浮点数。
Instantiate(a, pos,Quaternion.identity);//第一个参数为 预设体, 第二个为 坐标,第三个为旋转度,Quaternion.Identity 为不旋转。
//Instantiate();//会返回一个 Object 的值。
//float b=Random.Range(0f,360f);//产生 0 到 360 之间的浮点数 表示随机度数。
//Quaternion.AngleAxis(b,Vector3.up);//表示 绕 Vector3.up 轴 旋转 b 度。
}
if (Input.GetKeyDown(KeyCode.E))
{
Vector3 pos = new Vector3();
pos.y = Random.Range(0f, 100f);//高度为 0 到 100 的浮点随机值
pos.x = Random.Range(150, 320);// x 位置在 150 到 320 之间的整数。
pos.z = Random.Range(150f, 320f);// y 位置在 150 到320 之间的浮点数。
float b = Random.Range(0f, 360f);//产生 0 到 360 之间的浮点数 表示随机度数。
//GameObject p = Instantiate(a, pos, Quaternion.AngleAxis(b, Vector3.up)) as GameObject;// as 类型转化。把Instantiate()返回的 Object 转化为 GameObject。
//p.AddComponent<yusheti>();//给对象增加 一个组件。
//p.AddComponent<Light>();//添加灯光组件。
}
}
}
鼠标事件
using UnityEngine;
using System.Collections;
public class shubiao : MonoBehaviour {
void OnMouseDown()//点在游戏对象身上时有效。与Input不同。
{
print("鼠标点击");
}
void OnMouseUp()//鼠标松开,无论在不在游戏对象身上,但是点的时候要在游戏对象身上。
{
print("鼠标松开");
}
void OnMouseDrag()//鼠标没松开时候。可以不在游戏对象身上,但是要先点在游戏对象身上。
{
print("鼠标拖拽");
}
void OnMouseEnter()
{
print("鼠标移动到游戏对象身上");
}
void OnMouseExit()
{
print("鼠标移出对象时候");
}
void OnMouseOver()
{
print("鼠标持续停留在游戏对象身上");
}
void OnMouseUpAsButton()//在游戏对象身上松开,就像点击按钮。
{
print("AsButton");
}
}