Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
316 views
in Technique[技术] by (71.8m points)

c# - EventSystem OnPointerXXX functions not getting called

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;
    }
}

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).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.Go to GameObject-->UI--->EventSystem.

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

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.If you are using a collider that ends with "2D" in its name then Physics2DRaycaster must be used instead of 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.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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...