LoginSignup
4
4

More than 5 years have passed since last update.

[Unity]コルーチンでNowLoading...のアニメーションをつける

Posted at

ロード画面でNowLoadingと表示されるゲームが多いが、何かしらのアニメーションがないとフリーズしていると勘違いされることもあるため、アニメーションを付けることにした。
アニメーションといっても、ただ単にNowLoading...の...の数が変化していくというものである。

実装

uGUIのテキストに表示するという前提で以下のようにコードを作成した。
ここでのnowLoadingは、あらかじめText nowLoading;と宣言しておく必要がある。

void Start(){
        StartCoroutine("displayNowLoading"); // コルーチン呼び出し
}

IEnumerator displayNowLoading(){ // ここでNowLoading表示とアニメーション
        while(true){
            nowLoading.text = "NowLoading";
            yield return new WaitForSeconds(0.5f);
            nowLoading.text = "NowLoading.";
            yield return new WaitForSeconds(0.5f);
            nowLoading.text = "NowLoading..";
            yield return new WaitForSeconds(0.5f);
            nowLoading.text = "NowLoading...";
            yield return new WaitForSeconds(0.5f);
        }
}

displayNowLoading()では、0.5秒ごとに...の数が変化するという仕組みになっている。
0.5秒のdelayは、yield return new WaitForSeconds(0.5f)で実装できる。
...の表示速度を変えるには、0.5fを別の値に変更すればよい。
また、whileループでそれらを囲うことで、
NowLoading
NowLoading.
NowLoading..
NowLoading...
NowLoading(戻る)
NowLoading.
NowLoading..
NowLoading...
NowLoading
という感じにアニメーションを作成できる。

割とゴリ押しコードだが、簡単に実装できて良いと思う。

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