首页 > 解决方案 > Unity Dark Rift Online 事件 Args 问题

问题描述

我们正在尝试建立一个在线服务器,并将玩家连接到使用库集合 Dark Rift 的服务器。我们的部分代码检查玩家何时加入游戏时遇到错误,它会生成一个连接到其 ID 的玩家:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DarkRift;
using DarkRift.Server;
using DarkRift.Client.Unity;

public class PlayerSpawner : MonoBehaviour
{
    const byte SPAWN_TAG = 0;

    [SerializeField]
    [Tooltip("The DarkRift client to communicate on.")]
    UnityClient client;

    [SerializeField]
    [Tooltip("The controllable player prefab.")]
    GameObject controllablePrefab;

    [SerializeField]
    [Tooltip("The network controllable player prefab.")]
    GameObject networkPrefab;

    void Awake()
    {
        if (client == null)
        {
            Debug.LogError("Client unassigned in PlayerSpawner.");
            Application.Quit();
        }

        if (controllablePrefab == null)
        {
            Debug.LogError("Controllable Prefab unassigned in PlayerSpawner.");
            Application.Quit();
        }

        if (networkPrefab == null)
        {
            Debug.LogError("Network Prefab unassigned in PlayerSpawner.");
            Application.Quit();
        }
        client.MessageReceived += SpawnPlayer;
    }
    void SpawnPlayer(object sender, MessageReceivedEventArgs e)
    {
        using (Message message = e.GetMessage())
        using (DarkRiftReader reader = message.GetReader())
        {
            if (message.Tag == Tags.SpawnPlayerTag)
            {
                if (reader.Length % 17 != 0)
                {
                    Debug.LogWarning("Received malformed spawn packet.");
                    return;
                }

                while (reader.Position < reader.Length)
                {
                    ushort id = reader.ReadUInt16();
                    Vector3 position = new Vector3(0, 0);

                    GameObject obj;
                    if (id == client.ID)
                    {
                        obj = Instantiate(controllablePrefab, position, Quaternion.identity) as GameObject;
                    }
                    else
                    {
                        obj = Instantiate(networkPrefab, position, Quaternion.identity) as GameObject;
                    }
                }
            }
        }
    }
}

在 client.MessageRecieved += SpawnPlayer; 行它没有给 SpawnPlayer 类型的参数(它想要 MessageRecievedEventArgs,但得到的是其他东西而不是我们认为的)。错误说:

No overload for 'SpawnPlayer' matches delegate 'EventHandler<MessageReceivedEventArgs>'[Assembly-CSharp] csharp(cs0123) [43, 9]

非常感谢!

标签: c#unity3dnetworkingservereventargs

解决方案


您也想在标题中使用 Darkrift.client,而不是 darkrift.server。干杯。


推荐阅读