Пример #1
0
    // Update is called once per frame
    void Update()
    {
        Vector2 mouseWorldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);

        RaycastHit2D[] hits = Physics2D.RaycastAll(mouseWorldPosition, Vector2.zero);


        if (m_activeListener != null)
        {
            m_activeListener.MouseMove(mouseWorldPosition);

            if (Input.GetMouseButtonDown(0))
            {
                m_activeListener.MouseButtonDown(0);
            }
            else if (Input.GetMouseButtonDown(1))
            {
                m_activeListener.MouseButtonDown(1);
            }
            else if (Input.GetMouseButtonUp(0))
            {
                m_activeListener = m_activeListener.MouseButtonUp(0) ? m_activeListener : null;
            }
            else if (Input.GetMouseButtonUp(1))
            {
                m_activeListener.MouseButtonUp(1);
            }
        }
        else
        {
            foreach (var hit in hits)
            {
                InputListener listener = hit.collider.gameObject.GetComponent <InputListener>();

                if (listener != null)
                {
                    if (Input.GetMouseButtonDown(0))
                    {
                        m_activeListener = listener.MouseButtonDown(0) ? listener : null;
                    }
                    if (Input.GetMouseButtonDown(1))
                    {
                        m_activeListener = listener.MouseButtonDown(1) ? listener : null;
                    }
                    if (Input.GetMouseButtonUp(0))
                    {
                        m_activeListener = listener.MouseButtonUp(0) ? listener : null;
                    }
                    if (Input.GetMouseButtonUp(1))
                    {
                        m_activeListener = listener.MouseButtonUp(1) ? listener : null;
                    }

                    if (m_activeListener != null)
                    {
                        break;
                    }
                }
            }
        }
    }