Unity中Monobehaviour类

Unity中Monobehaviour类_第1张图片
Paste_Image.png
  // 鼠标进入的时候
    private void OnMouseEnter()
    {
        Debug.Log("enter");
    }
    // 鼠标在有碰撞器的物体上停留
    private void OnMouseOver()
    {
        Debug.Log("over");
     }
    // 当鼠标离开碰撞器控件的时候
    private void OnMouseExit()
    {
        Debug.Log("Exit");
    }
 // 当按鼠标拖动的时候
    private void OnMouseDrag()
    {
        Debug.Log("Drag");
        GetComponent().material.color -= Color.white * Time.deltaTime;
    }
    //当点击鼠标的并释放一次的时候
    private void OnMouseUpAsButton()
    {
        Debug.Log("OnMouseUpAsButton");
    }
    // 当鼠标按键抬起的时候
    private void OnMouseUp()
    {
        Debug.Log("Up");
    }

如何拖动游戏对象

 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 局部坐标
        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;
        //坑:后面必须设置层Layer Mask, 不设置会点击所有的物体,造成始终移向摄像机
        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;
    }
}

你可能感兴趣的:(Unity中Monobehaviour类)