在Unity中,有如下功能函数可直接使用(直接将脚本挂载在游戏对象上即可使用):
1. void OnMouseOver() //鼠标移动至游戏对象上事件
{
this.GetComponent<MeshRenderer>().material.color = Color.red;
transform.Rotate(0,150*Time.DeltaTime,0,Space.Self) //绕自身坐标Y轴旋转
}2. void OnMouseExit() //鼠标从该游戏物体上移开事件
{
this.GetComponent<MeshRenderer>().material.color = Color.white;
}
3. void OnMouseDown() //鼠标在该游戏物体上左键按下
{
Debug.Log("Mouse Down");
}
//当游戏对象添加了Charator Controller组件后,以下函数被调用,可用于检测游戏对象是否发生碰撞,并在if语句在判断碰撞物体的相关属性,如tag,如为真,再执行相关操作.
4. private void OnControllerColliderHit(ControllerColliderHit hit)
{
if (hit.gameObject.tag=="tagDoor")
{
player.GetComponent<Animation>().Play("Default Take");
theDoor.GetComponent<AudioSource>().clip = doorOpen;
theDoor.GetComponent<AudioSource>().Play();
}
}
5. gameObject.AddComponent<Rigidbody>();
可在代码中给对象动态添加属性、脚本。