You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.3 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Networking;
  5. namespace Multiplayer
  6. {
  7. public class ClientManager : MonoSingleton<ClientManager>
  8. {
  9. #region Inspector Variables
  10. [Header("Client Settings")]
  11. [Tooltip("Address server is hosted on")]
  12. [SerializeField]
  13. private string ServerAddress = "127.0.0.1";
  14. [Tooltip("Port to connect to server on")]
  15. [SerializeField]
  16. private int Port = 4444;
  17. [Tooltip("Connects client to server on awake")]
  18. [SerializeField]
  19. private bool ConnectOnStart = false;
  20. public string LobbyScene;
  21. public string GameScene;
  22. public string Name;
  23. #endregion
  24. //Returns if client is current connected to a server
  25. public bool IsConnected;
  26. public byte ID;
  27. //UNET client class which connects to the server;
  28. public NetworkClient Client { get; private set;}
  29. public ClientLoginManager loginManager;
  30. /// <summary>
  31. /// Call to connect client to host
  32. /// </summary>
  33. [ContextMenu("Start Client")]
  34. public void StartClient()
  35. {
  36. StartClient(ServerAddress, Port);
  37. }
  38. /// <summary>
  39. /// Call to connect the client to the host
  40. /// </summary>
  41. /// <param name="ServerAddress">Server Address to connect to</param>
  42. /// <param name="Port">Port to connect on</param>
  43. public void StartClient(string ServerAddress, int Port)
  44. {
  45. //If client is already connected to don't continue
  46. if (IsConnected)
  47. {
  48. Debug.Log("Client already connected to a server. Client needs to disconnect before it can connect to a server");
  49. return;
  50. }
  51. Debug.Log("Attempting to connect to: '" + ServerAddress + "' on port : '" + Port + "'");
  52. Client = new NetworkClient();
  53. loginManager = new ClientLoginManager(this);
  54. Client.Configure(ChannelConfig.DefaultTopology());
  55. Client.RegisterHandler(PlayerMsgID.Lobby, StartLobby);
  56. Client.Connect(ServerAddress, Port);
  57. }
  58. /// <summary>
  59. /// Sends message to server
  60. /// </summary>
  61. /// <param name="msgType">Message type, used to determin message handler</param>
  62. /// <param name="msg">The message to send</param>
  63. /// <param name="channel">Which channel to send on, by deafult sends on Reliable</param>
  64. /// <param name="BackLog">If client isn't connected to server add to BackLog, by default true</param>
  65. public void SendMessage(short msgType, MessageBase msg, ServerChannel channel = ServerChannel.Reliable)
  66. {
  67. //if client isn't connected add to backlog
  68. if (!IsConnected)
  69. Debug.Log("Player not connected to server");
  70. Debug.Log("Sending message to server");
  71. Client.SendByChannel(msgType, msg, (int)channel);
  72. }
  73. public void SendMessage(short msgType)
  74. {
  75. SendMessage(msgType, new PlayerMsg(ID));
  76. }
  77. public void StartLobby(NetworkMessage msg)
  78. {
  79. UnityEngine.SceneManagement.SceneManager.LoadScene(LobbyScene);
  80. }
  81. public void StartGame(NetworkMessage msg)
  82. {
  83. }
  84. }
  85. }