Example #1
0
    private void CheckInput()
    {
        if(MainCamera == null || Target == null)
            return;

        if(Input.GetKey(KeyCode.D))
        {
            m_inputState = InputState.Deletion;
        }
        else if(Status == GameStatus.Editor && Input.GetKey(KeyCode.A))
        {
            m_inputState = InputState.Addition;
        }
        else if(Input.GetKey(KeyCode.W))
        {
            m_inputState = InputState.Mark;
        }
        else
            m_inputState = InputState.None;

        if(m_clickable && Input.GetMouseButtonDown(0))
        {
            //从摄像机发出到点击坐标的射线
            Ray ray = MainCamera.ScreenPointToRay(Input.mousePosition);
            RaycastHit hitInfo;
            if(Physics.Raycast(ray, out hitInfo, 100f, 1<<8))
            {
                //划出射线,只有在scene视图中才能看到
                Debug.DrawLine(ray.origin, hitInfo.point);
                ModelCube target = hitInfo.collider.gameObject.GetComponent<ModelCube>();
                if(target != null)
                {
                    Vector3 point = target.transform.InverseTransformPoint(hitInfo.point);

                    switch(m_inputState)
                    {
                        case InputState.Mark:
                            //cube.Mark();
                            break;
                        case InputState.Deletion:
                            Target.DeleteCube(target);
                            break;
                        case InputState.Addition:
                            CubeFaceType face = ClickFace(point);
                            Target.AddCube(target, face);
                            break;
                    }//end switch
                }//end null
            }//end ray
        }//end buttondown
    }