使用Unity UNET同步变量

时间:2018-08-06 16:21:33

标签: c# unity3d unity3d-unet unet

我想做什么 ·启动应用程序1,应用程序2 ·如果在应用程序1中按下按钮,即使在应用程序2中也将按下按钮(更改click事件或bool类型变量)

我尝试过的方法(我将Unity文件放在这里) https://drive.google.com/open?id=1aV-1SiB56L3JGVzWqAmqc85PYFNW1268

将以下内容添加到按钮的对象中,单击

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class MoviePlay : NetworkBehaviour {
    [SyncVar]
    bool IsPlay = false;

    void Update(){
        if(IsPlay){
            Debug.Log("再生中");
        }        
    }

    public void OnClick(){
        CmdOnClick();
    }
    [Command]
    void CmdOnClick(){
        IsPlay = true;
    }
}

结果:控制台显示“未找到1的同步消息目标,并且未同步”

-----添加------

我尝试过。 PushButton(播放器对象)

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

public class PushButton : NetworkBehaviour {
    [SerializeField]
    GameObject DebugScript;

    public void OnClickButton(){
        NetworkInstanceId target = DebugScript.GetComponent<NetworkIdentity>().netId;
        CmdSendMessageToServer(target, "DebugText");
    }
    [Command]
    public void CmdSendMessageToServer (NetworkInstanceId target, string message)
    {
        NetworkServer.FindLocalObject(target).SendMessage(message);
    }
}

DebugScript

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class DebugScript : MonoBehaviour {
    void DebugText(){
        Debug.Log("再生");
    }
}

1 个答案:

答案 0 :(得分:0)

可以调用播放器对象上的命令。因此,如果它不是本地播放器,则根本不会调用命令。您必须将函数移至播放器对象,然后在按钮上调用方法。

我没有在播放器上制作大量方法,而是制作了一个方法来仅利用每个GameObject的SendMessage方法。我在播放器对象上有一个名为CmdSendMessageToServer的方法,该方法具有两个参数,即NetworkInstanceId和一个字符串。看起来有点像这样...

[Command]
public void CmdSendMessageToServer (NetworkInstanceId target, string message)
{
     NetworkServer.FindLocalObject(target).SendMessage(message);
}

将按钮的“ netId”(NetworkInstanceId)变量以及您要调用的方法名称(在本例中为OnClick)传递给上述函数。

希望这会有所帮助!

编辑:为澄清起见,您应该制作一个放置在播放器对象上的单独的类。就像下面的...

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class TestNetworkPlayer : NetworkBehaviour 
{
     //a singleton for the CLIENT
     public static TestNetworkPlayer local;

     public override void OnStartLocalPlayer ()
     {
        base.OnStartLocalPlayer();
        //so whenever we access TestNetworkPlayer.local,
        //it will ALWAYS be looking for the LOCAL player on the client we use
        local = this;
     }

    [Command]
    public void CmdSendMessageToServer(NetworkInstanceId target, string message)
    {
        NetworkServer.FindLocalObject(target).SendMessage(message);
    }
}

,然后在“ MoviePlay”脚本中,您将获得此代码...

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class MoviePlay : NetworkBehaviour {
    [SyncVar]
    bool IsPlay = false;

    void Update(){
        if(IsPlay){
            Debug.Log("再生中");
        }        
    }

    //called when the button is clicked
    public void OnClick(){
        TestNetworkPlayer.local.CmdSendMessageToServer (netId, "ServerOnClick");
    }

    //ONLY called on the server, and then IsPlay should change on all clients as well
    void ServerOnClick(){
        IsPlay = true;
    }
}

希望这可以清除它!