Last active
April 5, 2019 05:16
-
-
Save TsubameUnity/3a34c448f2bb629d0440ccaac82648d8 to your computer and use it in GitHub Desktop.
ビルドシーンに一覧からシーン選択できるアトリビュート。登録シーンが消えても手動で変更するまで保持します
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
using UnityEngine; | |
#if UNITY_EDITOR | |
using UnityEditor; | |
#endif | |
class SceneAttribute : PropertyAttribute | |
{ | |
} | |
#if UNITY_EDITOR | |
[CustomPropertyDrawer(typeof(SceneAttribute))] | |
class SceneDrawer : PropertyDrawer | |
{ | |
static readonly string DummySceneName = "__Deleted__"; | |
bool _init; | |
int _index; | |
string[] _sceneNames; | |
public SceneDrawer() | |
{ | |
var scenes = EditorBuildSettings.scenes | |
.Where(scene => scene.enabled) | |
.Select(scene => System.IO.Path.GetFileNameWithoutExtension(scene.path)); | |
if (scenes.Count() == 0) | |
{ | |
scenes = scenes.Concat(new string[] { DummySceneName }); | |
} | |
_sceneNames = scenes.ToArray(); | |
} | |
void GetCurrentSelectIndex(SerializedProperty property, ref int index) | |
{ | |
string sceneName = property.stringValue; | |
for (int i = 0; i < _sceneNames.Length; i++) | |
{ | |
if (sceneName == _sceneNames[i]) | |
{ | |
_index = i; | |
return; | |
} | |
} | |
_index = _sceneNames.Length - 1; | |
} | |
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) | |
{ | |
if (property.propertyType == SerializedPropertyType.String) | |
{ | |
property.serializedObject.Update(); | |
if (!_init) | |
{ | |
GetCurrentSelectIndex(property, ref _index); | |
_init = true; | |
} | |
_index = EditorGUI.Popup(position, property.displayName, _index, _sceneNames); | |
if (_sceneNames[_index] != DummySceneName) | |
{ | |
property.stringValue = _sceneNames[_index]; | |
} | |
property.serializedObject.ApplyModifiedProperties(); | |
return; | |
} | |
EditorGUI.PropertyField(position, property, label, true); | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment