Duplicating Unity's mystic Interface power

后端 未结 3 609
刺人心
刺人心 2021-01-06 04:07

Unity3D has an interface like this, for any Component on a MonoBehavior you just do this:

public class LaraCroft:MonoBehaviour,IPointerDownHandler
  {
  publ         


        
3条回答
  •  执笔经年
    2021-01-06 04:53

    I hate to answer my own questions, but the answer here is really:

    You cannot. You do have to add a daemon.

    But then, it's very much worth noting that

    Indeed, Unity add a daemon - they just hide it a little.

    The final absolutely critical point to understand is that:

    Unity screwed-up: you cannot in fact inherit from their lovely StandAloneInputModule. This is a big mistake.

    Unity's StandAloneInputModule and IPointerDownHandler family - are brilliant. But you can't inherit from them properly.

    The fact is, you just have to inherit sideways from IPointerDownHandler. That's all there is to it.

    The fact is you have to make your own daemon ("as if" it inherits from StandAloneInputModule) which actually just goes sideways from IPointerDownHandler family.

    So the actual answer is (A) you have this

    public interface ISingleFingerHandler
        {
        void OnSingleFingerDown (Vector2 position);
        void OnSingleFingerUp (Vector2 position);
        void OnSingleFingerDrag (Vector2 delta);
        }
    
    public class SingleFingerInputModule:MonoBehaviour,
                    IPointerDownHandler,IPointerUpHandler,IDragHandler
    

    and (B) you do have to put that on a game object (it's a daemon), and then (C) it's just stupidly easy to finally handle pinches, etc.

    public class YourFingerClass:MonoBehaviour, IPinchHandler
        {
        public void OnPinchZoom (float delta)
            {
            _processPinch(delta);
            }
    

    That's it!

    Full production code for PinchInputModule ...

    https://stackoverflow.com/a/40591301/294884

    ...which indeed inherits sideways from ("uses") IPointerDownHandler family.

提交回复
热议问题