publicclassCubeCollider:MonoBehaviour{
privateVector3screenPoint;
privateVector3offset;
//Usethisforinitialization
voidStart(){
}
//Updateiscalledonceperframe
voidUpdate(){
}
//鼠标按下
voidOnMouseDown(){
//将物体的坐标转化为屏幕坐标
screenPoint=Camera.main.WorldToScreenPoint(transform.position);
//鼠标点击位置和物体中心点的偏移
offset=transform.position-Camera.main.ScreenToViewportPoint(newVector3(Input.mousePosition.x,Input.mousePosition.y,screenPoint.z));
print("OnMouseDown");
}
//坐标系转化本地坐标,世界坐标,屏幕坐标,视口坐标
//鼠标拖动
voidOnMouseDrag(){
////获取鼠标在屏幕上移动的位置
Vector3curScreenPoint=newVector3(Input.mousePosition.x,Input.mousePosition.y,screenPoint.z);
//将获得的屏幕坐标转化为世界坐标
Vector3curPosition=Camera.main.ScreenToWorldPoint(curScreenPoint)+offset;
transform.position=curPosition;
}
//鼠标抬起
voidOnMouseUp(){
print("OnMouseUp");
}
//鼠标进入
voidOnMouseEnter(){
//改变材质颜色
gameObject.GetComponent().material.color=Color.cyan;
print("OnMouseEnter");
}
//鼠标移出
voidOnMouseExit(){
gameObject.GetComponent().material.color=Color.white;
print("OnMouseExit");
}
//鼠标停留
voidOnMouseOver(){
//颜色渐变
//gameObject.GetComponent().material.color-=Color.white*Time.deltaTime;
print("OnMouseDrag");
print("OnMouseOver");
}
//鼠标按下并抬起
voidOnMouseUpAsButton(){
print("OnMouseUpAsButton");
}
}