Become any tier member on my Patreon below for the source files!Would you like to help me grow? There are a variety of ways you can support me here: http://f. How to make a Multiplayer Video Games in Unity using the Photon 2 pluginPhoton Documentaiton: https://doc.photonengine.com/en-us/pun/v2/getting-started/pun-i. PhotonNetwork.autoJoinLobby = false; // #Critical // this makes sure we can use PhotonNetwork.LoadLevel on the master client and all clients in the same room sync their level automatically PhotonNetwork.automaticallySyncScene = true; /// /// MonoBehaviour method called on GameObject by Unity during initialization phase. PhotonNetwork.playerName = MainMenu.nickName; We want to display the name of other players in the game above their tanks, so to do this I’ve added a canvas and UIText object to the player prefab to act as a name label.
Get the Photon Friend List package from Lovatto Studio and speed up your game development process. Find this & other Network options on the Unity Asset Store.
- using Photon.Realtime;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerListingsMenu : MonoBehaviourPunCallbacks, IConnectionCallbacks
- [SerializeField]
- private PlayerListing _playerListing;
- [SerializeField]
- private List<PlayerListing> _Listings = new List<PlayerListing>();
- private bool _ready = false;
- public void FirstInitialize(RoomsCanvases canvases)
- _roomsCanvases = canvases;
- {
- }
- private void SetReadyUp(bool state)
- _ready = state;
- {
- }
- {
- }
- }
- public override void OnLeftRoom()
- _content.DestroyChildren();
- {
- {
- }
- if (PhotonNetwork.CurrentRoom null || PhotonNetwork.CurrentRoom.Players null)
- return;
- foreach (KeyValuePair<int, Player> playerInfo in PhotonNetwork.CurrentRoom.Players)
- AddPlayerListing(playerInfo.Value);
- }
- private void AddPlayerListing(Player player)
- {
- if (child.gameObject.GetComponent<PlayerListing>()._text.text.Equals(player.NickName))
- return;
- }
- PlayerListing listing = Instantiate(_playerListing, _content);
- if (listing != null)
- listing.SetPlayerInfo(player);
- _Listings.Add(listing);
- }
- ReorderPlayers();
- public override void OnMasterClientSwitched(Player newMasterClient)
- _roomsCanvases.CurrentRoomCanvas.LeaveRoomMenu.OnClick_LeaveRoom();
- public override void OnPlayerEnteredRoom(Player newPlayer)
- AddPlayerListing(newPlayer);
- public override void OnPlayerLeftRoom(Player otherPlayer)
- int index = _Listings.FindIndex(x => x.Player otherPlayer);
- {
- _Listings.RemoveAt(index);
- }
- private void ReorderPlayers()
- List<Transform> _ToReorder = new List<Transform>();
- foreach (Transform child in _content)
- _ToReorder.Add(child);
- List<Transform> _ToReorder2 =_ToReorder.OrderBy(o=>o.gameObject.gameObject.GetComponent<PlayerListing>()._text.text).ToList();
- int I = 0;
- foreach (Transform transformInList in _ToReorder2)
- transformInList.SetSiblingIndex(I);
- }
- }
- public void OnClick_StartGame()
- if (PhotonNetwork.IsMasterClient)
- for (int i = 0; i < _Listings.Count; i++)
- if (_Listings[i].Player != PhotonNetwork.LocalPlayer)
- if (!_Listings[i].Ready)
- return;
- }
- PhotonNetwork.CurrentRoom.IsVisible = false;
- }
- public void OnClick_ReadyUp()
- if (!PhotonNetwork.IsMasterClient)
- SetReadyUp(!_ready);
- base.photonView.RPC('RPC_ChangeReadyState',RpcTarget.MasterClient, PhotonNetwork.LocalPlayer, _ready);
- //base.photonView.RpcSecure('RPC_ChangeReadyState',RpcTarget.MasterClient, true, PhotonNetwork.LocalPlayer, _ready);
- }
- [PunRPC]
- private void RPC_ChangeReadyState(Player player, bool ready)
- int index = _Listings.FindIndex(x => x.Player player);
- {
- }
- }