毎回忘れるのでFLGの待ち方をメモっておく、
ついStartCoroutine使いたくなる。
全ループが終わった後にAfter Waitのログが出力されているので
ちゃんと待てていると思われる。
using System;
using System.Threading.Tasks;
using UnityEngine;
public class FlgWait : MonoBehaviour
{
/// <summary>
/// ボタンが押された
/// </summary>
public void OnClickButton()
{
WaitHoge();
}
private async Task WaitHoge()
{
bool doHoge = true;
var _ = Task.Run(() =>
{
int i = 1;
while (doHoge)
{
try
{
i++;
Debug.LogError($"{i}");
if (i >= 10000)
{
doHoge = false;
}
}
catch (Exception e)
{
Debug.LogWarning("[Server] " + e);
throw new Exception("Error");
}
}
});
Debug.LogError($"Before Wait");
await _;
Debug.LogError($"After Wait");
}
}
参考URL
https://qiita.com/Alupaca1363Inew/items/0126270bca99883605de