OfflineState.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System;
  2. using Unity.Multiplayer.Samples.Utilities;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5. //离线状态,默认初始状态
  6. public class OfflineState : ConnectionState
  7. {
  8. public override void Enter()
  9. {
  10. //关闭连接
  11. m_ConnectionManager.NetworkManager.Shutdown();
  12. }
  13. public override void Exit()
  14. {
  15. }
  16. //启动客户端,切换到ClientConnectingState状态。playerName为客户端名称(建议SystemInfo.deviceName设备名称)
  17. public override void StartClientIP(string ipaddress, int port)
  18. {
  19. //设置客户端账号,IP、端口
  20. Debug.Log("OfflineState StartClientIP");
  21. //设置IP
  22. m_ConnectionManager.ConnectionMethod.SetIPData(ipaddress, (ushort)port);
  23. //设置信息
  24. m_ConnectionManager.m_ClientReconnecting.Configure();
  25. m_ConnectionManager.m_ClientConnecting.Configure();
  26. //切换到连接状态
  27. m_ConnectionManager.ChangeState(m_ConnectionManager.m_ClientConnecting);
  28. }
  29. //启动服务端,切换到StartingHostState状态
  30. public override void StartHostIP(string ipaddress, int port)
  31. {
  32. Debug.Log("OfflineState StartHostIP");
  33. m_ConnectionManager.ConnectionMethod.SetIPData(ipaddress, (ushort)port);
  34. m_ConnectionManager.m_StartingHost.Configure();
  35. m_ConnectionManager.ChangeState(m_ConnectionManager.m_StartingHost);
  36. }
  37. }