extensions.unity.network
v1.4.1
Published
Library for managering Network requests, especially REST API
Downloads
51
Readme
Unity Network REST
REST plugin for client app/game to communicate with single or multiple remote servers using SOLID principles and clean code. Only JSON format is supported for data sending/receiving.
Features
- :white_check_mark: Supported REST requests
- ✔️ GET
- ✔️ POST
- ✔️ PUT
- ✔️ DELETE
- :white_check_mark: JSON serialization/deserialization
- :white_check_mark: Headers control
- :white_check_mark: Requests in dedicated background thread
How to install - Option 1 (RECOMMENDED)
- Install ODIN Inspector
- Install OpenUPM-CLI
- Open command line in Unity project folder
openupm --registry https://registry.npmjs.org add extensions.unity.network
How to install - Option 2
- Install ODIN Inspector
- Add this code to /Packages/manifest.json
{
"dependencies": {
"extensions.unity.network": "1.4.1",
},
"scopedRegistries": [
{
"name": "package.openupm.com",
"url": "https://package.openupm.com",
"scopes": [
"extensions.unity",
"com.cysharp",
"com.neuecc"
]
}
]
}
How to use
STEP 1: Create Server representation as ScriptableObject instance
Create class for representing server, let's call it RemoteServerSO
. Extend the class from NetworkSO
. In Unity Editor press right mouse click in project and create new instance of server representation using menu: Tools/Remote Server
. Select the instance and put into server endpoint. We will use the instance for sending requests to the server.
[CreateAssetMenu(fileName = "RemoteServer", menuName = "Tools/Remote Server", order = 0)]
public class RemoteServerSO : NetworkSO
{
}
STEP 2: Create request
Let's imagine the server by the Enpoint api/data
returns the JSON.
Each unique request in REST API should be represented as C# class. Let's create one GET request as example. Need to override Endpoint
for this specific request.
public class GetDataRequest : RequestGet<Data>
{
protected override string Endpoint => $"api/data";
public GetDataRequest(RemoteServerSO remote) : base(remote) { }
}
Optional data processing
If needed to process received data from the request, need to override OnDataReceived
public class GetDataRequest : RequestGet<Data>
{
protected override string Endpoint => $"api/data";
public GetDataRequest(RemoteServerSO remote) : base(remote) { }
protected override UniTask OnDataReceived(Data data)
{
// doing something with data
return base.OnDataReceived(data);
}
}
STEP 3: Send request
Creating request instance and providing server instance
var request = new GetRemoteConfigs(remoteServer);
Option 1 - just send request
request.SendRequest().Forget();
Option 2 - send request and wait for response with valid data deserialization
var data = (await request.SendRequest()).ResponseData;
Option 3 - send and subscribe on callback
request.SubscribeOnSuccess(data =>
{
// doing something with data
}, this).SendRequest().Forget();
Request callbacks
Subscription should be done before call SendRequest()
// Response received, data successfully serialized from JSON to C# object
request.SubscribeOnSuccess(data =>
{
// doing something with data
}, this);
// Response received, raw JSON data provided
request.SubscribeOnSuccessRaw(rawJson =>
{
// doing something
}, this);
// Response received, JSON can't be deserialized for any reason
request.SubscribeOnSerializationError(rawJson =>
{
// doing something
}, this);
// HTTP error
request.SubscribeOnHttpError(httpError =>
{
// doing something
}, this);
// Network error, related with internet connection, can't reach the server at all
request.SubscribeOnNetworkError(networkError =>
{
// doing something
}, this);
// Progress is float number in range from 0.0f to 1.0f
request.SubscribeOnProgress(progress =>
{
// doing something with data
}, this);
// Request completed with boolean "success" status (true or false)
request.SubscribeOnComplete(success =>
{
// doing something with data
}, this);