Unity鼠标点击Collider

void OnGUI()
    {
        if (Event.current != null && Event.current.type == EventType.mouseDown)
        {
            if (Input.GetMouseButton(0))
            {
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                RaycastHit hit;
                if (Physics.Raycast(ray, out hit))
                {
                    GameObject selectedGmObj = hit.collider.gameObject; //获得点击的物体
                    if(selectedGmObj.name.Contains("Face"))
                    {
                        Debug.Log("Face");
                    }
                    else if (selectedGmObj.name.Contains("Hair"))
                    {
                        Debug.Log("Hair");
                    }
                    else if (selectedGmObj.name.Contains("Clothes"))
                    {
                        Debug.Log("Clothes");
                    }
                    else if (selectedGmObj.name.Contains("Accessory"))
                    {
                        Debug.Log("Accessory");
                    }
                }
            }
        }
    }

 

posted @ 2017-09-13 12:24  酷熊  阅读(1674)  评论(0编辑  收藏  举报