满足条件时显示 Photon Room(4 名玩家)中一名玩家的字符串
Displaying a string of one of the players in a Photon Room (4 players) when a condition is met
Playerprefs 有一种设置和获取玩家信息的方法,例如字符串、整数等
我正在使用 photon 来设置和获取每个玩家的字符串,但它只能捕获一个本地玩家!每个玩家输入的字符串稍后应该仅在满足条件(bool)时显示给所有其他玩家。
Playerprefs 是否实际存储了每个玩家的信息并且是否真的可以检索(每个玩家)?
例如:
轮到你玩时,假设一个布尔值设置为 true,然后向所有其他玩家显示你的字符串。
所以应该启动game over,阻止游戏继续!
我是通过下面的脚本来做到这一点的吗:您的建议或更正确实意义重大。我已经卡了一段时间了。
我的第一个脚本(输入字符串):
[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);
}
结果很好。经过多次尝试和理解,我能够得到每个玩家的字符串。
代码如下:
[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);
}
Playerprefs 有一种设置和获取玩家信息的方法,例如字符串、整数等
我正在使用 photon 来设置和获取每个玩家的字符串,但它只能捕获一个本地玩家!每个玩家输入的字符串稍后应该仅在满足条件(bool)时显示给所有其他玩家。
Playerprefs 是否实际存储了每个玩家的信息并且是否真的可以检索(每个玩家)?
例如: 轮到你玩时,假设一个布尔值设置为 true,然后向所有其他玩家显示你的字符串。 所以应该启动game over,阻止游戏继续!
我是通过下面的脚本来做到这一点的吗:您的建议或更正确实意义重大。我已经卡了一段时间了。
我的第一个脚本(输入字符串):
[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);
}
结果很好。经过多次尝试和理解,我能够得到每个玩家的字符串。
代码如下:
[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);
}