【问题标题】:Unity networking. transform.setparent() not working on a clinet side统一网络。 transform.setparent() 在客户端不起作用
【发布时间】:2019-02-16 12:52:04
【问题描述】:

我正在用 Unity 编写一个简单的多人棋盘游戏。

我有以下问题:transport.setparent() 在客户端不起作用。 当我将游戏作为服务器启动时,一切正常。当我作为客户端连接到服务器时,transform.setParent() 什么都不做。

这是我的代码:

public GameObject PlayerPrefab;

private GameObject player;

// Use this for initialization
void Start () {
    if (!isLocalPlayer)
    {
        return;
    }
    Debug.Log("Spawning.");
    CmdSpawn();
}


[Command]
void CmdSpawn()
{
    player = Instantiate(PlayerPrefab);
    NetworkServer.SpawnWithClientAuthority(player, connectionToClient);
    player.transform.SetParent(GameObject.Find("BoardPanel").transform, false);
}

【问题讨论】:

  • 你有没有试过SetParent之前打电话给SpawnWithClientAuthority

标签: unity3d unity3d-unet


【解决方案1】:

我找到了答案。这是我的解决方案: 步骤 1) 使用 SyncVar 在服务器和客户端之间同步父对象的 netID。 第 2 步)当对象在客户端生成时,使用同步的 netID 找到父对象并将其设置为转换的父对象。

[Command]
void CmdSpawn()
{
    Debug.Log("Spawning.");
    player = Instantiate(PlayerPrefab);
    player.GetComponent<Player>().ParentNetId = this.netId;
    NetworkServer.SpawnWithClientAuthority(player, connectionToClient);
}

并且需要在播放器脚本中添加这段代码:

[SyncVar]
public NetworkInstanceId ParentNetId;

public override void OnStartClient()
{
    Debug.Log("OnStartClient.");
    transform.SetParent(GameObject.Find("BoardPanel").transform, false);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-04
    相关资源
    最近更新 更多