unity3d 如何通过Android设备上的UI检测点击

mwkjh3gx  于 2022-11-16  发布在  Android
关注(0)|答案(1)|浏览(205)

如何通过UI元素检测点击?我做了一个画布,在画布上有一个可拖动/滚动的面板,当你按下游戏对象时,它会自动打开,如果我按下游戏对象以外的任何地方,它会自动关闭。问题是,每当我试图在手指没有悬停在游戏对象上的地方滚动时,即使我试图在面板区域滚动,窗口也会关闭

I am using Raycast to detect a gameobject with a layermask and found another method which is places inside of if statement which check for clicks. Here's my code for panel animation of the up-down movement:

if (Input.GetMouseButtonDown(0))
        {
            //to prevent indicator to be clicked when using UI elements.
            //It is put after checking for button click and before creating Ray
            if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
                return;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out RaycastHit hit))
            {
                if (hit.transform.gameObject.layer == LayerMask.NameToLayer("Boutiques"))
                {
                    GetComponent<Animator>().Play("AnimUP");
                }

            }
            else {GetComponent<Animator>().Play("AnimDOWN");
            }
        }

我试图做一个可滚动的窗口,当你用手指按一个物体时,它会打开,如果你按窗口外的某个地方,衣服会放下

ncecgwcz

ncecgwcz1#

您可以在Update.查找示例脚本中检查touchPhase

public class TouchPhaseExample : MonoBehaviour
{
    public Vector2 startPos;
    public Vector2 direction;

    public Text m_Text;
    string message;

    void Update()
    {
        //Update the Text on the screen depending on current TouchPhase, and the current direction vector
        m_Text.text = "Touch : " + message + "in direction" + direction;

        // Track a single touch as a direction control.
        if (Input.touchCount > 0)
        {
            Touch touch = Input.GetTouch(0);

            // Handle finger movements based on TouchPhase
            switch (touch.phase)
            {
                //When a touch has first been detected, change the message and record the starting position
                case TouchPhase.Began:
                    // Record initial touch position.
                    startPos = touch.position;
                    message = "Begun ";
                    break;

                //Determine if the touch is a moving touch
                case TouchPhase.Moved:
                    // Determine direction by comparing the current touch position with the initial one
                    direction = touch.position - startPos;
                    message = "Moving ";
                    break;

                case TouchPhase.Ended:
                    // Report that the touch has ended when it ends
                    message = "Ending ";
                    break;
            }
        }
    }
}

相关问题