Why "void IDragHandler.OnDrag()" in DragItem?

The title is possibly confusing but I don’t know how to phrase it better.
My question is, DragItem implements three interfaces that come from the EventSystem in Unity. I understood that much.
So to implement them we must provide functionality to their methods. That is clear to me too.
However how I expect to do that is to implement IDragHandler, for example, with a method called OnDrag() and that’s all.
The scipt provided with the lesson implements it with a method called void IDragHandler.OnDrag().
Why do you have to include the interface name in the method name like that?
It seems to be an event, is it because of that? What would happen if I implement OnDrag() “alone”, without prefacing it with the name of the interface?

If you don’t make the method public, then the script will fail to compile as you need a reachable method to satisfy the requirement of IDragHandler.

Prefacing the method with an IDragHandler prefix allows us to keep the method private, so it should only be called through the IDragHandler interface.

You can do it either way, and there are sound arguments on both sides of the debate on whether interface methods should be public or qualified. Public methods can be called by any class with access to the class in general. Qualified private methods can only be called by something specifically looking for that interface. The strictest OOP rules say it should stay private when possible, but this also incurs some extra overhead in the stack.

1 Like

Thanks! I didn’t know that, good to know.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms