进行“直接"交易新的Unity网络中的RPC样式调用? [英] Making "direct" RPC style calls in new Unity networking?

查看:113
本文介绍了进行“直接"交易新的Unity网络中的RPC样式调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说你有

使用新" Unity网络,您必须在某处具有(据我所知).它将生成可笑的玩家"对象.

Using "new" Unity networking, you must have (as far as I know) a NetworkManager somewhere. It will spawn the ridiculous "player" object.

荒谬的玩家"对象必须具有从NetworkBehaviour继承的类.

The ridiculous "player" object must have a class which inherits from NetworkBehaviour.

大多数团队将此类称为"Comms"或"AbstractPlayer"或"Junction".

Most teams call that class something like "Comms" or "AbstractPlayer" or "Junction".

public class Comms : NetworkBehaviour { }

因此,每个命令(如跳转")都必须由该"Comms"类介导.

So, every command (like "Jump") has to be mediated by that "Comms" class.

对于每个命令(例如跳转"),在Comms类中需要一对函数:

For each command (like "Jump") you need a pair of functions in the Comms class:

public class Comms : NetworkBehaviour {

    // for every command (say, "Jump") you need THIS PAIR of functions:

    public void CommandJump() {
        Debug.Log("this is comms - client, do a Jump");
        CmdJump();
    }

    [Command]
    void CmdJump() {
        Debug.Log("this is comms - server. a Jump arrived");
        // simply pass on that command to the correct script on a server...
        s = Object.FindObjectOfType<Some Class On The Server>();
        s.ClientJumped();
    }
}

所以!

有了新的Unity网络,每次您想在客户端进行

With new Unity networking, in the client side, every time you want to

send to server Jump()

实际上,您必须通过抽象播放器上的"comms"类向其发送.

you in fact have to send it VIA the "comms" class on the abstract player.

public class Jumping: MonoBehavior...

    // here in the client side ... we want to send "Jump"
    Comms comms = Object.FindObjectOfType<Comms>();
    comms.CommandClick1();

(注意-显然,您会在其中缓存"comms".这只是一个示例.)

此外:非常重要:这只是一个概述示例.

Aside: extremely important: that is just a outline example.

在 练习您必须这样做:

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

所以!

事情是这样的:

在旧" Unity网络中,任何旧客户端脚本 都可以直接进行网络调用 (示例中为"CmdJump").您只需要一个指向NetworkView的链接即可.

in "old" Unity networking, any old client script could just directly make a network call ("CmdJump" in the example). All you needed was a link to a NetworkView.

现在,您必须拥有这个新的介于两者之间"的类,在示例中为"Comms".

Now you have to have this new "in-between" class, "Comms" in the example.

我想了解,

有没有办法让客户端上的普通脚本(如Jumping-MonoBehavior)直接调用Command?

is there a way for a normal script on the client side (like Jumping - a MonoBehavior) to directly call a Command?

据我所知,只有NetworkBehaviour派生类可以做到这一点.在旧的网络中,任何班级都可以拨打电话.

As far as I know, only NetworkBehaviour derived classes can do that. In the old networking, any class could make such a call.

据我所知,在新" Unity网络中,必须使每个呼叫都通过NetworkBehaviour派生类进行传递,如此处的示例所示.

In "new" Unity networking, as far as I know you must you pass every call through a NetworkBehaviour derived class, as in my example here.

如您所见,如果我缺少某些东西",而Jumping之类的类可以直接做到这一点,那么我的代码就很麻烦:)

As you can see, if I am "missing something," and the classes such as Jumping can just do it directly, then my code is heinous :)

您可以直接从MonoBehavior中调用Command吗?

Can you call Commands directly from in a MonoBehavior?

我真的完全错了,您必须拥有可笑的自动生成的玩家"对象,依此类推吗?也许还有另一种方式来调用Command.

Am I in fact completely wrong that you have to have the ridiculous auto-spawned "player" objects, and so on? Maybe there's an entirely other way to call Commands.

推荐答案

您的问题的简短答案是否".仅仅是因为在MonoBehavior中使用[Command]关键字会立即在您的编辑器中导致错误,因为该代码无法编译.

The short answer to your question is No. Simply because using the [Command] keyword in a MonoBehavior will immediately result in an error in your editor, since the code can't be compiled.

UNetWeaver错误:脚本XXXXXX使用[Command] 是,但不是NetworkBehaviour.

UNetWeaver error: Script XXXXXX uses [Command] YYYYY but is not a NetworkBehaviour.

但是,还有其他可能性与您要实现的目标非常接近.

However there are other possibilities that get very close to what you want to achieve.

  • 如评论中所述,您可以使用事件和委托将您的[Command]函数订阅到从MonoBehavior继承的类中的另一个函数中.
  • 我刚刚尝试并获得成功的另一个非常聪明的可能性是使用Interfaces.
  • As discussed in the comments, you could use Events and Delegates to subscribe your [Command] function to another function from a class that inherits from MonoBehavior.
  • Another very clever possibility that I have just attempted and had success with is using Interfaces.

我刚刚制作了一个简单的接口,该接口不实现任何其他接口,它仅声明一条命令.看起来像这样:

I have just made a simple interface that does not implement any other interfaces, it just declares a command. It looks like this:

public interface ICustomCommands
{
    [Command]
    void CmdCustomSpawn(GameObject toSpawn);
}

然后,我制作了一个简单的类,将在不使用[Command]关键字的情况下实现此接口(一种方法). 我的自定义类继承自MonoBehavior ,如下所示:

Then I made a simple class that will implement this interface (the one method) without using the [Command] keyword. My custom class inherits from MonoBehavior and looks like this:

public class CustomCommands : MonoBehaviour, ICustomCommands
{
    public void CmdCustomSpawn(GameObject toSpawn)
    {
        NetworkServer.Spawn(toSpawn);
    }
}

然后,我成功地调用了CmdCustomSpawn函数,并在服务器上生成了一个播放器.

I was then successfully able to call my CmdCustomSpawn function and spawn a player on the server.

这篇关于进行“直接"交易新的Unity网络中的RPC样式调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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