【发布时间】: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