4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Unity】3stepで簡単な画面遷移を実装する 〜SceneManager.LoadSceneAsync()〜

Posted at

画面遷移を実装する

【step1】シーンを用意する

遷移させる画面分のシーンを用意する。###

今回は以下のような画面遷移を想定する為、用意するシーンは3つ。

StartScene
↓
GameScene
↓ 
ClearScene
スクリーンショット 2019-04-03 11.50.35.png ## 【step2】スクリプトを用意し、アタッチする ### 遷移させる回数分のスクリプトを用意する。### 今回遷移させる回数は以下の2回の為、用意するスクリプトは2つ。
StartScene → GameScene
GameScene → ClearScene

遷移元となるStartSceneGameScene用のスクリプトを用意する。

StartSceneDirector
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class StartSceneDirector : MonoBehaviour
{
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            SceneManager.LoadSceneAsync("GameScene");
        }
    }
}
GameSceneDirector
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class GameSceneDirector : MonoBehaviour
{
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            SceneManager.LoadSceneAsync("ClearScene");
        }
    }
}

【解説】
using UnityEngine.SceneManagement;
 ⇒ 画面遷移の実装に必要なので追記します。

if (Input.GetMouseButtonDown(0)){}
 ⇒ 今回はマウスの左クリック押下で反応するように設定します。

SceneManager.LoadSceneAsync("遷移させるシーン名");
 ⇒ このコードが実行されると画面遷移します。

スクリプトをアタッチする。###

各シーンのHierarchyウィンドウで空のGameObjectを作成し、スクリプトをアタッチする。
※空のGameObjectを作成するショートカットキー:CTRL/CMD+SHIFT+N

StartScene
スクリーンショット 2019-04-08 11.26.32.png
スクリーンショット 2019-04-08 11.39.39.png

GameScene
スクリーンショット 2019-04-08 11.41.17.png
スクリーンショット 2019-04-08 11.41.27.png

【step3】Build SettingScenes In Buildに使用するシーンを追加する

Projectウィンドウから使用するシーンをドラッグ&ドロップする。
スクリーンショット 2019-04-03 11.50.35.png

スクリーンショット 2019-04-08 11.54.51.png
※最初に表示させたい画面が一番上になるように並べる

完成!(実行イメージ)

ビルドして実行する。
※マウスの左クリックで画面遷移
StartScene
スクリーンショット 2019-04-08 12.10.01.png

GameScene
スクリーンショット 2019-04-08 12.10.11.png

ClearScene
スクリーンショット 2019-04-08 12.10.18.png

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?