Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Understanding Interfaces. IPointerDownHandler

Discussion in 'Scripting' started by CatchFires, Apr 2, 2021.

  1. CatchFires

    CatchFires

    Joined:
    Jul 19, 2020
    Posts:
    38
    Hi, I'm trying to understand how IPointerDownHandler works as an interface

    When I make an interface i just have the Method name with no implementation

    So how does the interface detect the mouse clicks?

    This might be a dumb question, I just can't seem to find it on google
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    It's a contract between you and Unity's EventSystem.

    If you implement that contract on a given GameObject, then it will get calls when certain things happen, in this case a mouse click going down.

    The docs are unfortunately vague on what constitutes "on a game object" but AFAIK it is to do with the whole way a Graphic Raycaster casts a click into the scene to see what it hits.

    If it hits your GameObject then Unity will look for implementations of the IPointerDownHandler interface on that GameObject and if they exist, it will call the implementations in your script.

    Using Interfaces in Unity3D:

    https://forum.unity.com/threads/how...rereceiver-error-message.920801/#post-6028457

    https://forum.unity.com/threads/manager-classes.965609/#post-6286820

    Check Youtube for other tutorials about interfaces and working in Unity3D. It's a pretty powerful combination.

    PS - in some other languages they are called "protocols"
     
  3. CatchFires

    CatchFires

    Joined:
    Jul 19, 2020
    Posts:
    38

    Sorry, I may have not explained myself. I've used interfaces before, but what I don't understand is how the IPointerDownHandler interface detects the mouse down? I understood interfaces to use blank/empty methods that could then be called by anything that uses it.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    It doesn't.

    It means "I want to know about mouse pointer downs"

    Over in Graphic Raycaster it does all that detection, and when it sees a mouse go down, it calls you IF you implement that interface.
     
    CatchFires likes this.
  5. CatchFires

    CatchFires

    Joined:
    Jul 19, 2020
    Posts:
    38

    Ohh, I see, dur, it just calls anything that uses the interface method. Bloody hell, sometimes ya brain just goes on a tangent ay. Lmao, thanks man
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Yeah you are basically saying "I am the kinda thing that wants to know about mouse going down."

    Interfaces can be used in all sorts of directions: you could look for them, you could implement them and someone else could look for anyone implementing them, etc.

    The coolest direct application in Unity is that interfaces work straight-up with
    GetComponent<T>();
    , which means if you are a bullet that hits an enemy, you can do a GetComponent<IDamageable>() and call its TakeDamage() method, for instance.