Unity 笔记鼠标点击事件

//鼠标点击事件
        //点击的是 UI
         if (EventSystem.current.IsPointerOverGameObject())
         {

             Debug.Log("Clicked on the UI");
             return;

         }
         else
         {
             Vector2 orgPos = cam.ScreenToWorldPoint(Input.mousePosition);

             RaycastHit2D[] hitArr = Physics2D.RaycastAll(orgPos, Vector2.zero);
             if (hitArr.Length > 0)
             {
                 string m_tag = hitArr[0].collider.gameObject.tag;

                 switch (m_tag)
                 {
                     case "Ground":
                         mainTamer.m_TargetPos = hitArr[0].point;
                         break;
                     case "monster":
                         hitArr[0].collider.GetComponent<DigimonObj>().guajiuan[2].SetActive(true);
                         break;
                     case "Player":
                         if (hitArr[0].collider.GetComponent<TamerSet>() == mainTamer)
                         {
                             Debug.Log("这是当前玩家");
                         }
                         break;
                     case "otherplayer":
                         break;
                     default:
                         break;
                 }

             }


         }

    //获取UI 控件
    public GameObject ClickObject()
    {
        PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
        eventDataCurrentPosition.position = new Vector2
            (
#if UNITY_EDITOR
Input.mousePosition.x, Input.mousePosition.y
#elif UNITY_ANDROID || UNITY_IOS
           Input.touchCount > 0 ? Input.GetTouch(0).position.x : 0, Input.touchCount > 0 ? Input.GetTouch(0).position.y : 0
#endif
);
        List<RaycastResult> results = new List<RaycastResult>();
        EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
        if (results.Count > 0)
        {
            return results[0].gameObject;
        }
        else
        {
            return null;
        }
    }
相关推荐
©️2020 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页