0
1

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 1 year has passed since last update.

Unity BuildSettingsからシーン(Scene)の名前を取得する方法

Posted at

0.0 はじめに

シーンの遷移(SceneManager.LoadScene)などを行う際に、下記のようにBuildSettings内のScenes in Buildへシーンを登録しますが、この登録されたシーンの名前を取得する方法を見つけるのに、少し手間取ったので忘れないよう記事作りました。
image.png

1.0 取得方法

EditorBuildSettingsを使うためにネームスペースにusing UnityEditorを追加します。
EditorBuildSettings.scenesシーンの情報が入っています。
シーンの情報は順番に配列の形で格納されています。
これを使ってまず各シーンのパスを入手。
そのパスからシーンの名前を取得します。最後はGetFileNameWithoutExtension(ファイル名だけを取得する関数を使いました。)

C# Test.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor; // EditorBuildSettingsを使うために追加

public class Test : MonoBehaviour
{
    private void Start() {
        CheckSceneName();
    }

    /// <summary>
    /// BuildSettingsからシーンの名前を取得する方法
    /// 登録されているシーンをコンソールに印字する
    /// </summary>
    void CheckSceneName() {

        for (int i = 0; i < EditorBuildSettings.scenes.Length; i++) {
            // シーンのパスを取得
            string scenePath = EditorBuildSettings.scenes[i].path;

            // パスからシーン名を取得
            string sceneName = System.IO.Path.GetFileNameWithoutExtension(scenePath);

            // コンソールに表示
            Debug.Log($"{i}番目のsceneの名前は {sceneName} です");
        }

    }

    /// <summary>
    /// BuildSettingsに登録されているシーンの名前を取得する
    /// 返り値はStringの配列
    /// </summary>
    string[] GetSceneName() {

        // シーンの名前を入れる配列
        string[] sceneNameArray = new string[EditorBuildSettings.scenes.Length];

        for (int i = 0; i < EditorBuildSettings.scenes.Length; i++) {
            // シーンのパスを取得
            string scenePath = EditorBuildSettings.scenes[i].path;

            // パスからシーン名を取得
            string sceneName = System.IO.Path.GetFileNameWithoutExtension(scenePath);

            // 配列に登録
            sceneNameArray[i] = sceneName;
        }

        return sceneNameArray;
    }
}

2.0 結果

image.png

3.0 おまけ

下記はシーンの名前を配列で返す関数です。

C# Test2.cs

/// <summary>
/// BuildSettingsに登録されているシーンの名前を取得する2
/// 返り値はStringの配列
/// </summary>
string[] GetSceneName() {
    
    // シーンの名前を入れる配列
    string[] sceneNameArray = new string[EditorBuildSettings.scenes.Length];

    for (int i = 0; i < EditorBuildSettings.scenes.Length; i++) {
        // シーンのパスを取得
        string scenePath = EditorBuildSettings.scenes[i].path;

        // パスからシーン名を取得
        string sceneName = System.IO.Path.GetFileNameWithoutExtension(scenePath);

        // 配列に登録
        sceneNameArray[i] = sceneName;
    }

    return sceneNameArray;
}
0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?