Paste_Image.png
// 鼠標(biāo)進(jìn)入的時(shí)候
private void OnMouseEnter()
{
Debug.Log("enter");
}
// 鼠標(biāo)在有碰撞器的物體上停留
private void OnMouseOver()
{
Debug.Log("over");
}
// 當(dāng)鼠標(biāo)離開(kāi)碰撞器控件的時(shí)候
private void OnMouseExit()
{
Debug.Log("Exit");
}
// 當(dāng)按鼠標(biāo)拖動(dòng)的時(shí)候
private void OnMouseDrag()
{
Debug.Log("Drag");
GetComponent<Renderer>().material.color -= Color.white * Time.deltaTime;
}
//當(dāng)點(diǎn)擊鼠標(biāo)的并釋放一次的時(shí)候
private void OnMouseUpAsButton()
{
Debug.Log("OnMouseUpAsButton");
}
// 當(dāng)鼠標(biāo)按鍵抬起的時(shí)候
private void OnMouseUp()
{
Debug.Log("Up");
}
如何拖動(dòng)游戲?qū)ο?/h3>
void OnMouseEnter()
{
this.transform.localScale = new Vector3(1.3f, 1.3f, 1.3f);
}
void OnMouseExit()
{
this.transform.localPosition = new Vector3(1.0f, 1.0f, 1.0f);
}
void OnMouseOver()
{
//Space.Self 局部坐標(biāo)
this.transform.Rotate(Vector3.up, 45 * Time.deltaTime, Space.Self);
}
void OnMouseDrag()
{
//MoveObject();
MoveObject_FiexdDepth();
}
private void MoveObject()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
//坑:后面必須設(shè)置層Layer Mask, 不設(shè)置會(huì)點(diǎn)擊所有的物體床估,造成始終移向攝像機(jī)
if (Physics.Raycast(ray, out hit, 1000f, 1))
{
this.transform.position = hit.point;
Debug.DrawLine(ray.origin, hit.point, Color.red);
}
}
void MoveObject_FiexdDepth()
{
Vector3 mouseScreen = Input.mousePosition;
mouseScreen.z = 10f;
Vector3 mouseWorld = Camera.main.ScreenToWorldPoint(mouseScreen);
this.transform.position = mouseWorld;
}
}
void OnMouseEnter()
{
this.transform.localScale = new Vector3(1.3f, 1.3f, 1.3f);
}
void OnMouseExit()
{
this.transform.localPosition = new Vector3(1.0f, 1.0f, 1.0f);
}
void OnMouseOver()
{
//Space.Self 局部坐標(biāo)
this.transform.Rotate(Vector3.up, 45 * Time.deltaTime, Space.Self);
}
void OnMouseDrag()
{
//MoveObject();
MoveObject_FiexdDepth();
}
private void MoveObject()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
//坑:后面必須設(shè)置層Layer Mask, 不設(shè)置會(huì)點(diǎn)擊所有的物體床估,造成始終移向攝像機(jī)
if (Physics.Raycast(ray, out hit, 1000f, 1))
{
this.transform.position = hit.point;
Debug.DrawLine(ray.origin, hit.point, Color.red);
}
}
void MoveObject_FiexdDepth()
{
Vector3 mouseScreen = Input.mousePosition;
mouseScreen.z = 10f;
Vector3 mouseWorld = Camera.main.ScreenToWorldPoint(mouseScreen);
this.transform.position = mouseWorld;
}
}