【问题标题】:Displaying a string of one of the players in a Photon Room (4 players) when a condition is met满足条件时显示 Photon Room(4 个玩家)中的一个玩家的字符串
【发布时间】:2021-11-08 17:16:51
【问题描述】:

Playerprefs 有一种设置和获取玩家信息的方法,例如字符串、int 等。

我正在使用 photon 来设置和获取每个玩家的字符串,但它只捕获一个本地玩家!每个玩家输入的字符串稍后应仅在满足条件时显示给所有其他玩家(布尔)。

Playerprefs 是否实际存储了每个玩家的信息,是否可以实际检索(每个玩家)?

例如: 当轮到你玩时,假设一个 bool 设置为 true,然后将你的字符串显示给所有其他玩家。 所以应该激活游戏结束并阻止游戏继续!

我是否通过以下脚本执行此操作:您的建议或更正真的很重要。我已经卡了一段时间了。

我的第一个脚本(输入字符串):

[SerializeField]
WordInfo wordInfo;

void Update()
{
    wordInfo.word = StringWord;
}

public void GetString()
{
    StringWord = PlayerPrefs.GetString("word", "no word");
}
//use Onclick to set the string 
public void SetString()
{
    saveWord = InputWord.text;
    PlayerPrefs.SetString("word", saveWord);
}

我的第二个脚本(检索输入的字符串以从另一个场景读取(静态))

public class WordInfo: MonoBehaviour
{
public string word;
public static WordInfo wordInfo;

private void Awake()
{
    if (wordInfo == null)
    {
        wordInfo = this;
        DontDestroyOnLoad(gameObject);
    }
    else if(wordInfo != this)
    {
        Destroy(wordInfo.gameObject);
        wordInfo = this;
        DontDestroyOnLoad(gameObject);
    }
}
}

我的第三个脚本(游戏脚本 - 不同的场景)

public string TheSavedWord;
private const string DISPLAY_MY_STRING_PLAYER = "{0} Your Word is displayed to others!";
private const string DISPLAY_OTHER_STRING_PLAYER = "{0}'s word has been Displayed!";
private const string MY_WORD = "{0} your word is: ";

private void Update()
{
    TheSavedWord = WordInfo.wordInfo.word;

}
 
public void ShowPlayerString()
{
 //a bool to check when to display the string
 //if i am active and the isShow is set to true then display a notification
 Player_Notification.text = string.Format(isShow && iAmActive ? DISPLAY_MY_STRING_PLAYER : DISPLAY_OTHER_STRING_PLAYER, activePlayer.NickName);
 
 //display the string 
 WordToDisplay.text = string.Format(isShow && iAmActive ? MY_WORD+ " " + TheSavedWord: MY_WORD + " " + TheSavedWord, activePlayer.NickName);

}

【问题讨论】:

  • 播放器首选项对于设置值的播放器是本地的。如果需要向所有客户端传输值,则需要使用 RPC 或 Player CustomProperties。
  • PlayerPrefs 存储您想要的任何内容本地 .. 它与 Photon 的自定义属性无关...
  • 我正在尝试使用自定义属性,但这对我来说非常技术性!我真的知道我想要什么,但当我开始输入时,我对调用属性感到困惑。
  • @derHugo 你的指导意义重大!

标签: c# visual-studio unity3d photon


【解决方案1】:

结果很好。经过多次尝试和理解,我能够得到每个玩家的字符串。

代码如下:

[SerializeField]
WordInfo wordInfo;

void Update()
{
    wordInfo.word = saveWord;
}

//use Onclick to set the string 
public void SetString()
{
    saveWord = InputWord.text;

    var hash = PhotonNetwork.LocalPlayer.CustomProperties;
    hash.Add("SaveWord", saveWord);
    PhotonNetwork.LocalPlayer.SetCustomProperties(hash);

    if (!PhotonNetwork.IsMasterClient) return;

    Debug.Log(PhotonNetwork.LocalPlayer.ToStringFull());
    
}
public override void OnPlayerPropertiesUpdate(Player targetPlayer, ExitGames.Client.Photon.Hashtable changedProps)
{
    base.OnPlayerPropertiesUpdate(targetPlayer, changedProps);

    if (!PhotonNetwork.IsMasterClient) return;
    if (!changedProps.ContainsKey("SaveWord")) return;
}

最初,我使用的是仅在本地调用播放器的播放器首选项。所以这是我之前的代码(改进之前的原始代码):

[SerializeField]
WordInfo wordInfo;

void Update()
{
    wordInfo.word = StringWord;
}

public void GetString()
{
    StringWord = PlayerPrefs.GetString("word", "no word");
}
//use Onclick to set the string 
public void SetString()
{
    saveWord = InputWord.text;
    PlayerPrefs.SetString("word", saveWord);
}

【讨论】:

  • 为了帮助使这个答案更清楚,您能否总结一下您的原始代码与您在答案中提供的代码之间的差异?
  • 刚刚更新了对原代码的总结@PeterGordon
猜你喜欢
  • 2018-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多