LoginSignup
0
0

More than 5 years have passed since last update.

MUN 2.4.1.1 で JoinRoom 直後に inRoom を使ってハマった時のメモ

Posted at

前提

Monobit Unity Networking 2.0(MUN)とは?
http://www.monobitengine.com/mun/

MonobitNetwork.JoinRoom

  • 戻り値 bool
    • 入室に成功したら true、失敗したら false を返します。

MonobitNetwork.inRoom

  • 戻り値 bool
    • 入室中なら true、入室していなかったら false を返します。

問題の箇所

// OnGUI is called for rendering and handling GUI events
void OnGUI(){
    if (GUILayout.Button("Enter Room)"))
    {
        MonobitNetwork.JoinRoom(room.name);
        if (MonobitNetwork.inRoom)
        {
            Debug.Log("入室完了");
        }
        else
        {
            Debug.Log("入室失敗");
        }
    }
}

出力結果

入室失敗

解決方法1

コルーチンで少し待ってやればinRoom判別通るようになる。

// OnGUI is called for rendering and handling GUI events
void OnGUI(){
    if (GUILayout.Button("Enter Room)"))
    {
        MonobitNetwork.JoinRoom(room.name);
        StartCoroutine(WaitInRoom());
    }
}
//入室待ちコルーチン
IEnumerator WaitInRoom()
{
    // 0.5秒待つ
    yield return new WaitForSeconds(0.5f);

    if (MonobitNetwork.inRoom)
    {
        Debug.Log("入室完了");
    }
    else
    {
        Debug.Log("入室失敗");
    }
}

出力結果

入室完了

解決方法2

そもそもJoinRoomでbool返ってくるので、それで用が成せるならこれでもいい。

// OnGUI is called for rendering and handling GUI events
void OnGUI(){
    if (GUILayout.Button("Enter Room)"))
    {
        if (MonobitNetwork.JoinRoom(room.name))
        {
            Debug.Log("入室完了");
        }
        else
        {
            Debug.Log("入室失敗");
        }
    }
}

出力結果

入室完了

まとめ

MonobitNetwork.JoinRoom

MonobitNetwork.inRoom
の入室判定は同じ結果にならない瞬間がある。

雑感

入室してすぐカスタムパラメータとか取得したかったので、
inRoomで判別しようかとして気づいたわずかな差でした。

恐らくサーバー側にinRoomがTrueになる条件のデータを入れきる前にinRoomしてしまってるのかな。
たぶんそのうち自前で解決する羽目になりそうなので、原因確認は後まわし。。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0