通过网络生成 2 个不同的预制件 Unity PUN 2

发布于 2025-01-12 23:55:28 字数 1621 浏览 3 评论 0原文

我正在制作一款在线多人游戏。一旦两个玩家都准备好了,2 个不同的角色预制件就会生成到一个房间中。问题是每个玩家的设备似乎无法读取其他玩家角色的信息。

它们是使用以下代码生成的:

public class GameplayManager : MonoBehaviour

{

    public PhotonView view;

    public string BlueName;

    public string GreenName;

    public GameObject BluePlayer;

    public GameObject GreenPlayer;

    public Vector2 BlueSpawnPos = new Vector2(4.07f, 0.02f);

    public Vector2 GreenSpawnPos = new Vector2(3.97f, -0.02f);


  public void Start()
    {
        InstantiatePlayers();
        BluePlayer = GameObject.Find("blue soldier(Clone)");
        BlueName = PhotonNetwork.LocalPlayer.NickName;

        GreenPlayer = GameObject.Find("green soldier(Clone)");
        GreenName = PhotonNetwork.LocalPlayer.NickName;

        VictoryText = GameObject.Find("VictoryText").GetComponent<Text>();
    }


    public void InstantiatePlayers()
    {
        //If player 1:
        if (PhotonNetwork.LocalPlayer.ActorNumber == 1)
        {
            BluePlayer = PhotonNetwork.Instantiate(BlueSoldier.name, BlueSpawnPos, Quaternion.identity);
            Debug.Log("bname" + BlueName);
        }

        //if player 2:
        if (PhotonNetwork.LocalPlayer.ActorNumber == 2)
        {
            GreenPlayer = PhotonNetwork.Instantiate(GreenSoldier.name, GreenSpawnPos, Quaternion.identity);
            Debug.Log("gname" + GreenName);
        }
    }

我使用 Unity 编辑器和要测试的构建来运行它。当我在编辑器中检查检查器时,编辑器中颜色字符的“玩家”和“名称”变量已正确更新,但游戏的其他副本的变量为空(即,如果编辑器为蓝色,则“ BlueName”和“BluePlayer”将被更新,但“GreenName”将为空,“GreenPlayer”将为“无”)。

我怀疑这与我使用 if 语句在角色中生成有关,但我不知道是否还有另一种方法可以在多个玩家的多个预制件中生成。

I am making an online multiplayer game. Once both players are ready, 2 DIFFERENT character prefabs are spawned into a room. The problem is that each player's device can't seem to read information about the other player's character.

They're spawned using the following code:

public class GameplayManager : MonoBehaviour

{

    public PhotonView view;

    public string BlueName;

    public string GreenName;

    public GameObject BluePlayer;

    public GameObject GreenPlayer;

    public Vector2 BlueSpawnPos = new Vector2(4.07f, 0.02f);

    public Vector2 GreenSpawnPos = new Vector2(3.97f, -0.02f);


  public void Start()
    {
        InstantiatePlayers();
        BluePlayer = GameObject.Find("blue soldier(Clone)");
        BlueName = PhotonNetwork.LocalPlayer.NickName;

        GreenPlayer = GameObject.Find("green soldier(Clone)");
        GreenName = PhotonNetwork.LocalPlayer.NickName;

        VictoryText = GameObject.Find("VictoryText").GetComponent<Text>();
    }


    public void InstantiatePlayers()
    {
        //If player 1:
        if (PhotonNetwork.LocalPlayer.ActorNumber == 1)
        {
            BluePlayer = PhotonNetwork.Instantiate(BlueSoldier.name, BlueSpawnPos, Quaternion.identity);
            Debug.Log("bname" + BlueName);
        }

        //if player 2:
        if (PhotonNetwork.LocalPlayer.ActorNumber == 2)
        {
            GreenPlayer = PhotonNetwork.Instantiate(GreenSoldier.name, GreenSpawnPos, Quaternion.identity);
            Debug.Log("gname" + GreenName);
        }
    }

I run this using the Unity Editor and a build to test. When I check the inspector in the editor, the 'Player' and 'Name' variables for the colour character in the editor have been updated correctly, but that of the other copy of the game are empty (i.e. if the editor is blue, 'BlueName' and 'BluePlayer' will be updated but 'GreenName' will be empty and 'GreenPlayer' will be 'None').

I suspect it's to do with my using if statements to spawn in characters, but I don't know if there's another way of spawning in multiple prefabs for multiple players.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文