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

查看:158
本文介绍了复制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.

"ISingleFingerHandler","IPinchHandler"和Unity中类似概念的完整解决方案在这里: 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天全站免登陆