Advertisement
Guest User

Untitled

a guest
Nov 28th, 2022
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using PlayFab;
  5. using PlayFab.MultiplayerAgent.Model;
  6.  
  7. public class ServerStartUp : MonoBehaviour
  8. {
  9. [SerializeField] private BuildType buildType;
  10.  
  11. private List<ConnectedPlayer> connectedPlayers;
  12.  
  13. void Start()
  14. {
  15. if(buildType.chosenBuild == BuildType.Build.RemoteServer) {
  16. StartRemoteServer();
  17. }
  18. }
  19.  
  20. private void StartRemoteServer() {
  21. Debug.Log("This is a test log. Please ignore.");
  22. PlayFabMultiplayerAgentAPI.Start();
  23. PlayFabMultiplayerAgentAPI.IsDebugging = buildType.debugBuild;
  24. PlayFabMultiplayerAgentAPI.OnShutDownCallback += OnShutdown;
  25. PlayFabMultiplayerAgentAPI.OnServerActiveCallback += OnServerActive;
  26. PlayFabMultiplayerAgentAPI.OnAgentErrorCallback += OnAgentError;
  27. NetworkManagerLobby.Instance.StartServer();
  28. Debug.Log("Server started");
  29. NetworkManagerLobby.Instance.OnPlayerAdded.AddListener(OnPlayerAdded);
  30. NetworkManagerLobby.Instance.OnPlayerRemoved.AddListener(OnPlayerRemoved);
  31.  
  32. StartCoroutine(ReadyForPlayers());
  33. }
  34.  
  35. IEnumerator ReadyForPlayers() {
  36. yield return new WaitForSeconds(1f);
  37. PlayFabMultiplayerAgentAPI.ReadyForPlayers();
  38. }
  39.  
  40. private void OnServerActive() {
  41. //TODO: cos do zrobienia po przejsciu serwera na active (jak otrzymuje graczy, startserver musi byc called wczesniej)
  42. }
  43.  
  44. private void OnPlayerRemoved(string playfabId) {
  45. ConnectedPlayer player = connectedPlayers.Find(x => x.PlayerId.Equals(playfabId, System.StringComparison.OrdinalIgnoreCase));
  46. connectedPlayers.Remove(player);
  47. PlayFabMultiplayerAgentAPI.UpdateConnectedPlayers(connectedPlayers);
  48. }
  49.  
  50. private void OnPlayerAdded(string playfabId) {
  51. connectedPlayers.Add(new ConnectedPlayer(playfabId));
  52. PlayFabMultiplayerAgentAPI.UpdateConnectedPlayers(connectedPlayers);
  53. }
  54.  
  55. private void OnAgentError(string error) {
  56. Debug.Log(error);
  57. }
  58.  
  59. private void OnShutdown() {
  60. Debug.Log("Server shutting down");
  61. StartCoroutine(Shutdown());
  62. }
  63.  
  64. IEnumerator Shutdown() {
  65. yield return new WaitForSeconds(5f);
  66. Application.Quit();
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement