Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.47 KB | None | 0 0
  1. using Unity.Entities;
  2. using Unity.NetCode;
  3. using Unity.Networking.Transport;
  4. using Unity.Burst;
  5. using UnityEngine;
  6.  
  7. [UpdateInGroup(typeof(ServerSimulationSystemGroup))]
  8. public class IntRpcReceiver : SystemBase
  9. {
  10.   protected override void OnUpdate()
  11.   {
  12.     var system = World.GetOrCreateSystem<BeginInitializationEntityCommandBufferSystem>();
  13.     var buffer = system.CreateCommandBuffer();
  14.  
  15.     // On the server, go through ever entity with a 'IntRpc' that we
  16.     // are _not_ sending
  17.     Entities.WithNone<SendRpcCommandRequestComponent>().ForEach((Entity entity, in IntRpc request, in ReceiveRpcCommandRequestComponent requestSource) => {
  18.         Debug.Log(request.message);
  19.  
  20.         // Once we process the message, delete it
  21.         buffer.DestroyEntity(entity);
  22.     }).WithoutBurst().Run();
  23.  
  24.     system.AddJobHandleForProducer(this.Dependency);
  25.   }
  26. }
  27.  
  28. [UpdateInGroup(typeof(ClientSimulationSystemGroup))]
  29. public class IntRpcSender : RpcCommandRequestSystem<IntRpc>
  30. {
  31.   // This class doesn't need a body. The parent class takes
  32.   // care of transmitting 'IntRpc's.
  33. }
  34.  
  35. [BurstCompile]
  36. public struct IntRpc : IRpcCommand
  37. {
  38.     public int message;
  39.  
  40.     // All this serialization stuff seems like boilerplate, but
  41.     // I don't know how to get rid of it yet.
  42.     public void Deserialize(ref DataStreamReader reader)
  43.     {
  44.       message = reader.ReadInt();
  45.     }
  46.  
  47.     public void Serialize(ref DataStreamWriter writer)
  48.     {
  49.       writer.WriteInt(message);
  50.     }
  51.  
  52.     [BurstCompile]
  53.     private static void InvokeExecute(ref RpcExecutor.Parameters parameters)
  54.     {
  55.         RpcExecutor.ExecuteCreateRequestComponent<IntRpc>(ref parameters);
  56.     }
  57.  
  58.     static PortableFunctionPointer<RpcExecutor.ExecuteDelegate>
  59.       InvokeExecuteFunctionPointer =
  60.       new PortableFunctionPointer<RpcExecutor.ExecuteDelegate>(InvokeExecute);
  61.  
  62.     public PortableFunctionPointer<RpcExecutor.ExecuteDelegate> CompileExecute()
  63.     {
  64.         return InvokeExecuteFunctionPointer;
  65.     }
  66. }
  67.  
  68. /* Sending the RPC:
  69.   var request = EntityManager.CreateEntity();
  70.   EntityManager.AddComponent<IntRpc>(request);
  71.   EntityManager.AddComponent<SendRpcCommandRequestComponent>(request);
  72.   EntityManager.SetComponentData(request, new SendRpcCommandRequestComponent {
  73.     TargetConnection = // The entity to send the RPC to. Must have a 'NetworkIdComponent' component
  74.   });
  75.   EntityManager.SetComponentData(request, new IntRpc {
  76.     message = 123, // The actual payload of the RPC
  77.   });
  78. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement