Unity用户名未显示

时间:2017-02-24 09:47:45

标签: c# multiplayer unity-networking

我和Unity有一个小型多人游戏。作为主机(汽车名为NiciBozz),它看起来像这样: Host view

这很好,但作为客户端(此处名为NiciBot),它看起来像这样: Client View

与用户名相关的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
[NetworkSettings(channel = 1, sendInterval = 0.2f)]

public class PlayerControll : NetworkBehaviour
{
    public Text Name;

    [SyncVar]
    public string playerName;

    private void Start()
    {
        if (isLocalPlayer)
        {
            CmdChangeName(PlayerPrefs.GetString("Name"));
        }
    }

    [Command(channel =1)]
    private void CmdChangeName(string name)
    {
        if (!isLocalPlayer)
        {
            Name.text = name;
            playerName = name;
            SetDirtyBit(1);
            return;
        }
        Name.text = name;
        playerName = name;
        SetDirtyBit(1);
    }
}

如何正确同步用户名?

2 个答案:

答案 0 :(得分:0)

如果isLocalPlayer成立,您似乎只尝试设置名称,但在CmdChangeName中,您有if来检查isLocalPlayer的值(这将是true根据您的代码段始终为CmdChangeName

你在其他地方打电话给<item name="android:colorAccent">@color/colorJust</item> 吗?

答案 1 :(得分:0)

使用syncVar命令使用这样的东西:

$.then()