Presentation is loading. Please wait.

Presentation is loading. Please wait.

Character Selection from a lobby in Unity

Similar presentations


Presentation on theme: "Character Selection from a lobby in Unity"— Presentation transcript:

1 Character Selection from a lobby in Unity

2 1) Set up 2) Add Overview 3) Add 4) Update
Set up lobby scene and change Lobby Player Prefab Overview 2) Add Add code to LobbyPlayer.cs 3) Add Add code to LobbyManager.cs 4) Update Inspector slots for lobby and lobby player

3 Set up Lobby scene and lobby Player Prefab
1 Set up Lobby scene and lobby Player Prefab Add Duplicate scene, import lobby from asset store, drag in lobby prefab to new scene, add scene to build Add Duplicate the Lobby Player prefab – add to scene Add Add the three Player Avatar buttons – apply and delete from scene Modify Set up the Lobby Manager Inspector with the scenes and the game player prefab and other settings Move Move/shrink the name field, drag buttons into place

4 Add the three Player Avatar buttons to the Lobby Player Prefab
1 Add the three Player Avatar buttons to the Lobby Player Prefab Make a copy of the lobby player prefab (edit – duplicate) Drag copy into scene Drag avatar buttons into prefab Apply Delete scene version

5 Update Inspector for Lobby Manager Prefab
1 Update Inspector for Lobby Manager Prefab Add Scenes Add New LobbyPlayer Prefab And default Avatar

6 Change the layout of the existing Elements in the player list
1 Change the layout of the existing Elements in the player list Start the lobby in play mode Switch over to scene tab Make the name field smaller Copy the rect transform component Leave play mode Paste component values into rect transform in prefab Repeat for buttons

7 Add code to LobbyPlayer.cs
2 Add code to LobbyPlayer.cs Buttons and Avatar Index Variables For each button – call the avatar selection method Button Listeners Case statement and rpc/command call Avatar Selection Method to change a line in a dictionary in Lobby Manager Rpc / Command

8 LobbyPlayer.cs code changes - variables
2 LobbyPlayer.cs code changes - variables public Button player1Button; public Button player2Button; public Button player3Button; public int avatarIndex = 5;

9 LobbyPlayer.cs code changes – Button Listeners
2 LobbyPlayer.cs code changes – Button Listeners Inside SetupLocalPlayer() player1Button.onClick.AddListener  (delegate {AvatarPicker  (player1Button.name);}); player2Button.onClick.AddListener  (delegate {AvatarPicker  (player2Button.name);});              player3Button.onClick.AddListener  (delegate {AvatarPicker  (player3Button.name);});

10 LobbyPlayer.cs code changes – AvatarPicker()
2 LobbyPlayer.cs code changes – AvatarPicker() void AvatarPicker(string buttonName) { switch (buttonName) case "Player1": avatarIndex = 0; break; case "Player2": avatarIndex = 1; case "Player3": avatarIndex = 2; } if (isServer) RpcAvatarPicked (avatarIndex); else CmdAvatarPicked (avatarIndex); I added this directly after setuplocalplayer()

11 LobbyPlayer.cs code changes – Rpc / CmdAvatarpicked
2 LobbyPlayer.cs code changes – Rpc / CmdAvatarpicked [ClientRpc] public void RpcAvatarPicked(int avIndex) { CmdAvatarPicked (avIndex); } [Command] public void CmdAvatarPicked(int avIndex) { LobbyManager.s_Singleton.SetPlayerTypeLobby(GetComponent<NetworkIdentity>().connectionToClient, avIndex); } I added this directly after avatarpicker()

12 Add code to LobbyManager.cs
3 Add code to LobbyManager.cs Set up and initialize dictionary Variables Add dictionary entry for each lobby player in OnLobbyServerCreateLobbyPlayer Modify Method SetPlayerTypeLobby – called by Lobby player to set the avatar index to the correct dictionary entry Add Method Override OnLobbyServerCreateGamePlayer - called when the lobby player is being turned into the game player – pass the correct avatar from spawnprefabs list here.

13 LobbyManager.cs code changes - Dictionary
3 LobbyManager.cs code changes - Dictionary Add to class: public Dictionary<int, int> currentPlayers; Add to Start() method: currentPlayers = new Dictionary<int, int>();

14 3 LobbyManager.cs code changes – Add dictionary entry for each lobby player if(!currentPlayers.ContainsKey(conn.connectionId)) currentPlayers.Add(conn.connectionId, 0);

15 LobbyManager.cs code changes – Add Method SetPlayerTypeLobby()
3 LobbyManager.cs code changes – Add Method SetPlayerTypeLobby() public void SetPlayerTypeLobby (NetworkConnection conn, int type)         {             if (currentPlayers.ContainsKey (conn.connectionId))                 currentPlayers [conn.connectionId] = type;         } I added this right after OnLobbyServerCreateLobbyPlayer()

16 LobbyManager.cs code changes – Add Method SetPlayerTypeLobby()
3 LobbyManager.cs code changes – Add Method SetPlayerTypeLobby() public override GameObject OnLobbyServerCreateGamePlayer(NetworkConnection conn, short playerControllerId)         {             int index = currentPlayers[conn.connectionId];                 GameObject playerPrefab = (GameObject)GameObject.Instantiate(spawnPrefabs[index], startPositions[conn.connectionId].position,Quaternion.identity);             return playerPrefab;         } I added this right after SetPlayerTypeLobby()

17 Update Inspector for Lobby Manager Prefab
4 Update Inspector for Lobby Manager Prefab Add New Avatar prefabs

18 Update Inspector for Lobby Player Prefab
4 Update Inspector for Lobby Player Prefab Add Player Button Prefabs

19 Thanks For Watching! Thanks to the blog entry on Les Jeux Videos: player.html And to Khalil and Ajay for watching my tutorials and leading me to the above blog post.


Download ppt "Character Selection from a lobby in Unity"

Similar presentations


Ads by Google