Help us understand the problem. What is going on with this article?

Unityでプロジェクトを開いたときにBuild Settingsと同じシーンをロードする

More than 1 year has passed since last update.

はじめに

マルチシーンで作っている時に毎回、プロジェクトを開く度にHierarychyにシーンを追加するのが面倒だったのでBuild Settingsと同じシーンを一気に開けるようにしました。

スクリプト

プロジェクトにEditorフォルダを作り、以下のスクリプトを配置するだけです。
Unity2018.3.6f1で動作確認しました。

Editor/BuildSettingsSceneLoader.cs
// MIT License
// 
// Copyright (c) 2019 shiena
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using System.IO;
using System.Reflection;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine.Events;

namespace Editor
{
    public static class EditorApplicationUtility
    {
        private const BindingFlags BINDING_ATTR = BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic;

        private static readonly FieldInfo m_info = typeof(EditorApplication).GetField("projectWasLoaded", BINDING_ATTR);

        public static UnityAction projectWasLoaded
        {
            get { return m_info.GetValue(null) as UnityAction; }
            set
            {
                var functions = m_info.GetValue(null) as UnityAction;
                functions += value;
                m_info.SetValue(null, functions);
            }
        }
    }

    public static class BuildSettingsSceneLoader
    {
        [InitializeOnLoadMethod]
        private static void LoadBuildSettingsScenes()
        {
            EditorApplicationUtility.projectWasLoaded += () =>
            {
                var isFirstScene = true;
                foreach (var scene in EditorBuildSettings.scenes)
                {
                    if (!(scene.enabled && File.Exists(scene.path)))
                    {
                        continue;
                    }

                    if (isFirstScene)
                    {
                        EditorSceneManager.OpenScene(scene.path, OpenSceneMode.Single);

                        isFirstScene = false;
                    }
                    else
                    {
                        EditorSceneManager.OpenScene(scene.path, OpenSceneMode.Additive);
                    }
                }
            };
        }
    }
}

解説

大した事はしていませんが、プロジェクトを開いたときに実行するInitializeOnLoadMethodAttributeとプロジェクトロード完了時に実行するEditorApplication.projectWasLoadedを組み合わせてBuild Settingsに設定したシーンを開いています。

参考リンク

kadinche
弊社では 2008年の会社設立当初から、一貫して VR ソリューションの研究開発に取り組んでいます。
https://www.kadinche.com/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした