我想做什么·启动应用程序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的同步消息的目标,并且未同步
-添加
我试过了。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("再生");
}
}发布于 2018-08-07 00:42:54
命令只能在播放器对象上调用。因此,如果它不是本地播放器,命令就不会被调用。您必须将该函数移动到player对象,并调用按钮上的方法。
我没有在播放器上创建成堆的方法,而是只创建了一个方法,只利用每个GameObject的SendMessage方法。我在player对象上有一个名为CmdSendMessageToServer的方法,它有两个参数,一个NetworkInstanceId和一个string。它看起来有点像这样...
[Command]
public void CmdSendMessageToServer (NetworkInstanceId target, string message)
{
NetworkServer.FindLocalObject(target).SendMessage(message);
}将按钮的'netId‘( NetworkInstanceId)变量传递给上面的函数,以及要调用的方法的名称(在本例中为OnClick)。
希望这能有所帮助!
编辑:为了清楚起见,您应该在player对象上创建一个单独的类。就像下面这样...
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;
}
}希望这能澄清这一点!
https://stackoverflow.com/questions/51711897
复制相似问题