ServerSpaceCoordController.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using Rokid.MRC;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Unity.Netcode;
  5. using UnityEngine;
  6. //服务端空间坐标系控制
  7. public class ServerSpaceCoordController : UnitySingleton<ServerSpaceCoordController>
  8. {
  9. SpaceCoordinateBehavior coordinateBehavior;
  10. public bool UseCustomMsg;
  11. //客户端id——相机物体
  12. public Dictionary<ulong, GameObject> clientCameraAnchor;
  13. public GameObject curCameraAnchor;
  14. public override void Awake()
  15. {
  16. base.Awake();
  17. coordinateBehavior = GetComponent<SpaceCoordinateBehavior>();
  18. coordinateBehavior.OnNetworkSpawnHook += OnNetworkSpawn;
  19. coordinateBehavior.OnNetworkDespawnHook += OnNetworkDespawn;
  20. }
  21. private void OnNetworkSpawn()
  22. {
  23. if(UseCustomMsg)
  24. {
  25. ServerMessageDispatcher.Instance.Init();
  26. }
  27. if(NetworkManager.Singleton.IsServer || NetworkManager.Singleton.IsHost)
  28. {
  29. ////只有服务器才能拥有该物体,目前已经加入登录验证,验证过的客户端不会生成PlayerPrefab
  30. //if(!coordinateBehavior.NetworkObject.IsOwnedByServer && !coordinateBehavior.NetworkObject.IsOwner)
  31. //{
  32. // coordinateBehavior.NetworkObject.Despawn();
  33. // Destroy(gameObject);
  34. // return;
  35. //}
  36. }
  37. else
  38. {
  39. enabled = false;
  40. }
  41. }
  42. private void Init()
  43. {
  44. Debug.Log("ServerSpaceCoordController Init");
  45. }
  46. private void OnNetworkDespawn()
  47. {
  48. if(NetworkManager.Singleton.IsServer || NetworkManager.Singleton.IsHost)
  49. {
  50. NetworkManager.Singleton.SceneManager.OnSceneEvent -= OnSceneEvent;
  51. }
  52. }
  53. //某个客户端切换了场景
  54. void OnSceneEvent(SceneEvent sceneEvent)
  55. {
  56. if(sceneEvent.SceneEventType != SceneEventType.LoadComplete)
  57. return;
  58. //发送相关数据给该客户端
  59. }
  60. //客户端校准完成,更新了自己的SpacePose,通知服务端
  61. //服务端修改所有NetworkList数据,同步给客户端,客户端收到数据后,打开录制界面
  62. public void ClientConfirmSpacePose(ulong clientId, SpacePose spacePose)
  63. {
  64. //更新客户端Pose列表
  65. coordinateBehavior.UpdateClientSpacePose(clientId, spacePose);
  66. SpacePose serverSpacePose = default(SpacePose);
  67. if(spacePose.CalibrationMode == ARCalibrationMode.FaceTrack)
  68. {
  69. //更新服务端Pose列表
  70. serverSpacePose = new SpacePose(clientId, Camera.main.transform.position, Camera.main.transform.rotation, spacePose.CalibrationMode);
  71. }
  72. else if(spacePose.CalibrationMode == ARCalibrationMode.SelfCalibration)
  73. {
  74. serverSpacePose = new SpacePose(clientId, MRCDriver.Instance.SelfCalibrationPhone.position, MRCDriver.Instance.SelfCalibrationPhone.rotation, spacePose.CalibrationMode);
  75. }
  76. coordinateBehavior.UpdateServerSpacePose(clientId, serverSpacePose);
  77. //通知构建提示界面关闭
  78. MessageCenter.SendMessage(GlobalDefine.SpaceBuildEnd, null);
  79. }
  80. public void ClientStartBuild(ulong clientID, ARCalibrationMode calibrationMode)
  81. {
  82. if(calibrationMode == ARCalibrationMode.FaceTrack)
  83. {
  84. //TODO,暂时先用Confirm界面来提示
  85. UIManager.Instance.ShowConfirm(
  86. LocalizationMgr.Instance.GetTextByKey("BuildTitle"),
  87. string.Format(LocalizationMgr.Instance.GetTextByKey("BuildReq"), PlayerManager.Instance.GetPlayerData(clientID)?.PlayerName),
  88. ConfirmType.None);
  89. }
  90. else if(calibrationMode == ARCalibrationMode.SelfCalibration)
  91. {
  92. //自构建,创建手机模型
  93. ResourceManager.Instance.CreatePhoneAnchor();
  94. }
  95. }
  96. public void ClientCancelBuild()
  97. {
  98. UIManager.Instance.ClosePanel(UIType.Confirm);
  99. }
  100. //某个客户端同步相机Pose
  101. private void OnClientUpdateCameraPose(ulong clientId, SpacePose spacePose)
  102. {
  103. //curCameraAnchor.transform
  104. }
  105. }