你好吗?我现在正在研究镜像网络。
在此处输入图像描述
然而,我在获取其他玩家的价值方面有一个问题。这张图片解释了我想做什么。
在此处输入图像描述
我创建了3个项目。一个是服务器(本地主机(,一个是客户端A,另一个是客户B。
首先,我写了这样的代码:
public class PlayerManager : NetworkBehaviour
{
[SyncVar(hook = nameof(onValueChanged))]
int value = 0;
private void Update()
{
if(isServer && Input.GetKeyDown("x"))
{
Message();
}
}
public override void OnStartServer()
{
Debug.Log("Here is Game Room Scene, Player add Successfully");
}
[Command]
private void Hola()
{
value++;
Debug.Log("Received Hola from the client!");
Debug.Log("Server Value : " + value);
ReplyHola();
}
[TargetRpc]
private void ReplyHola()
{
Debug.Log("Received Hola from Client!");
}
[ClientRpc]
private void Message()
{
Debug.Log("Ping...");
}
[ClientRpc]
private void UpdateValue(int value)
{
this.value = value;
}
private void onValueChanged(int oldValue, int newValue)
{
Debug.Log("New Value Detective :");
Debug.Log("Old Value : " + oldValue);
Debug.Log("New Value : " + newValue);
Debug.Log("Sum Value : " + PlayerStat.Value);
}
}
3个项目都有相同的代码。我引用了此视频中的代码(https://www.youtube.com/watch?v=8tKFF0RP9Jw)。
而且,我写了关于sum客户端A和B的代码,如下所示:
private void SumDatas()
{
foreach(var playerObj in FindObjectsOfType(typeof(GameObject)) as GameObject[])
{
if(gameObject.name == "Player(Clone)")
{
PlayerStat.Value += GameObject.Find("Player(Clone)").transform.GetComponent<PlayerManager>().GetValue();
}
}
}
PlayerStat是一个静态类,代码如下:
public static class PlayerStat
{
public static int Value { get; set; }
}
有人帮我吗?
我解决了这个问题。我不会删除这个问题的其他人谁会对我有同样的问题。
我在我的服务器项目、客户端A和B项目中添加了以下代码:
[SyncVar(hook = nameof(onValueChanged))]
int value = 0;
int myValue = 0;
private void Update()
{
myValue = PlayerStat.Value;
}