未调用 EventSystem OnPointerXXX 函数 [英] EventSystem OnPointerXXX functions not getting called

查看:20
本文介绍了未调用 EventSystem OnPointerXXX 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含大约 200 个图块的平面,我希望能够检测到玩家点击了哪个图块.每个瓷砖都有一个盒子对撞机.我还在我附加了 EventSystem 和以下脚本的场景中创建了一个空的游戏对象:

I have a plane that contains around 200 tiles, and I'd like to be able to detect which tile has been clicked by the player. Each tile has a box collider attached. I've also created an empty game object in the scene to which I attached EventSystem and the below script:

public class PlaneBehaviour : MonoBehaviour, IPointerDownHandler {
    public GameObject ClickSymbol;

    public void Start() {
        var physicsRaycaster = FindObjectOfType<PhysicsRaycaster>();

        if (physicsRaycaster == null) {
            Camera.main.gameObject.AddComponent<PhysicsRaycaster>();
        }
    }

    public void OnPointerDown(PointerEventData eventData) {
        var o = eventData.pointerCurrentRaycast.gameObject;
    }
}

现在,当用户点击任何地方时,什么都没有发生 - 我在 OnPointerDown 方法中放置了一个断点,但它没有被击中(以及 Start 中的代码)方法已运行 - 我还通过在其中放置断点来验证这一点).

Now, when user clicks anywhere, nothing happens - I've put a breakpoint inside the OnPointerDown method, but it's not getting hit (and the code inside the Start method is run - I've also verified that by putting a breakpoint in there).

推荐答案

导致回调函数不被调用的原因有很多.在尝试这些之前,将 Debug.Log 放在 OnPointerDown 函数中以确保它被调用:

There are many things that could cause the callback function not being called. Before trying these, put Debug.Log inside the OnPointerDown function to make sure that it's being called:

1.转到游戏对象-->UI--->事件系统.

1.Go to GameObject-->UI--->EventSystem.

一个名为 "EventSystem" 的游戏对象将被创建,并将适当的脚本,例如 EventSystemStandAloneInputModule 附加到它上面.

A GameObject named "EventSystem" will be created and will proper scripts such as EventSystem, StandAloneInputModule attached to it.

2.PlaneBehaviour 脚本必须附加到游戏对象,而碰撞器不能附加到空游戏对象.它检测点击它所附加的对象.

2.The PlaneBehaviour script must be attached to the GameObject with the Collider not to an empty GameObject. It detects click on the Objects it is attached to.

3.如果您使用的对撞机名称以2D"结尾,则必须使用 Physics2DRaycaster 而不是 PhysicsRaycaster.

3.If you are using a collider that ends with "2D" in its name then Physics2DRaycaster must be used instead of PhysicsRaycaster.

4.您是否使用了两个以上的摄像头?如果是这样,手动将 PhysicsRaycaster 附加到当前显示该对象的正确相机.

4.Are you using more than two cameras? If so manually attach PhysicsRaycaster to the correct Camera that is currently showing that Object.

5.重新调整您想要检测点击的游戏对象的大小以使其更大.我见过这样做有时可以解决问题.如果可行,您必须将相机向后移动.

5.Re-size the GameObjects you want to detect clicks on to be bigger. I've seen doing this solve the problems sometimes. You then have to move the camera back if this works.

这篇关于未调用 EventSystem OnPointerXXX 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆