LoginSignup
1
1

More than 3 years have passed since last update.

【Unity】FungusのLuaで非同期処理を待機する方法

Posted at

結論

Fungusが提供しているLua関数群の「runwait」を用います。

前提

C#側の関数は「IEnumerator」を戻り値にする必要があります。

応用方法

前回の記事のLuaが終了するまで待機する方法とあわせると、

  • Fungus側で会話1
  • Unity側でイベント
  • Fungus側で会話2
  • Unity側で終了を検知

することができるので、チュートリアルやRPGのイベントなどの表現の幅がぐっと広がります。

例)番号順に処理されます

C#側

// UnityのButtonで呼び出す関数
public async void OnFungusAsync()
{
    Debug.Log($"1 Luaの呼び出し開始");

    await _luaScript.OnExecuteAsync();

    Debug.Log($"6 Luaの呼び出し終了");
}

// Luaで呼び出している関数
public IEnumerator OnMoveCoroutine()
{
    Debug.Log("3 C#側で開始");

    // DoTweenを使い移動処理を行う
    transform.DOMove(new Vector3(1f, 1f, 0f), 5f);

    // 移動が終わるまで待機する
    yield return new WaitForSeconds(5f);

    Debug.Log("4 C#側で終了");
}

Lua側

say("2 Lua開始")

-- C#側の非同期関数を呼び出す
runwait(mainmanager.OnMoveCoroutine())

say("5 Lua終了")

その他

非同期を投げっぱなしにする関数「run」も用意されています。

Fungasが提供している関数は

\Assets\Fungus\Thirdparty\FungusLua\Resources\Lua\fungus.txt

を参照するとわかると思います。

1
1
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
1
1