【发布时间】:2021-09-23 07:05:48
【问题描述】:
真的可以吗?
我在命令中将单个值发送回服务器没有问题,但我无法将它们打包成游戏对象(具有网络身份),然后将其作为参数发送到命令中。
按照 Mirror 文档应该是可能的,我已经尝试按照这个例子:
class Item
{
public string name;
}
class Weapon : Item
{
public int hitPoints;
}
class Armor : Item
{
public int hitPoints;
public int level;
}
class Player : NetworkBehaviour
{
[Command]
void CmdEquip(Item item)
{
// IMPORTANT: this does not work. Mirror will pass you an object of type item
// even if you pass a weapon or an armor.
if (item is Weapon weapon)
{
// The item is a weapon,
// maybe you need to equip it in the hand
}
else if (item is Armor armor)
{
// you might want to equip armor in the body
}
}
[Command]
void CmdEquipArmor(Armor armor)
{
// IMPORTANT: this does not work either, you will receive an armor, but
// the armor will not have a valid Item.name, even if you passed an armor with name
}
}
但是我没有运气,在服务器上而不是我传递的对象,服务器有自己的副本和自己的值。
知道我哪里出错了吗?
【问题讨论】:
-
您是否尝试过让您的课程
[Serializable]?对于CmdEquip,如果参数是Item,您通常只会得到Item。最近实现了一个自定义多用户模块,通常您会查看预期的类型,创建该类型的实例并用接收到的数据填充它。如果镜子有什么不同,我不能告诉你;) -
我确实在 Rusty 提到它之后添加了 [Serializable] 和 [SerializeField],但没有骰子。我不认为 Mirror 使用了这些属性,但可能是错误的!
-
这些属性(至少
[Serializable])是核心c#不是 Unity 或 Mirror 特定的;)我只认为 mirrorigjt 使用需要这些的 Unity 序列化程序.. 参见例如mirror-networking.gitbook.io/docs/guides/data-types -
->
Classes as long as each field has a supported data type These will allocate garbage and will be instantiated new on the receiver every time they're sent.所以对于第一个问题,他们和我一样 -> 只有接收到的类型很重要,你永远不会得到Weapon或Armor作为参数。我会为这种情况推荐一个重载 -
一般来说,它会帮助你向我们展示你是如何调用这些方法的;)