c# - 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]
非常感谢!
解决方案
您也想在标题中使用 Darkrift.client,而不是 darkrift.server。干杯。
推荐阅读
- c# - 有没有办法在 Unity 中缩放 2D 画布?
- java - Spring boot:预期的 json 响应,获取 XML 响应
- javascript - 反应原生,将 xml 字符串作为 .xml 文件发送
- python - Python 类中用于检索子子文件夹的问题
- django - 限制对 Django 中某些页面的访问
- javascript - Nuxtjs/Toast 和自定义图标
- node.js - Mocha 和 ts-node 给出“不支持的参数”
- html - Swup 与 svg xlink
- python - python pandas将唯一行转换为具有各自数据的新列
- javascript - 查找两个字符串之间的差异并将差异存储在javascript中的另一个字符串中