Skip to content

Instantly share code, notes, and snippets.

@TsubameUnity
Last active April 5, 2019 05:16
Show Gist options
  • Save TsubameUnity/3a34c448f2bb629d0440ccaac82648d8 to your computer and use it in GitHub Desktop.
Save TsubameUnity/3a34c448f2bb629d0440ccaac82648d8 to your computer and use it in GitHub Desktop.
ビルドシーンに一覧からシーン選択できるアトリビュート。登録シーンが消えても手動で変更するまで保持します
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