服务器端的UNET SyncVar未更新

时间:2017-06-30 19:39:08

标签: c# unity3d unity3d-unet

我正在尝试使用SyncVar,但我不完全理解我做错了什么,如果我做错了那就是。情况如下:

  1. 我有两个公开的SyncVar:redFunds和blueFunds以及两个本地" -version与...进行比较

  2. 我使用Cmd_UpdateXxx在Start中启动SyncVar,它可以正常工作

  3. 我有两个按钮,每个按钮对应一个SyncVar

  4. 在更新中,我比较了SyncVar与oldXxxFunds。如果我在场景中显示了一个点击

  5. 当我运行代码时,它确实在两个玩家的场景中显示正确的数字(红色和蓝色),但在查看" public"时,编辑器中没有完全反映出来。 SyncVar' S。当按下红色按钮时,它只在编辑器中反映红色播放器而不是蓝色。

    有人可以向我解释我在这里做错了什么吗? ......如果我做错了那就是。难道我也不能在编辑器中看到变化吗?

    [SyncVar] public int redFunds;
    [SyncVar] public int blueFunds;
    public int oldRedFunds;
    public int oldBlueFunds;
    
    private void Start () 
    {
    
        if (!isLocalPlayer)
            return;
    
        Cmd_UpdateRed (10);
        Cmd_UpdateBlue (20);
    }
    
    // Button
    void btn_Red () 
    {
        if (!hasAuthority)
            return;
    
        Cmd_UpdateRed (10000);
    }
    
    void btn_Blue () 
    {
        if (!hasAuthority)
            return;
    
        Cmd_UpdateBlue (20000);
    }
    
    [Command]
    void Cmd_UpdateRed (int _value) 
    {
        redFunds = _value;
    }
    
    [Command]
    void Cmd_UpdateBlue (int _value) 
    {
        blueFunds = _value;
    }
    
    void Update () 
    {
        if (redFunds != oldRedFunds) 
        {
            txt_RedTotalFunds = GameObject.Find ("txt_RedTotalFunds").GetComponent<Text> ();
            txt_RedTotalFunds.text = "$" + redFunds;
            oldRedFunds = redFunds;
        }
    
        if (blueFunds != oldBlueFunds) 
        {
            txt_BlueTotalFunds = GameObject.Find ("txt_BlueTotalFunds").GetComponent<Text> ();
            txt_BlueTotalFunds.text = "$" + blueFunds;
            oldBlueFunds = blueFunds;
        }
    }
    

1 个答案:

答案 0 :(得分:1)

我能想到的最好的方法是使用GameRoomInfo作为服务器拥有的网络对象。

可以使用以下2个简单脚本完成:

GameRoomInfo脚本

using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using System.Collections;

public class GameRoomInfo : NetworkBehaviour
{
    public Text txt_RedTotalFunds;
    public Text txt_BlueTotalFunds;

    public int oldRedFunds = 0;
    public int oldBlueFunds = 0;

    [SyncVar]
    public int redFunds;
    [SyncVar]
    public int blueFunds;

    void Update()
    {
        if (redFunds != oldRedFunds)
        {
            //txt_RedTotalFunds = GameObject.Find("txt_RedTotalFunds").GetComponent<Text>();
            txt_RedTotalFunds.text = "$" + redFunds;
            Debug.Log("Red - $" + redFunds);
            oldRedFunds = redFunds;
        }

        if (blueFunds != oldBlueFunds)
        {
            //txt_BlueTotalFunds = GameObject.Find("txt_BlueTotalFunds").GetComponent<Text>();
            txt_BlueTotalFunds.text = "$" + blueFunds;
            Debug.Log("Blue - $" + blueFunds);
            oldBlueFunds = blueFunds;
        }
    }
}

播放器脚本

using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using System.Collections;

public class Player : NetworkBehaviour 
{
    public enum PlayerColor
    { 
        Red, 
        Blue
    };

    //used to update the right variables (Blue player updates blueFunds and Red player updates redFunds)
    [SyncVar]
    PlayerColor playerColor;

    static int colorSelect = 0;

    void Start()
    {
        if(isServer) 
        {
            // the server selects the player color when created
            switch(colorSelect % 2)
            {
                case 0:
                    playerColor = PlayerColor.Red;
                    break;
                case 1:
                    playerColor = PlayerColor.Blue;
                    break;
            }

            colorSelect++;
        }
    }

    void Update()
    {
        if (!isLocalPlayer)
            return;

        if (hasAuthority && Input.GetKeyDown(KeyCode.KeypadPlus))
        {
            Cmd_UpdateAdd(10);
        }
    }

    // Button
    void btn_Update()
    {
        if (!hasAuthority)
            return;

        Cmd_Update(0);
    }

    void btn_Add()
    {
        if (!hasAuthority)
            return;

        Cmd_Update(10);
    }


    [Command]
    public void Cmd_Update(int _value)//The command updates the GameRoomInfo variables according to the player color
    {
        switch (playerColor)
        {
            case PlayerColor.Red:
                FindObjectOfType<GameRoomInfo>().redFunds = _value;
                break;

            case PlayerColor.Blue:
                FindObjectOfType<GameRoomInfo>().blueFunds = _value;
                break;

            default:
                break;
        }
    }

    [Command]
    public void Cmd_UpdateAdd(int _value)
    {
        switch (playerColor)
        {
            case PlayerColor.Red:
                FindObjectOfType<GameRoomInfo>().redFunds += _value;
                break;

            case PlayerColor.Blue:
                FindObjectOfType<GameRoomInfo>().blueFunds += _value;
                break;

            default:
                break;
        }
    }
}

我做了一些小改动来帮助测试。随意适应您的需求。