C# 将UI画布同步到光子网络| PunRPC
我遇到无法将UI画布信息同步到PhotonNetwork的问题。它在没有光子的情况下可以很好地同步,我调试它时会得到正确的信息。但如果我以光子开始,它就不会同步 代码如下:C# 将UI画布同步到光子网络| PunRPC,c#,unity3d,photon,C#,Unity3d,Photon,我遇到无法将UI画布信息同步到PhotonNetwork的问题。它在没有光子的情况下可以很好地同步,我调试它时会得到正确的信息。但如果我以光子开始,它就不会同步 代码如下: void Start() { PV = GetComponent<PhotonView>(); Debug.Log("I know who is : " + PV); } void Update() { PV.RPC("DisplayHP"
void Start()
{
PV = GetComponent<PhotonView>();
Debug.Log("I know who is : " + PV);
}
void Update()
{
PV.RPC("DisplayHP", RpcTarget.All);
DisplayHP();
ShootTest();
Debug.Log("Player HP : " + playerHP);
}
}
[PunRPC]
void DisplayHP()
{
if(PV.IsMine)
{
showHP.text = playerHP.ToString();
}
}
void Start()
{
PV=GetComponent();
Log(“我知道谁是:+PV”);
}
无效更新()
{
RPC(“DisplayHP”,RpcTarget.All);
DisplayHP();
射击测试();
Log(“玩家HP:+playerHP”);
}
}
[扑克牌]
void DisplayHP()
{
if(PV.IsMine)
{
showHP.text=playerHP.ToString();
}
}
}
可能我用错了,有人能告诉我我在这里做错了什么吗?这只是健康画布中的一个示例,其他画布的作用也与健康画布完全相同
全文如下:
[SerializeField] float playerHP = 100f;
[SerializeField] TextMeshProUGUI showHP;
[SerializeField] AudioClip audioClip;
public int armorCheck;
public int energyCheck;
float energyBack = 25;
//public PhotonView PV;
void Start()
{
// PV = GetComponent<PhotonView>();
}
public void EnergyPot()
{
//if(PV.IsMine)
//{
Ammo currentAmmo = GetComponent<Ammo>();
int energyCheck = GetComponent<Ammo>().GetCurrentAmmo(AmmoType.EnergyDrink);
if (energyCheck > 0)
{
playerHP = energyBack + playerHP;
if (playerHP >= 100)
{
playerHP = 100;
currentAmmo.ReduceCurrentAmmo(AmmoType.EnergyDrink);
}
}
//}
}
public void TakeDamage(float damage)
{
// if (PV.IsMine)
//{
Ammo currentAmmo = GetComponent<Ammo>();
int armorCheck = GetComponent<Ammo>().GetCurrentAmmo(AmmoType.Armor);
if (armorCheck <= 0)
{
playerHP -= damage;
if (playerHP <= 0)
{
GetComponent<DeathHandler>().HandleDeath();
AudioSource.PlayClipAtPoint(audioClip, Camera.main.transform.position);
GetComponent<Animator>().SetTrigger("Dead_Player");
}
}
else
{
currentAmmo.ReduceCurrentAmmo(AmmoType.Armor);
}
}
//}
//public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
//{
// if(stream.IsWriting)
// {
// //DisplayHP();
// stream.SendNext(playerHP);
// }
// else if (stream.IsReading)
// {
// //DisplayHP();
// playerHP = (float)stream.ReceiveNext();
// }
//}
void Update()
{
DisplayHP();
ShootTest();
}
void ShootTest()
{
if (Input.GetMouseButtonDown(0))
{
GetComponent<Animator>().SetBool("Shoot_Player", true);
// GetComponent<Animator>().SetTrigger("ShootPlayer");
}
// GetComponent<Animator>().SetBool("Shoot_Player", false);
}
void DisplayHP()
{
//if(PV.IsMine)
//{
showHP.text = playerHP.ToString();
//Debug.Log("PV works under DisplayHP, code : " + PV);
Debug.Log("Player HP : " + playerHP);
//}
}
[SerializeField]浮点播放器HP=100f;
[SerializeField]TextMeshProUGUI showHP;
[SerializeField]音频剪辑音频剪辑;
公共内部检查;
公共能源检查;
浮动能量返回=25;
//公共PhotonView PV;
void Start()
{
//PV=GetComponent();
}
公共空间能量点()
{
//if(PV.IsMine)
//{
Ammo currentAmmo=GetComponent();
int energyCheck=GetComponent().GetCurrentAmmo(amomType.EnergyDrink);
如果(能量检查>0)
{
playerHP=能量回退+playerHP;
如果(playerHP>=100)
{
playerHP=100;
当前氨.还原氨(氨型.能量饮料);
}
}
//}
}
公共财产损失(浮动损失)
{
//if(PV.IsMine)
//{
Ammo currentAmmo=GetComponent();
int armorCheck=GetComponent().GetCurrentAmmo(amomType.Armor);
如果(armorCheck补充我的评论,这里有一个例子。假设您的HP和其他玩家的HP有Text
。首先,您通过调用UpdateHP
方法更改HP,您为HP更新Text
,并向其他人发送RPC,以更新其客户端上的其他HP(因为你将成为他们机器上的“其他人”)为你的HP带来新的价值
public Text myHpText;
public Text otherHpText;
void UpdateHp(int hp)
{
myHpText.text = hp.ToString();
PV.RPC("UpdateOtherText", RpcTarget.Others, myHp);
}
[PunRPC]
void UpdateOtherText(int hp)
{
otherHpText.text = hp.ToString();
}
您只同步更新显示的无参数方法调用。但您从未更新要显示的信息:playerHP
获取方法名称,RpcTarget
,然后根据需要/方法所需的数量选择参数
void Start()
{
PV = GetComponent<PhotonView>();
Debug.Log("I know who is : " + PV);
}
private void Update()
{
if(PV.IsMine)
{
ShootTest();
Debug.Log("Player HP : " + playerHP);
}
}
// only executed every 0.2 seconds to reduce traffic
void FixedUpdate()
{
if(PV.IsMine)
{
// pass in the value to sync
PV.RPC(nameof(DisplayHP), RpcTarget.Others, playerHP);
DisplayHP(playerHP);
}
}
[PunRPC]
void DisplayHP(float hp)
{
// receive the synced value if needed
playerHP = hp;
showHP.text = hp.ToString();
}
所以无论你什么时候
playerHP = XY;
它会自动更新显示,如果是您的光子视图,它会将远程呼叫发送给其他人,并更新值和显示。有人请:'(我非常需要这方面的帮助。你到底需要同步什么?你需要所有玩家显示相同数量的HP,或者你需要所有玩家HP的某种列表吗?无论如何,你没有在RPC中发送任何信息,你只发送调用方法的命令。并且不要在Update()中调用RPC)
,这会产生太多的流量。我有一个名为“玩家”的预设在一个大厅里,我们总共有4个玩家。所有这些玩家都将使用相同的UI画布和脚本,所以我需要将它们分开。例如:如果player1有90马力,player2应该不会在他的UI画布上看到90马力。那么你根本不需要任何与光子相关的东西,不要调用rpc,也不要检查pv.isMine,只要用你的HPat更新文本即可一开始它没有rpc和pv.ismine,但是当我用Photon实例化时它根本不起作用,这就是为什么我认为我需要通过Photon传递此信息。它在没有Photon的情况下仍然起作用,所以不太确定如何解决此问题:/Yeah,刚刚重试。我在调试播放器的HP时用Photon实例化了播放器它在控制台上显示了正确的信息,但在UI中并没有。您用光子?播放器或UI实例化了什么?似乎这是对HP金额问题的引用。您在控制台中有任何错误吗?
playerHP = XY;