4
6

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.

Scene遷移に使うScene名をBuildSettingsから読み込むエディタ拡張

Last updated at Posted at 2017-04-04

まずScene遷移は一般的に

        SceneManager.LoadScene("遷移先のScene名");

と書かれる。
stringをコードに直接書き込むのはtypoの発生やScene名の変更に弱い等の問題点があるのでBuildSettingsからSceneのPathを読み込み、ドロップダウンメニューを作成するPropertyAttributeを作成した。

SceneChangerAttribute.cs
using UnityEngine;
# if UNITY_EDITOR
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
# endif

class SceneChangerAttribute : PropertyAttribute { }
# if UNITY_EDITOR
[CustomPropertyDrawer(typeof(SceneChangerAttribute))]
public class SceneChangerEditor : PropertyDrawer
{
    //Scene名のList
    List<string> AllSceneName
    {
        get
        {
            List<string> sceneNames = new List<string>();
            //BuildSettingsからSceneのPathを読み込む
            List<string> AllPaths = (from scene in EditorBuildSettings.scenes where scene.enabled select scene.path).ToList();
            //PathからScene名を切り出す
            foreach (string x in AllPaths)
            {
                int slash = x.LastIndexOf("/");
                int dot = x.LastIndexOf(".");
                sceneNames.Add(x.Substring(slash + 1, dot - slash - 1));
            }
            return sceneNames;
        }
    }
    //ドロップダウンメニューの作成
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        var list = AllSceneName;
        var selectedIndex = list.FindIndex(item => item.Equals(property.stringValue));
        if (selectedIndex == -1)
        {
            selectedIndex = list.FindIndex(item => item.Equals(list[0]));
        }

        selectedIndex = EditorGUI.Popup(position, label.text, selectedIndex, list.ToArray());

        property.stringValue = list[selectedIndex];
    }
}
# endif

使用時は上記のSceneChangerAttribute.csをUnityのプロジェクトに入れ、以下のようにする(例はButtonでScene遷移したい場合)。

SceneChanger4Button.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class SceneChanger4Button : MonoBehaviour
{
    //どんな名前でもいいのでstring型のフィールドに付ける
    [SerializeField, SceneChangerAttribute]
    string _NextScene;

    void OnClick()
    {
        SceneManager.LoadScene(_NextScene);
    }
}

sc4b.png
このようにBuildSettingsに追加したSceneの名前が表示される。
利点はtypoがないこととエディタ上で遷移先がわかること。
欠点はBuildSettingsでSceneの順番を入れ替えると対応ができず設定をし直さねばならないこと、ただこれはタイトルに使うSceneを0に設定する以外は順番を入れ替えることはほとんどないと思うのでそこまで影響はないと思います。
参考文献
UnityのAttribute(属性)についてまとめてメモる。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?