邀请玩家Photon Pun Unity

问题描述 投票:0回答:0

我现在正在尝试制作一款多人游戏,我正在尝试创建一个功能来邀请玩家输入姓名,但是当主持人创建房间时,大厅中的玩家列表没有出现。即使其他玩家输入了名字。我该怎么做?或者我的代码有错误。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Photon.Pun;
using Photon.Realtime;

public class PlayerListPopUp : MonoBehaviour
{
    public Transform playerListContent;
    public GameObject playerPrefab;
    public GameObject playerListPanel;
    public Button openButton;
    // public Button inviteButton;


    // Start is called before the first frame update
    private void Start()
    {
        // Set up button listeners
        openButton.onClick.AddListener(ShowPlayerList);
        // closeButton.onClick.AddListener(HidePlayerList);
        
        // Hide the player list panel at start
        playerListPanel.SetActive(false);
    }

  private void ShowPlayerList()
{   
    playerListPanel.SetActive(true);
    // Clear the player list content
    foreach (Transform child in playerListContent)
    {
        Destroy(child.gameObject);
    }
        if(!PhotonNetwork.IsConnected) return;

    // Add player list items for each player who is not in a room
    foreach (Photon.Realtime.Player player in PhotonNetwork.PlayerList)
    {
        if (player.Room != null) continue; // skip players who are already in a room

        // Add player list items only for players who are in the lobby
    if (player.CustomProperties["status"] != null && player.CustomProperties["status"].ToString() == "lobby")
        {
            Debug.Log("Player NickName: " + player.NickName);
            GameObject item = Instantiate(playerPrefab, playerListContent);
            item.GetComponent<PlayerNameInvite>().SetUp(player);

            // // Add Invite button
            // Button inviteButton = item.transform.Find("InviteButton")?.GetComponent<Button>();
            // if (inviteButton != null)
            // {
            //     inviteButton.onClick.AddListener(() => { OnInviteButtonClick(player); });
            // }
        }
    }
}


     private void OnInviteButtonClick(Player player)
    {
        // Implement your invite functionality here
        Debug.Log("Inviting player: " + player.NickName);
    }
    private void HidePlayerList()
    {
        playerListPanel.SetActive(false);
    }
}


我希望有人能帮助我

unity3d multiplayer photon photon-pun
© www.soinside.com 2019 - 2024. All rights reserved.