复制 Unity 神秘的界面功能 [英] Duplicating Unity's mystic Interface power

查看:19
本文介绍了复制 Unity 神秘的界面功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Unity3D 有一个这样的界面,对于 MonoBehavior 上的任何组件,您只需执行以下操作:

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

public class LaraCroft:MonoBehaviour,IPointerDownHandler
  {
  public void OnPointerDown(PointerEventData data)
        {
        Debug.Log("With no other effort, this function is called
        for you, by the Unity engine, every time someone touches
        the glass of your iPhone or Android.");
        }

不必注册、设置代表或其他任何事情.每次有人触摸屏幕时,都会为您调用 OnPointerDown(IPointerDownHandler 中的唯一项).

You do not have to register, set a delegate or anything else. OnPointerDown (the only item in IPointerDownHandler) gets called for you every single time someone touches the screen.

太棒了!

这是我写的一个类似的界面...

Here's a similar interface I wrote ...

public interface ISingleFingerDownHandler
    {
    void OnSingleFingerDown();
    }

现在,我希望消费者能够做到这一点......

Now, I want consumers to be able to do this...

public class LaraCroft:MonoBehaviour,ISingleFingerDownHandler
    {
    public void OnSingleFingerDown(PointerEventData data)
        {
        Debug.Log("this will get called every time
        the screen is touched...");
        }

简单回顾一下,使用 Unity 的界面,该函数会自动调用,无需进一步 - 消费者不必注册或进行任何其他操作.

Just to recap, using Unity's interface, the function gets called automatically with no further effort - the consumer does not have to register or anything else.

遗憾的是,我只能这样实现:

Sadly, I can achieve that only like this:

我写了一个守护进程"..

I write a "daemon" ..

public class ISingleFingerDaemon:MonoBehaviour
    {
    private ISingleFingerDownHandler needsUs = null;
    // of course that would be a List,
    // just one shown for simplicity in this example code

    void Awake()
        {
        needsUs = GetComponent(typeof(ISingleFingerDownHandler))
                                       as ISingleFingerDownHandler;
        // of course, this could search the whole scene,
        // just the local gameobject shown here for simplicity
        }

    ... when something happens ...

        if (needsUs != null) needsUs.OnSingleFingerDown(data);


    }

我让守护进程在某处运行.

And I get that daemon running somewhere.

如果您不是 Unity 用户 - 它所做的就是四处寻找并找到任何 ISingleFingerDownHandler 消费者,保留他们的列表,然后适当地调用 OnPointerDown 根据需要.这工作正常但是

If you're not a Unity user - what it does is looks around for and finds any of the ISingleFingerDownHandler consumers, keeps a list of them, and then appropriately calls OnPointerDown as needed. This works fine BUT

  • 消费者程序员必须记住将守护进程放在某处"并让它运行等.

  • the consumer-programmer has to remember to "put the daemon somewhere" and get it running etc.

每当你做这样的事情(在 Unity 或其他地方)、重新效率、放置等时,都会有明显的反优雅

there are obvious anti-elegancies whenever you do something like this (in Unity or elsewhere), re efficiency, placement, etc etc

• 如果消费者在守护进程没有搜索消费者的时候出现,这种方法当然会失败(Unity 的魔法接口不会遇到这个问题——他们有更多的魔法来处理)

• this approach fails of course if a consumer comes in to existence at a time when the daemon is not searching for them (Unity's magic interfaces don't suffer this problem - they have more magic to deal with that)

(PS,我知道如何编写放置守护程序等的自动助手:请不要那样回复,谢谢!)

(PS, I know how to write an automatic helper that places the daemon and so on: please do not reply in that vein, thanks!)

确实,很明显 Unity 的开发人员有一些系统在幕后运行,这一切都做得很好,因为他们的"接口完全能够调用所有需要的调用,而不管是否即时创建项目等.

Indeed, obviously the developers at Unity have some system going on behind the scenes, which does all that beautifully because "their" interfaces are perfectly able to call all the needed calls, regardless of even items being created on the fly etc.

最好的解决方案是什么?我是否需要一个守护进程?也许必须注册?

What's the best solution? Am I stuck with needing a daemon? And perhaps having to register?

(它肯定会很糟糕 - 实际上通常不能在典型的 Unity 项目中使用 - 只是让它成为一个继承自的类;这种类型的设施自然是一个接口.)

(It would surely suck - indeed generally not be usable in typical Unity projects - to just make it a class to inherit from; that type of facility is naturally an interface.)

总结一下,Unity 有这个:

So to recap, Unity has this:

public class LaraCroft:MonoBehaviour,IPointerDownHandler

当然有一种方法可以让我替换、扩展...

Surely there's a way for me to make a replacement, extension, for that...

public class LaraCroft:MonoBehaviour,ISuperiorPointerDownHandler

然后可以以相同的方式使用/共享该界面的神奇品质?我可以做得很好,但我只能制作一个守护进程.

which can then be used the same way / which shares the magic qualities of that interface? I can do it fine, but only my making a daemon.

Unity 中ISingleFingerHandler"IPinchHandler"和类似概念的完整解决方案在这里:https://stackoverflow.com/a/40591301/294884

Full solution for "ISingleFingerHandler" "IPinchHandler" and similar concepts in Unity is here: https://stackoverflow.com/a/40591301/294884

推荐答案

我讨厌回答我自己的问题,但这里的答案是:

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

但是,非常值得注意的是

But then, it's very much worth noting that

要理解的最后一个绝对关键点是:

The final absolutely critical point to understand is that:

Unity 的 StandAloneInputModule 和 IPointerDownHandler 系列 - 非常棒.但是你不能正确继承它们.

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

事实是,您只需要从 IPointerDownHandler 横向继承即可.这就是全部.

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

所以实际答案是 (A) 你有这个

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

和(B)你确实必须把它放在一个游戏对象上(它是一个守护进程),然后(C)最终处理捏合等问题非常容易.

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

就是这样!

PinchInputModule 的完整生产代码 ...

Full production code for PinchInputModule ...

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

...它确实从(使用")IPointerDownHandler 家族中横向继承.

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

这篇关于复制 Unity 神秘的界面功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆