# Unity SDK

## システム要件 <a href="#undefined" id="undefined"></a>

Game Chat Unity SDKを使用するためのシステム要件は、以下のとおりです。

* 最小スペック: 2018.4.0以上\
  (下位バージョンの Unityへの対応が必要な場合、<cs@nbase.io> までご連絡ください。)
* 2019.4.X / 2020.3.X / 2021.1.Xバージョンの Unityエディタのユーザーは、2019.4.29f1以上 / 2020.3.15f2以上 / 2021.1.16f1以上のバージョンを使用します(AABバージョンビルド時の Unityエディタのバグが変更されたバージョン)

## SDKのインストールと環境の構成 <a href="#sdk" id="sdk"></a>

Game Chat Unity SDKをダウンロードして、Unityでプロジェクトを構成する方法は以下のとおりです。

1. **設定** > **SDKのダウンロード**メニューを順にクリックし、**Unity SDKのダウンロード**をクリックします。
2. Unityプログラムを実行し、プロジェクトを作成します。
3. Unityで、**Assets** > **Import Package** > **Custom Package**...メニューを順にクリックします。
4. ダッシュボードでダウンロードした「GameChatUnitySDK\_xxxxxxxx」ファイルを読み込みます。
5. パッケージにあるすべてのファイルを選択し、 **\[Import]** ボタンをクリックします。
6. プロジェクトを保存します。

## 認証 <a href="#undefined" id="undefined"></a>

### Game Chatインスタンスの初期化 <a href="#gamechat" id="gamechat"></a>

Game Chatプロジェクト IDを用いて Game Chatインスタンスを初期化するには、以下のコードを使用します。

```csharp
GameChat.initialize(PROJECT_ID);

// シンガポールリージョンを使用する場合
GameChat.setRegion("sg");
GameChat.initialize(PROJECT_ID);
```

<table><thead><tr><th width="167">ID</th><th width="146">type</th><th>desc</th></tr></thead><tbody><tr><td>PROJECT_ID</td><td>string</td><td>プロジェクト ID</td></tr></tbody></table>

### Game Chatソケットサーバとの接続

Game Chatソケットサーバに接続する方法は以下のとおりです。

1. チャットユーザー IDを用いてGame Chatソケットサーバにアクセスします。
   * Game Chatプロジェクトでチャットユーザー IDは固有の値です。
2. APIを使用するためのトークンの値を取得します。
   * GameChat.connect以降に更新されたトークンの値を確認できます。
3. トークンの値を取得した後、現在アクセス中のデバイスに対するチャットユーザー情報が更新されたのか確認します。
   * GameChat.connectのコールバックで渡される Memberは、更新されたデータです。

Game Chatソケットサーバに接続するには、以下のコードを使用します。

```csharp
GameChat.connect(USER_ID,  (Member User, GameChatException Exception)=>
{

    if(Exception != null)
    {
        // エラー処理
        return;
    }
});
```

<table><thead><tr><th width="190">ID</th><th width="136">type</th><th>desc</th></tr></thead><tbody><tr><td>USER_ID</td><td>string</td><td>チャットユーザーの固有 ID</td></tr></tbody></table>

### Game Chatサーバとの接続解除 <a href="#gamechat" id="gamechat"></a>

Game Chatソケットサーバとの接続を解除するには、以下のコードを使用します。

```csharp
GameChat.disconnect();
```

### チャットユーザー情報のアップデート

connect成功後にチャットユーザー情報を保存してアップデートするには、以下のコードを使用します。

#### **ハンドルネームの変更**

```csharp
GameChat.setNickname(USER_ID, NickName, (member, exception) =>
{
    if (exception != null)
    {
        // エラー処理
        return;
    }

});
```

<table><thead><tr><th width="175">ID</th><th width="123">type</th><th>desc</th></tr></thead><tbody><tr><td>USER_ID</td><td>string</td><td>チャットユーザーの固有 ID</td></tr><tr><td>NickName</td><td>string</td><td>チャットユーザーのハンドルネーム</td></tr></tbody></table>

#### **Profile URLの変更**

```csharp
GameChat.setProfileUrl(USER_ID, ProfileUrl, (member, exception) =>
{
    if (exception != null)
    {
        // エラー処理
        return;
    }

});
```

<table><thead><tr><th width="187">ID</th><th width="159">type</th><th>desc</th></tr></thead><tbody><tr><td>USER_ID</td><td>string</td><td>チャットユーザーの固有 ID</td></tr><tr><td>ProfileUrl</td><td>string</td><td>チャットユーザーの ProfileURL</td></tr></tbody></table>

## チャンネルの購読と購読解除

特定のチャンネルに対して購読または購読解除を行うには、以下のコードを使用します。

```csharp
GameChat.subscribe(CHANNEL_ID);

GameChat.unsubscribe(CHANNEL_ID);
```

<table><thead><tr><th width="217">ID</th><th width="150">type</th><th>desc</th></tr></thead><tbody><tr><td>CHANNEL_ID</td><td>string</td><td>チャンネル ID</td></tr></tbody></table>

## メッセージの送信 <a href="#undefined" id="undefined"></a>

特定のチャンネルにメッセージを送信するには、以下のコードを使用します。

```csharp
GameChat.sendMessage(CHANNEL_ID, MESSAGE);
```

<table><thead><tr><th width="197">ID</th><th width="156">type</th><th>desc</th></tr></thead><tbody><tr><td>CHANNEL_ID</td><td>string</td><td>チャンネル ID</td></tr><tr><td>MESSAGE</td><td>string</td><td>送信メッセージテキスト</td></tr></tbody></table>

MESSAGEパラメータに@\[ユーザー ID]空白\[メッセージ内容]を入力時

```csharp
@user_idメッセージ本文
```

上記のケースでユーザー IDがログインした履歴がある場合、メッセージの詳細情報のうち、mentions情報はユーザー IDです。

## イベントの登録と解除

Game Chatソケットサーバから受信するイベントに対し、カスタムハンドラを登録または解除するには、以下のコードを使用します。

```csharp
GameChat.dispatcher.(EVENT_NAME) += (CALLBACK_FUNCTION);
```

```csharp
public delegate void onConnectedCallback(string data);
public onConnectedCallback onConnected;
//「connect」Eventに対する、コールバック

public delegate void onDisconnectedCallback(string reason);
public onDisconnectedCallback onDisconnected;
//「disconnect」Eventに対する、コールバック

public delegate void onMessageReceivedCallback(Message message);
public onMessageReceivedCallback onMessageReceived;
//「message」Eventに対する、コールバック

public delegate void onUserAddedCallback(UserInfo userinfo);
public onUserAddedCallback onUserAdded;
//「userAdded」Eventに対する、コールバック

public delegate void onUserRemovedCallback(Message message);
public onUserRemovedCallback onUserRemoved;
//「userRemoved」Eventに対する、コールバック

public delegate void onErrorReceivedCallback(string result, GameChatException exception);
public onErrorReceivedCallback onErrorReceived;
//「error」Eventに対する、コールバック
```

## 例外事項

Game Chat APIの使用中に発生する Exceptionに対する共通処理 Classは、以下のとおりです。

```csharp
public class GameChatException
{
    // Detail Error Code

    // 不明なエラー
    public static readonly int CODE_UNKNOWN_ERROR           = 0;
    // 初期化に失敗
    public static readonly int CODE_NOT_INITALIZE           = 1;
    // パラメータが正しくない場合
    public static readonly int CODE_INVAILD_PARAM           = 2;
    // ソケットサーバから発生したエラー
    public static readonly int CODE_SOCKET_SERVER_ERROR     = 500;
    //ソケットから発生したエラー
    public static readonly int CODE_SOCKET_ERROR = -501;
    // ネットワークコネクションエラーおよびタイムアウトが発生した場合
    public static readonly int CODE_SERVER_NETWORK_ERROR    = 4002;
    // サーバから取得したデータをパースする際のエラー
    public static readonly int CODE_SERVER_PARSING_ERROR    = 4003;

    // HTTPエラーの場合、当該ステータスコードがレスポンスコードとして伝達されます。 (400, 403 ...)

    // Error Code
    public int code { get; set; }
    // Error Message
    public string message { get; set; }
}
```

## Client API

### チャンネルの購読 <a href="#undefined" id="undefined"></a>

#### **Subscription Data Class (per Unit)**

```csharp
public class Subscription
{
    public string id;
    public string channel_id;
    public string user_id;
    public string created_at;
}
```

<table><thead><tr><th width="213">ID</th><th width="154">type</th><th>desc</th></tr></thead><tbody><tr><td>id</td><td>string</td><td>固有 ID</td></tr><tr><td>channel_id</td><td>string</td><td>チャンネル ID</td></tr><tr><td>user_id</td><td>string</td><td>チャットユーザーの固有 ID</td></tr><tr><td>created_at</td><td>string</td><td>作成日時</td></tr></tbody></table>

#### **チャンネル購読リストを取得する**

特定のチャンネルの購読データをリスト形式で取得するには、以下のコードを使用します。

```csharp
GameChat.getSubscriptions(CHANNEL_ID, OFFSET, LIMIT, (List<Subscription> Subscriptions, GameChatException Exception) => {

    if(Exception != null)
    {
        // エラー処理
        return;
    }

    foreach(Subscription elem in Subscriptions)
    {
        //handling each subscription instance
    }
}));
```

### チャンネル

**Channel Data Class (per Unit)**

```csharp
public class Channel
{
    public string id;
    public string project_id;
    public string unique_id;
    public string name;
    public string user_id;
    public string created_at;
    public string updated_at;
}
```

<table><thead><tr><th width="202">ID</th><th width="128">type</th><th>desc</th></tr></thead><tbody><tr><td>id</td><td>string</td><td>チャンネル ID(固有)</td></tr><tr><td>project_id</td><td>string</td><td>プロジェクト ID</td></tr><tr><td>unique_id</td><td>string</td><td>開発会社で設定できるチャンネル ID(固有)</td></tr><tr><td>name</td><td>string</td><td>チャンネル名</td></tr><tr><td>user_id</td><td>string</td><td>(チャンネルを作成した)チャットユーザー ID</td></tr><tr><td>created_at</td><td>string</td><td>作成日時</td></tr><tr><td>updated_at</td><td>string</td><td>更新日</td></tr></tbody></table>

#### **チャンネルリストを取得する**

プロジェクトのチャンネルデータをリスト形式で取得するには、以下のコードを使用します。

```csharp
GameChat.getChannels(OFFSET, LIMIT, (List<Channel> Channels, GameChatException Exception) => {

    if(Exception != null)
    {
        // エラー処理
        return;
    }

    foreach(Channel elem in Channels)
    {
        //handling each channelInfo instance
    }
});
```

<table><thead><tr><th width="195">ID</th><th width="133">type</th><th>desc</th></tr></thead><tbody><tr><td>OFFSET</td><td>int</td><td>全体チャンネルリストから取得するチャンネルの開始位置(index)</td></tr><tr><td>LIMIT</td><td>int</td><td>取得するチャンネル数</td></tr></tbody></table>

#### **チャンネルデータを取得する**

チャンネル IDや固有 IDを用いてチャンネルデータを取得するには、以下のコードを使用します。

```csharp
//CHANNEL_IDでのみ検索する場合、CHANNEL_UNIQUE_IDパラメータに nullを入れます。

//CHANNEL_IDと CHANNEL_UNIQUE_IDの値が同時に存在する場合、CHANNEL_UNIQUE_IDの値を優先的に検索します。

GameChat.getChannel(CHANNEL_ID, CHANNEL_UNIQUE_ID,  (Channel Channel, GameChatException Exception) => {

    if(Exception != null)
    {
        // エラー処理
        return;
    }

    //handling channelInfo instance
});
```

```csharp
GameChat.getChannel(CHANNEL_UNIQUE_ID, (Channel Channel, GameChatException Exception) => {

    if(Exception != null)
    {
        // エラー処理
        return;
    }

    //handling channelInfo instance
});
```

| ID                  | type   | desc                  |
| ------------------- | ------ | --------------------- |
| CHANNEL\_ID         | string | チャンネル ID(自動作成)        |
| CHANNEL\_UNIQUE\_ID | string | チャンネル(固有)ID(カスタマイズ可能) |

### チャンネルの作成と削除

プロジェクト内で新しいチャンネルを作成または削除するには、Open APIを活用します。\
セキュリティ問題のため、チャンネル作成やアップデートなどは、Open APIを活用して Server to Serverで直接行うことをお勧めします。 詳細は、[Game Chat APIガイド](https://api.ncloud-docs.com/docs/ja/game-gamechat)をご参照ください。

### メッセージ

**(Received) Message Data Class (per Unit)**

```csharp
public class Message
{
    public class User
    {
        public string id;
        public string name;
        public string profile;
    }

    public string message_id;
    public string channel_id;
    public string message_type;
    public string content;

    public string[] mentions;
    public bool mentions_everyone;
    public User sender;
    public string created_at;
}
```

<table><thead><tr><th width="200">ID</th><th width="160">type</th><th>desc</th></tr></thead><tbody><tr><td>message_id</td><td>string</td><td>メッセージの固有 ID</td></tr><tr><td>channel_id</td><td>string</td><td>チャンネル ID</td></tr><tr><td>message_type</td><td>string</td><td>メッセージタイプ</td></tr><tr><td>content</td><td>string</td><td>メッセージの内容(JSON文字列)</td></tr><tr><td>mentions</td><td>string</td><td>メンション(タグ)</td></tr><tr><td>created_at</td><td></td><td>string</td></tr></tbody></table>

#### **メッセージリストを取得する**

特定のチャンネルに対するメッセージデータをリスト形式で取得するには、以下のコードを使用します。

```csharp
GameChat.getMessages(CHANNEL_ID, OFFSET, LIMIT, SEARCH, QUERY, SORT, (List<Message> Messages, GameChatException Exception) => {

    if(Exception != null)
    {
        // エラー処理
        return;
    }

    foreach(Message elem in Messages)
    {
        //handling each message instance
    }
});
```

<table><thead><tr><th width="184">ID</th><th width="121">type</th><th>desc</th></tr></thead><tbody><tr><td>CHANNEL_ID</td><td>string</td><td>チャンネル ID</td></tr><tr><td>OFFSET</td><td>string</td><td>全体メッセージリストから取得するメッセージの開始位置</td></tr><tr><td>LIMIT</td><td>string</td><td>取得するメッセージ数</td></tr><tr><td>SEARCH</td><td>string</td><td>メッセージ検索基準キー。 &#x3C;例> content.text<br>空の文字列を伝達する場合、全体検索</td></tr><tr><td>QUERY</td><td>string</td><td>メッセージ検索の値。 完全一致のみ検索可能。 空の文字列を伝達する場合、全体検索</td></tr><tr><td>SORT</td><td>string</td><td>メッセージのソート順序(デフォルト: 降順 - 最新順) (オプション: 昇順)</td></tr></tbody></table>

#### **メッセージの翻訳**

自動翻訳機能が有効化されている場合、任意のテキストを指定した言語に翻訳できます。 自動翻訳機能は、[Papago Translation](https://www.ncloud.com/product/aiService/papagoTranslation)サービスと連携すると使用できます。

**(Received) Translation Data Class (per Unit)**

```csharp
public class Translation
{
    public string detectLang = "";
    public string lang = "";
    public bool translated = false;
    public string message = "";
}
```

<table><thead><tr><th width="167">ID</th><th width="138">type</th><th>desc</th></tr></thead><tbody><tr><td>detectLang</td><td>string</td><td>ソース言語コード</td></tr><tr><td>lang</td><td>string</td><td>ターゲット言語コード</td></tr><tr><td>translated</td><td>bool</td><td>翻訳の成否</td></tr><tr><td>message</td><td>string</td><td>結果メッセージの内容(JSON文字列)</td></tr></tbody></table>

#### 参考

ソース言語コードとターゲット言語コードについての説明は、[Papago Text Translation APIガイド](https://api.ncloud-docs.com/docs/ja/ai-naver-papagonmt-translation)をご参照ください。

```csharp
GameChat.translateMessage(CHANNEL_ID, SORCE_LANG, TARTGET_LANG, TEXT, (List<Translation> Translations, GameChatException Exception) => {

    if(Exception != null)
    {
        // エラー処理
        return;
    }

    foreach(Translation elem in Translations)
    {
        //handling each Translation instance
    }
});

GameChat.translateMessage(SORCE_LANG, TARTGET_LANG, TEXT, (List<Translation> Translations, GameChatException Exception) => {

    if(Exception != null)
    {
        // エラー処理
        return;
    }

    foreach(Translation elem in Translations)
    {
        //handling each Translation instance
    }
});
```

<table><thead><tr><th width="181">ID</th><th width="113">type</th><th>desc</th></tr></thead><tbody><tr><td>CHANNEL_ID</td><td>string</td><td>チャンネル ID</td></tr><tr><td>SORCE_LANG</td><td>string</td><td>(送信する)テキストの言語名(auto: 自動検出)<br><a href="https://api.ncloud-docs.com/docs/ja/ai-naver-papagonmt-translation">API Guide</a>を参考</td></tr><tr><td>TARTGET_LANG</td><td>string</td><td>(翻訳受信する)テキストの言語コード<br>(","で区切って、複数入力可能。 &#x3C;例> "en, fr, th")<br><a href="https://api.ncloud-docs.com/docs/ja/ai-naver-papagonmt-translation">Papago Text Translation APIガイド</a>を参考</td></tr><tr><td>TEXT</td><td>string</td><td>送信するテキスト</td></tr></tbody></table>

### チャットユーザー

**(Received) Member Data Class (per Unit)**

```csharp
public class Member
{
    public string id = "";
    public string project_id = "";
    public string nickname = "";
    public string profile_url = "";
    public string country = "";
    public string remoteip = "";
    public string adid = "";
    public string device = "";
    public string network = "";
    public string version = "";
    public string model = "";
    public string logined_at = "";
    public string created_at = "";
    public string updated_at = "";
}
```

<table><thead><tr><th width="162">ID</th><th width="111">type</th><th>desc</th></tr></thead><tbody><tr><td>id</td><td>string</td><td>チャットユーザーの固有 ID</td></tr><tr><td>project_id</td><td>string</td><td>ログインした Game Chatプロジェクト ID</td></tr><tr><td>nickname</td><td>string</td><td>チャットユーザーのハンドルネーム</td></tr><tr><td>profile_url</td><td>string</td><td>プロフィール画像 URL</td></tr><tr><td>country</td><td>string</td><td>アクセスした国</td></tr><tr><td>remoteip</td><td>string</td><td>アクセス IPアドレス</td></tr><tr><td>adid</td><td>string</td><td>広告識別子</td></tr><tr><td>device</td><td>string</td><td>アクセスデバイスの環境</td></tr><tr><td>network</td><td>string</td><td>アクセスネットワークのタイプ (セルラー、Wi-Fi)</td></tr><tr><td>version</td><td>string</td><td>アクセスアプリのバージョン</td></tr><tr><td>model</td><td>string</td><td>アクセスデバイスのモデル</td></tr><tr><td>logined_at</td><td>string</td><td>ログインした日</td></tr><tr><td>created_at</td><td>string</td><td>チャットユーザーの作成日時</td></tr><tr><td>updated_at</td><td>string</td><td>チャットユーザー情報の更新日</td></tr></tbody></table>

#### **チャットユーザー情報のアップデート**

チャットサーバのユーザー情報をアップデートできます。

```csharp
// チャットユーザーのハンドルネームをアップデート
// ハンドルネームには、whitespace(spaces、tabs、line breaks)を除く2~128文字で入力できます。
GameChat.setName(MEMBER_ID, NAME, (Member member, GameChatException Exception) => {

    if(Exception != null)
    {
        // エラー処理
        return;
    }
    //handling updated Member instance
});

//チャットユーザーのプロフィール画像 URLをアップデート
GameChat.setProfileUrl(MEMBER_ID, PROFILE_URL, (Member member, GameChatException Exception) => {

    if(Exception != null)
    {
        // エラー処理
        return;
    }
    //handling updated Member instance
});
```

<table><thead><tr><th width="170">ID</th><th width="128">type</th><th>desc</th></tr></thead><tbody><tr><td>MEMBER_ID</td><td>string</td><td>チャットユーザーの固有 ID</td></tr><tr><td>NAME</td><td>string</td><td>チャットユーザーのハンドルネームまたは名前</td></tr><tr><td>PROFILE</td><td>string</td><td>プロフィール画像 URL</td></tr></tbody></table>

## GameChatExtension (Emoji, HyperLink) <a href="#gamechatextensionemojihyperlink" id="gamechatextensionemojihyperlink"></a>

受信メッセージに含まれた絵文字とハイパーリンクテキストを扱いやすいようにサポートするヘルパークラスです。

* TMP\_GameChatTextUGUIは、Unityビルトインアセットの TextMeshProを拡張したクラスであるため、先に Package Managerを用いて TextMeshProをインストールする必要があります。
* TextMeshProアセットの場合、Unity 2018.2以上のバージョンからビルトインアセットとして含まれます。
* 絵文字スプライトシートの場合、絵文字バージョン13(Android)を基準に基本的に表示され、スプライトシートを変更してカスタマイズできます。

```csharp
namespace GameChatUnity.Extension
{
    public class TMP_GameChatTextUGUI : TextMeshProUGUI
    {
        public bool isHyperLinked { get; set; }    // リンク形式アドレスをハイパーリンク処理するかどうか(HTMLタグを追加する)
        public string LinkTextColor { get; set; }  // hyperlink text color
    }
}
```

**<例>**

```csharp
using GameChatUnity.Extension;

TMP_GameChatTextUGUI message = msgObject.GetComponent<TMP_GameChatTextUGUI>();

//ハイパーリンクの認識と処理のために、テキストは setMessageを通じて入れます。
message.setMessage(MESSAGE_CONTENT);
message.color = Color.green;
message.isHyperLinked = true;

...

msgObject = Instantiate(msgObject) as GameObject;

...

// ハイパーリンクに対するクリックイベントリスナーは、直接実装してください。

//Handling with TMP_LinkInfo
TMP_LinkInfo linkInfoArr = message.textInfo.linkInfo[LINK_INDEX];

...
```


---

# 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://docs.gamechat.me/basics/game-chat-v2/game-chat-ri-ben-yu/game-chat-wosuru/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.
