LoginSignup
14
11

More than 5 years have passed since last update.

yield return www みたいに書きたい

Last updated at Posted at 2016-01-28

Unityでcoroutineを使用した際
whileを使用して待ち処理を作ることがよくあります。

test1.cs
class ABC {
    public bool isBusy;
}

IEnumerator Hoge()
{
    ABC a = new ABC();
    while(a.isBusy) {
        yield retun null;
    }
}

WWWクラスを使用した際は

test2.cs
IEnumerator Hoge()
{
    WWW www = new WWW();
    yield return www;
}

…なんかズルい…

自分が作ったclassでもwhile使わずに書きたい!

ググったところ Unity5.3から使える
CustomYieldInstructionを使用すれば実装出来ました。

CustomYieldInstructionのkeepWaitingプロパティを
オーバーライドして条件を書けばOKでした。

test3.cs
class ABC : CustomYieldInstruction {
    public bool isBusy;
    public override bool keepWaiting {
        get { return isBusy; }
    }
}

IEnumerator Hoge()
{
    ABC a = new ABC();
    yield return a;
}

便利!簡潔!

14
11
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
14
11