MRCCommonBehavior.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using Unity.Netcode;
  5. using UnityEngine;
  6. using UnityEngine.Video;
  7. //空间坐标系同步
  8. public class MRCCommonBehavior : NetworkBehaviour
  9. {
  10. //位置和旋转
  11. private NetworkVariable<Vector3> spacePos;
  12. private NetworkVariable<Quaternion> spaceRot;
  13. public void SyncSpaceCordinateServer(Vector3 pos, Quaternion rot)
  14. {
  15. spacePos.Value = pos;
  16. spaceRot.Value = rot;
  17. }
  18. public void SyncSpaceCordinateClient()
  19. {
  20. //同步
  21. }
  22. //生成物体
  23. public override void OnNetworkSpawn()
  24. {
  25. base.OnNetworkSpawn();
  26. if(!NetworkManager.Singleton.IsClient)
  27. {
  28. enabled = false;
  29. }
  30. else
  31. {
  32. NetworkManager.OnClientConnectedCallback += OnClientConnected;
  33. NetworkManager.OnClientDisconnectCallback += OnClientDisconnected;
  34. }
  35. }
  36. private void OnClientConnected(ulong clientId)
  37. {
  38. }
  39. private void OnClientDisconnected(ulong clientId)
  40. {
  41. }
  42. //删除物体
  43. public override void OnNetworkDespawn()
  44. {
  45. base.OnNetworkDespawn();
  46. }
  47. //获得所有权
  48. public override void OnGainedOwnership()
  49. {
  50. base.OnGainedOwnership();
  51. }
  52. //失去所有权
  53. public override void OnLostOwnership()
  54. {
  55. base.OnLostOwnership();
  56. }
  57. //更改了该物体的父物体
  58. public override void OnNetworkObjectParentChanged(NetworkObject parentNetworkObject)
  59. {
  60. base.OnNetworkObjectParentChanged(parentNetworkObject);
  61. }
  62. public void TestSend()
  63. {
  64. //向某个客户端发送消息
  65. //var message = new DestroyObjectMessage
  66. //{
  67. // NetworkObjectId = NetworkObjectId,
  68. // DestroyGameObject =
  69. //};
  70. //NetworkManager.Singleton.SendMessage(ref message, NetworkDelivery.ReliableSequenced, clientId);
  71. }
  72. }