【问题标题】:How to get pointer down and up events for camera space?如何获取相机空间的指针向下和向上事件?
【发布时间】:2019-09-05 12:34:36
【问题描述】:

当用户触摸屏幕的任何一点时,我需要相机接收 pointer down|up 事件。我尝试使用脚本将 BoxCollider2D 附加到相机:

public class PlayerController : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
    public void OnPointerUp(PointerEventData eventData)
    {
        // Some logic
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        // Some logic
    }
}

但它不起作用。

如何制作?

P.S.不管是不是点击GameObject,没关系,最重要的是用户点击了屏幕。 但我需要忽略对 UI 元素的点击。
P.P.S我使用 Unity3D 2018.3.12f1

【问题讨论】:

  • 您可以使用 Input.GetButtonDown 来实现这一点,但它不会忽略单击 UI 元素。你可以做的是创建一个与屏幕大小相同的 UI 标签并检测点击它。如果你把这个标签放在所有其他 UI 元素下面,它就会这样做。
  • 先将 Physic2DRaycaster 组件附加到相机!

标签: unity3d unity3d-gui


【解决方案1】:

简而言之,我不确定这是否是最佳解决方案,但使用其他一些主题和 Pierre Baret 的建议,我想出了这个脚本:

[RequireComponent(typeof(CatController))]
public class PlayerController : MonoBehaviour
{
    public float delay = 0.25f;

    private CatController _cat;
    private short _clicked = 0;
    private float _pressingTime = 0f;
    private bool _isPressed = false;

    private void Awake()
    {
        _cat = GetComponent<CatController>();
    }

    private void Update()
    {
        if (_pressingTime > 0)
        {
            _pressingTime -= Time.deltaTime;
        }

        if (Input.GetMouseButtonDown(0))
        {
            if (!IsOverUI())
            {
                OnPointerPressedDown();
            }
        }
        else if (Input.GetMouseButtonUp(0))
        {
            _isPressed = false;
            OnPointerPressedUp();
        }
        else if (_isPressed && _pressingTime <= 0)
        {
            OnPointerPressing();
        }
    }

    private void OnPointerPressedDown()
    {
        _isPressed = true;

        if (_clicked == 1)
        {
            if (_pressingTime > 0f)
            {
                OnDoubleClick();
                _clicked = 0;
            }
            else
            {
                _pressingTime = delay;
            }
        }
        else
        {
            _clicked = 1;
            _pressingTime = delay;
        }
    }

    private void OnPointerPressedUp()
    {
        _cat.Stop();
    }

    private void OnDoubleClick()
    {
        _cat.Jump();
    }

    private void OnPointerPressing()
    {
        var clickPos = Camera.main.ScreenToViewportPoint(Input.mousePosition);
        var direction = clickPos.x < 0.5 ? CatController.Direction.Left : CatController.Direction.Right;
        _cat.Walk(direction);
    }

    private static bool IsOverUI()
    {
        var pointerData = new PointerEventData(EventSystem.current)
        {
            position = Input.mousePosition
        };
        var results = new List<RaycastResult>();
        EventSystem.current.RaycastAll(pointerData, results);

        if (results.Count > 0)
        {
            for (int i = 0; i < results.Count; i++)
            {
                if (results[i].gameObject.GetComponent<CanvasRenderer>())
                {
                    return true;
                }
            }
        }

        return false;
    }
}

也许有人会寻找相同的控制器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-27
    • 1970-01-01
    • 1970-01-01
    • 2012-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-27
    相关资源
    最近更新 更多