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.

61 lines
1.6 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Networking;
  5. namespace Networking.Client
  6. {
  7. [CreateAssetMenu(menuName = "Major Project/Networking/ClientObject", order = 150)]
  8. public class ClientObject : ScriptableObject
  9. {
  10. #region Inspector Fields
  11. [Header("Connection Settings")]
  12. [SerializeField]
  13. [Tooltip("Server Address to Connect to")]
  14. private string ServerIP;
  15. [SerializeField]
  16. [Tooltip("Port to connect on")]
  17. private int Port;
  18. [Header("Player Settings")]
  19. [SerializeField]
  20. [Tooltip("Player Name")]
  21. private string DisplayName;
  22. [SerializeField]
  23. [Tooltip("Player Color")]
  24. private Color PlayerColor;
  25. #endregion Inspector Fields
  26. #region ReadOnly Variables
  27. public NetworkClient client { get; private set; }
  28. public bool isConnected { get { return client != null && client.isConnected; } }
  29. #endregion ReadOnly Variables
  30. public void Connect(string serverAddress,int port)
  31. {
  32. Debug.Log("Connecting to server: " + serverAddress + ", " + port);
  33. this.ServerIP = serverAddress;
  34. this.Port = port;
  35. client = new NetworkClient();
  36. client.Connect(serverAddress, port);
  37. }
  38. public void Stop()
  39. {
  40. client.Disconnect();
  41. }
  42. public void UpdatePlayerDetails(string DisplayName, Color PlayerColor)
  43. {
  44. this.DisplayName = DisplayName;
  45. this.PlayerColor = PlayerColor;
  46. }
  47. }
  48. }