在 Unity 中使用 PUN 同步文本
Posted
技术标签:
【中文标题】在 Unity 中使用 PUN 同步文本【英文标题】:Syncing Text using PUN in Unity 【发布时间】:2021-08-16 12:26:49 【问题描述】:我正在制作一个简单的 Word Builder 类型的游戏(其中一个玩家输入一个单词,另一个玩家输入一个以第一个玩家单词的最后一个字母开头的单词),但我无法获取 Text to出现在两个屏幕上。 如果我对其进行硬编码,我可以让文本显示出来,例如:
[PunRPC]
public void DisplayWord ()
MainWord.text = "Word";
但我无法让它在两个屏幕上显示玩家刚刚输入的单词,我认为这段代码会显示:
[PunRPC]
public void DisplayWord ()
MainWord.text = UsedString;
//UsedString is the word the (one of the) players just entered.
任何帮助将不胜感激,非常感谢!
这是输入单词的完整代码:
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
using Photon.Pun;
public class DictionaryScript : MonoBehaviourPunCallbacks
List<string> DictA = new List<string> ()
"a",
"aa",
"aaa",
"aah",
"aahed",
"aahing",
"aahs",
"aal",
"aalii",
"aaliis",
"aals",
"aam",
"aani",
"aardvark",
"aardvarks",
"aardwolf",
"aardwolves",
"aargh",
"aaron",
"aaronic",
"aaronical",
"aaronite",
"aaronitic",
"aarrgh",
"aarrghh",
"aaru",
"aas",
"aasvogel",
"aasvogels";
public List<string> DictAUsed = new List<string>() ;
public string UsedString;
public string NewString;
public InputField inputField;
public Text AddedWord;
public Text MainWord;
public Text TurnText;
public Text ConnectedText;
public Button StartButton;
public Canvas OverlayCanvas;
// Start is called before the first frame update
public void Awake()
if (PhotonNetwork.IsMasterClient)
StartButton.enabled = true;
OverlayCanvas.enabled = true;
else
StartButton.enabled = false;
OverlayCanvas.enabled = true;
void Start()
if (PhotonNetwork.InRoom)
ConnectedText.text = "Connected!";
public void StartGame ()
photonView.RPC("CanvasDisplay", RpcTarget.All);
public void MyWord (string newWord)
string newString = newWord.ToLower();
print(newString);
if (DictA.Contains(newString) && newString[0] == MainWord.text[0] && !DictAUsed.Contains(newString))
UsedString = newString;
Debug.Log(UsedString);
photonView.RPC("OnInput", RpcTarget.All);
else
print("This word does not exist!");
AddedWord.text = newString + " is not a valid word!";
inputField.text = "";
[PunRPC]
public void OnInput ()
DictAUsed.Add(UsedString);
AddedWord.text = "The word was: " + UsedString;
photonView.RPC("DisplayWord", RpcTarget.All);
[PunRPC]
public void DisplayWord ()
MainWord.text = UsedString;
//UsedString is the word the (one of the) players just entered.
[PunRPC]
public void CanvasDisplay()
//string Alphabet = "abcdefghijklmnopqrstuvwxyz";
string Alphabet = "aaaaaaaaaaaaaaaaaaaaaaaaaa";
MainWord.text = Alphabet[Random.Range(0, 25)].ToString();
StartButton.enabled = false;
OverlayCanvas.enabled = false;
我现在只有以“aa”开头的单词,一旦我能正常工作,我会添加所有单词。
【问题讨论】:
【参考方案1】:UsedString
未在您的网络中同步 => 在调用 RPC 时,每个玩家可能有不同的 UsedWorld
值。
为什么不将UsedWord
作为参数传递给OnInput
和DisplayWord
?
还有为什么DisplayWord
甚至通过 RPC 调用?由于OnInput
已经与ALL
同步,您可以直接调用该方法
...
photonView.RPC(nameof(OnInput), RpcTarget.All, UsedWord);
...
[PunRPC]
public void OnInput (string usedString)
DictAUsed.Add(usedString);
AddedWord.text = "The word was: " + usedString;
// since this shall happen on any client as well
// why even call it via RPC?
// OnInput is called on ALL anyway so just do it right away
DisplayWord(usedString);
public void DisplayWord (string usedString)
MainWord.text = usedString;
【讨论】:
非常感谢!这似乎解决了它。我尝试过类似的方法,但我不确定如何使用参数调用 RPC 方法,这清除了它。以上是关于在 Unity 中使用 PUN 同步文本的主要内容,如果未能解决你的问题,请参考以下文章
使用 Photon Unity Network(多人游戏)在 Unity 中实时同步