https://github.com/insthync/ngorequestresponse
A Request and response addon for Unity's Netcode for Game Object
https://github.com/insthync/ngorequestresponse
Last synced: about 2 months ago
JSON representation
A Request and response addon for Unity's Netcode for Game Object
- Host: GitHub
- URL: https://github.com/insthync/ngorequestresponse
- Owner: insthync
- License: mit
- Created: 2022-12-09T02:00:11.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-12-09T02:27:17.000Z (over 3 years ago)
- Last Synced: 2025-12-26T20:14:08.389Z (6 months ago)
- Language: C#
- Size: 15.6 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# NGOReqRes
A Request and response addon for Unity's [Netcode for Game Objects](https://github.com/Unity-Technologies/com.unity.netcode.gameobjects)
## How to use it
Attach `RequestResponseManager` to any game object.
Register requests by uses functions from `RequestResponseManager` class, example:
```
// Request Data
using Unity.Netcode;
public struct TestReq : INetworkSerializable
{
public int a;
public int b;
public void NetworkSerialize(BufferSerializer serializer) where T : IReaderWriter
{
serializer.SerializeValue(ref a);
serializer.SerializeValue(ref b);
}
}
// Response Data
using Unity.Netcode;
public struct TestRes : INetworkSerializable
{
public int c;
public void NetworkSerialize(BufferSerializer serializer) where T : IReaderWriter
{
serializer.SerializeValue(ref c);
}
}
// Register class, attach this to the same game object with `RequestResponseManager`
using Unity.Netcode.Insthync.ResquestResponse;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestRequestResponse : MonoBehaviour
{
public RequestResponseManager reqResMgr;
public int a = 0;
public int b = 0;
public bool sendUpdate;
private void Start()
{
reqResMgr.RegisterRequestToServer(1, RequestHandler, ResponseHandler);
}
public void Update()
{
if (sendUpdate)
{
sendUpdate = false;
reqResMgr.ClientSendRequest(1, new TestReq()
{
a = a,
b = b,
});
}
}
void RequestHandler(RequestHandlerData requestHandler, TestReq request, RequestProceedResultDelegate result)
{
Debug.Log("Request: " + request.a + ", " + request.b);
result.Invoke(AckResponseCode.Success, new TestRes()
{
c = request.a + request.b
});
}
void ResponseHandler(ResponseHandlerData requestHandler, AckResponseCode responseCode, TestRes response)
{
Debug.Log("Response: " + responseCode + " = " + response.c);
}
}
```