未调用EventSystem OnPointerXXX函数 [英] EventSystem OnPointerXXX functions not getting called
问题描述
我有一架包含约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 .转到GameObject-> UI ---> EventSystem.
1.Go to GameObject-->UI--->EventSystem.
将创建一个名为"EventSystem" 的GameObject,并将附加适当的脚本,例如EventSystem
,StandAloneInputModule
.
A GameObject named "EventSystem" will be created and will proper scripts such as EventSystem
, StandAloneInputModule
attached to it.
2 .必须将PlaneBehaviour
脚本附加到GameObject,而对撞机 not 不能附加到空的GameObject.它会检测到其附加到的对象上的点击.
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 .重新调整您要检测点击次数的GameObject的大小.我看到这样做有时可以解决问题.然后,如果可以的话,您必须将相机移回去.
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屋!