unity3d:物体点击与拖动互不影响

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/luoyikun/article/details/82888815
    private bool bInTouch = false;
    private float ClickAfter = 0.0f;
    GameObject m_curClickBox;
    private Vector3 mousePosLast = Vector3.zero;
    private bool Dragged = false;

    private bool bTemporarySelect = false;
 public void Update()
    {
        //if (Input.GetMouseButton(0))
        //{

        //    if (EventSystem.current.IsPointerOverGameObject())
        //    {
        //        //Debug.Log("left-click over a GUI element!");
        //        return;
        //    }

        //    if (!bInTouch)
        //    {
        //        bInTouch = true;

        //    }
        //}
        //else
        //{
        //    if (bInTouch)
        //    {
        //        bInTouch = false;

        //        EventManager.Instance.DispatchEvent(Common.EventStr.DeleteBtHomeHeadUi);
        //    }
        //}

        if (Input.GetMouseButton(0))
        {

            if (Application.isMobilePlatform && Input.touchCount > 0)
            {
                if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
                    return;
            }
            else if (EventSystem.current.IsPointerOverGameObject())
            {
                return;
            }

            if (bInTouch == false)
            {

                bInTouch = true;
                ClickAfter = 0.0f;
                mousePosLast = Input.mousePosition;
                bTemporarySelect = false;
                Dragged = false;

                //Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                //RaycastHit hit;
                //if (Physics.Raycast(ray, out hit) && (hit.collider.gameObject.tag == "HtBox"))
                //{
                //    m_curClickBox = hit.collider.gameObject;
                //}
                //else
                //{
                //    m_curClickBox = null;
                //}
            }
            else
            {
                if (Vector3.Distance(Input.mousePosition, mousePosLast) > 0.01f)
                {
                    if (!Dragged)
                    {
                        Dragged = true;
                    }
                }
                else
                {
                    if (!Dragged)
                    {
                        ClickAfter += Time.deltaTime;
                        if (!bTemporarySelect && (ClickAfter > 0.5f))
                        {
                            bTemporarySelect = true;
                            //Debug.Log ("Update2 buildingSelected:"+((buildingSelected != null) ? buildingSelected.name : "none"));
                            PickOneBox();
                        }
                    }
                }
            }
        }
        else
        {
            if (bInTouch)
            {
                bInTouch = false;

                if (Dragged)
                {

                }
                else {
                    if (bTemporarySelect)
                    {
                    }
                    else {
                        PickOneBox();
                    }
                }
            }
        }
    }
    void PickOneBox()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit))
        {
            if (hit.collider.gameObject.tag == "HtBox")
            {
                hit.collider.gameObject.GetComponent<BoxCtrl>().OnPick();
              
            }   
        }
    }

猜你喜欢

转载自blog.csdn.net/luoyikun/article/details/82888815
今日推荐