juicycleff / flutter-unity-view-widget

Embeddable unity game engine view for Flutter. Advance demo here https://github.com/juicycleff/flutter-unity-arkit-demo
BSD 3-Clause "New" or "Revised" License
2.13k stars 518 forks source link

Unity's OnMouseDown function not working #823

Open cancaglar opened 1 year ago

cancaglar commented 1 year ago

This function is not working when I use this flutter package image

timbotimbo commented 1 year ago

Which platform is this?

RoyalCoder88 commented 1 year ago

Hi @cancaglar ,

Don't use the method OnMouseDown() on phones, this will not work on all devices also you can get a lot of errors, for tapping interaction use in Update() --- >Input.touch

        void Update()
        {
            if (Input.touchCount > 0)
            {
                m_touch = Input.GetTouch(0);
                //logic on Tap
                print("TAP");

                if (m_touch.phase == TouchPhase.Began)
                {
                    var hit = new RaycastHit();

                    Ray ray = m_mainCamera.ScreenPointToRay(Input.GetTouch(0).position);

                    if (Physics.Raycast(ray, out hit, m_maxDistanceHit))
                    {
                        Debug.Log(hit.transform.gameObject.tag);
                        if (hit.transform.gameObject.tag == "PLAYER")
                        {
                            Debug.Log("SOME ACTION ON TAPPING ON PLAYER");
                        }
                        UnityMessageManager.Instance.SendMessageToFlutter($"action#{hit.transform.gameObject.tag}");
                    }
                }

            }
        }
cancaglar commented 1 year ago

Which platform is this?

android

cancaglar commented 1 year ago

Hi @cancaglar ,

Don't use the method OnMouseDown() on phones, this will not work on all devices also you can get a lot of errors, for tapping interaction use in Update() --- >Input.touch

        void Update()
        {
            if (Input.touchCount > 0)
            {
                m_touch = Input.GetTouch(0);
                //logic on Tap
                print("TAP");

                if (m_touch.phase == TouchPhase.Began)
                {
                    var hit = new RaycastHit();

                    Ray ray = m_mainCamera.ScreenPointToRay(Input.GetTouch(0).position);

                    if (Physics.Raycast(ray, out hit, m_maxDistanceHit))
                    {
                        Debug.Log(hit.transform.gameObject.tag);
                        if (hit.transform.gameObject.tag == "PLAYER")
                        {
                            Debug.Log("SOME ACTION ON TAPPING ON PLAYER");
                        }
                        UnityMessageManager.Instance.SendMessageToFlutter($"action#{hit.transform.gameObject.tag}");
                    }
                }

            }
        }

Yes this is exactly what I did. But this is kind of weird issue.