我正在使用光子进行多人游戏,我希望每次死亡时都要计算我的玩家死亡人数。我使用了此脚本,但未添加任何photonView.IsMine,因为每次玩家死亡时,画布上的文本都会保留:Deaths:0。我用于死亡的脚本是
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Photon;
using Photon.Pun;
public class DeathsCount : MonoBehaviour
{
public Text countText;
public int deathcount;
void Start()
{
countText = GetComponent<Text>();
deathcount = 0;
CmdDeathsCount();
}
void CmdDeathsCount()
{
countText.text = "Death: " + deathcount.ToString();
}
void CmdSetDeathsCount(int newCount)
{
deathcount = newCount;
}
public void RpcRespawn()
{
Debug.Log(deathcount);
deathcount = deathcount + 1;
CmdDeathsCount();
}
}
以及我用来伤害玩家和致死的角色脚本中的功能:
[PunRPC]
void Damage()
{
Debug.Log("I damaged");
Health -= 20;
if (Health <= 0) // check health status
{
Health = 0; // make that Heath don't be < 0
if (photonView.IsMine)
{
myCounts.RpcRespawn(); //Here you should to call counter
}
}
}
答案 0 :(得分:2)
您需要添加一种方法来简单地增加死亡计数中的死亡计数,并在确定死亡时调用它。
void IncreaseDeathsCount()
{
deathcount++;
CmdDeathsCount()
}
[PunRPC]
void Damage()
{
Debug.Log("I damaged");
Health -= 20;
if (Health <= 0) // check health status
{
Health = 0; // make that Heath don't be < 0
if (photonView.IsMine)
{
myCounts.RpcRespawn(); //Here you should to call counter
DeathsCount.cmdDeathsCount;
}
}
}
但是,您要么必须将cmdDeathsCount设为静态,要么添加对实例的引用才能调用它