# Unity SDK

This guide will walk you through the necessary steps to get started with SendingNetwork requests using the SDK.

## Prerequisites

Before you begin, please ensure that you have the following prerequisites:

* [.NET Standard 2.0](https://docs.microsoft.com/en-us/dotnet/standard/net-standard) or greater installed on your development machine.

## Installation

To install the SDK, you can use either NuGet package manage:

```shell
dotnet add package SDN.Sdk
```

## Use the SDK in your app

### 1. Create

Use `SDNClientFactory` to create an instance of `SDNClient`.

```csharp
var factory = new SDNClientFactory();
ISDNClient client = factory.Create();
```

### 2.Login

Currently, `SDNClient` supports only wallet account login.

```csharp
// pre-login
var preLoginResponse = await client.PreLoginAsync(new Uri(nodeUrl), walletAddress);

// sign message (or prompt user to sign with a wallet app)
var signer = new EthereumMessageSigner();
var signature = signer.EncodeUTF8AndSign(preLoginResponse.Message, new EthECKey(privateKey));

// login did
var loginResponse = await client.LoginAsync(preLoginResponse.Did, preLoginResponse.RandomServer, preLoginResponse.Updated, preLoginResponse.Message, signature);
                userId = loginResponse.UserId;
                accessToken = loginResponse.AccessToken;

// set client credential
client.SetCredential(new Uri(nodeUrl), loginResponse.UserId, loginResponse.AccessToken);
```

### 3. Start listening for incoming events

To listen for the incoming room events you need to subscribe to `OnSDNRoomEventsReceived`.

```csharp
// add event listener
client.OnSDNRoomEventsReceived += (sender, eventArgs) =>
{
    foreach (var roomEvent in eventArgs.SDNRoomEvents)
    {
        if (roomEvent is TextMessageEvent textMessageEvent)
        {
            Console.WriteLine($"RoomId: {textMessageEvent.RoomId} received event from {textMessageEvent.SenderUserId}: {textMessageEvent.Message}.");
        }
    }
};

// start syncing
client.Start();
```

### 4. Call API functions

```csharp
// create new room
await client.CreateRoomAsync(roomName, null);

// invite user to the room
await client.InviteUserAsync(roomId, userId);

// send room message
await client.SendMessageAsync(roomId, "hello");
```

## Examples

For a complete example, refer to *Sample.cs*. You can also clone this repository and run `SDN.Sdk.Sample.Console`.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://sending-network.gitbook.io/sending.network/sdk-documentation/unity-sdk.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
