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

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

问题描述

说你有

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

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 { }

因此,每个命令(如Jump")都必须由该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()

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

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

(注意 - 显然,你会在那里缓存通信".这只是一个例子.)

旁白:非常重要:这只是一个大纲示例.

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 - a MonoBehavior)直接调用 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?

我真的完全错了,你必须拥有可笑的自动生成的玩家"对象等等?也许有一种完全不同的方式来调用 Commands.

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]YYYYY 但不是网络行为.

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 的类中的另一个函数.
  • 我刚刚尝试并取得成功的另一个非常聪明的可能性是使用接口.
  • 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天全站免登陆