【问题标题】:Unity Spawning child with client authorityUnity 生成具有客户端权限的子节点
【发布时间】:2016-12-05 03:56:01
【问题描述】:

我目前正在了解整个网络如何统一运作。在我的代码中,我正在创建一个由多个预制件制成的宇宙飞船。

这一切都始于一个HardpointHardpoint 可以保存一个对象,稍后将在循环中实例化该对象。

PlayerController(起点)我有这段代码来生成第一个对象,驾驶舱:

[Command]
void CmdOnConnect() {
    string json = GameObject.Find("TestPlayer").GetComponent<ComponentObject>().ToJSON();
    CompressedComponent compressedComponent = JsonUtility.FromJson<CompressedComponent>(json);
    gameObject.GetComponent<Hardpoint>().Hold(GameObject.Find("Component Repository").GetComponent<ComponentRepository>().cockpit[compressedComponent.componentNumber]);
    gameObject.GetComponent<Hardpoint>().SpawnComponent();
    gameObject.GetComponent<Hardpoint>().RollThroughDecompression(compressedComponent);
    Camera.main.GetComponent<PlayerCamera>().player = gameObject;
}

接下来是SpawnComponent() 代码,位于Hardpoint 脚本中:

public void SpawnComponent() {
    Clear();
    CmdSpawn();
}

CmdSpawn,同样位于Hardpoint

[Command]
public void CmdSpawn()
{
    Debug.Log("[COMMAND] Spawning " + holds.name);
    heldInstance = Instantiate(holds, transform.position, transform.rotation) as GameObject;
    heldInstance.transform.SetParent(transform);
    NetworkServer.SpawnWithClientAuthority(heldInstance, transform.root.gameObject);
}

最后是RollThroughDecompression,它只调用了Decompress() 函数:

public void RollThroughDecompression(CompressedComponent c) {
    heldInstance.GetComponent<ComponentObject>().Decompress(c);
}

为了不遗漏任何信息,Decompress():

public void Decompress(CompressedComponent c) {
    componentType = (Type)Enum.Parse(typeof(Type), c.componentType);
    componentNumber = c.componentNumber;
    UpdateHardPoints();
    GameObject[] typeRepository = GetRepository(componentType);

    //update children 
    int point = 0;
    foreach (Transform child in transform)
    {
        Hardpoint hardpoint = child.GetComponent<Hardpoint>();
        if (hardpoint != null) {
            if (c.hardpoints[point] != null) {
                //get the hardpoint's repository
                GameObject[] hardpointRepo = GetRepository((Type)Enum.Parse(typeof(Type), c.hardpoints[point].componentType));
                //set the hardpoint to hold this object
                hardpoint.Hold(hardpointRepo[c.hardpoints[point].componentNumber]);
                hardpoint.SpawnComponent();
                hardpoint.RollThroughDecompression(c.hardpoints[point]);
                point++;
            }
        }
    }
}

对不起,代码有点混乱/令人困惑,但我一直在试图弄清楚为什么新生成的对象没有client authority,除了生成的第一个对象(可能是因为它是从PlayerController)。我已经被这个问题困扰了好几天了。新生成的对象被设置为本地玩家对象的子对象,甚至在测试时使用NetworkServer.SpawnWithClientAuthority 生成:

Trying to send command for object without authority. 调用 CmdSpawn() 时。

NetworkManager:

我得到的结果:

如您所见,驾驶舱(第一部分)按预期生成。但安装在Hardpoints 上的零件没有。澄清一下,EmptyHardpoint 就是这样。一个没有孩子的硬点,只是一个带有hardpoint 脚本和playercontroller 附加到它的空游戏对象。驾驶舱预制件还包括imghardpoints

【问题讨论】:

    标签: unity3d unity5 unity3d-unet


    【解决方案1】:

    我猜你忘记将你的子 Spawn 添加到 NetworkManager 的 Spawn 列表中。

    【讨论】:

    • 不正确。这也是我的第一个猜测,但是应该生成的第一个组件确实会生成,而其他组件则不会。一会儿我会发截图。
    • 如果你是分开生成的,所以分开分配
    • 对不起,看错了。已更新,但不幸的是问题仍然存在。
    • 我现在收到更多警告。由于附加了脚本,所有hardpoints 都有网络身份,它是networkbehavior,所以我无法删除它。我将转移一些代码,看看我是否可以摆脱警告消息并就此与您联系。我想我们在这里做点什么,谢谢!
    猜你喜欢
    • 2018-02-24
    • 1970-01-01
    • 1970-01-01
    • 2018-02-03
    • 2018-05-23
    • 2018-04-27
    • 2020-06-24
    • 2014-02-24
    • 2021-12-31
    相关资源
    最近更新 更多