1

我想做什么·启动应用程序1,应用程序2·如果我在应用程序1中按下按钮,即使在应用程序2中也会按下按钮(更改点击事件或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 找不到同步消息的目标并且未同步

- - -添加 - - -

我尝试过这个。按钮(播放器对象)

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);
    }
}

调试脚本

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

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

1 回答 1

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;
    }
}

希望这可以清除它!

于 2018-08-06T16:42:53.750 回答