Quantcast
Channel: C#タグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8901

自動でBootシーンに切り替える仕組みを作ると人生が楽になる 【Unity】

$
0
0

ゲームを作る時に、初期化用のシーンを経由してから指定したシーンに移行し動作させることってあると思うのですが
そういう設計にすると制作中テストする度に初期化用のシーンに戻る必要が出てきて少し大変ですよね

この問題はUnityのEditor拡張を利用することで解決できます。

実装は簡単。再生時にシーンを切り替える命令を登録するだけ

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

[InitializeOnLoad]
public static class PlayModeEditor
{
    static string m_bootSceneName ="Boot";// ←移動したいシーンの名前を記入する

    static PlayModeEditor()
    {
        EditorApplication.playModeStateChanged += ChangeBootScene;
    }

    static void ChangeBootScene(PlayModeStateChange state)
    {
        // 実行状態になったら
        if (state == PlayModeStateChange.EnteredPlayMode)
        {
            // 別シーンで起動していた場合切り替える
            Scene scene = SceneManager.GetActiveScene();
            if (!scene.Equals(m_bootSceneName))
            {
                SceneManager.LoadScene(m_bootSceneName);
            }
        }
    }    
}

実行時の結果

playmode.gif

やったぜ。

この機能を利用すれば他でも応用できそうです。


Viewing all articles
Browse latest Browse all 8901

Trending Articles