Skip to content

Instantly share code, notes, and snippets.

@anchan828
Created December 10, 2012 16:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anchan828/4251675 to your computer and use it in GitHub Desktop.
Save anchan828/4251675 to your computer and use it in GitHub Desktop.
インスペクターでシーン名をEnumで選択できるプロパティ拡張
using UnityEngine;
public class SceneAttribute : PropertyAttribute
{
public int selectedValue = 0;
public SceneAttribute ()
{
}
}
using UnityEngine;
using System.Linq;
using System.Collections.Generic;
using UnityEditor;
[CustomPropertyDrawer(typeof(SceneAttribute))]
public class SceneNameDrawer : PropertyDrawer
{
public override void OnGUI (Rect position, SerializedProperty property, GUIContent label)
{
SceneAttribute sceneAttribute = (SceneAttribute)attribute;
HashSet<string> sceneNames = GetSceneNames ();
string[] sceneNamesArray = sceneNames.ToArray ();
int[] sceneNumbers = new int[sceneNamesArray.Length];
SetSceneNambers (sceneNumbers, sceneNamesArray);
sceneAttribute.selectedValue = EditorGUI.IntPopup (position, label.text, sceneAttribute.selectedValue, sceneNamesArray, sceneNumbers);
property.stringValue = sceneNamesArray [sceneAttribute.selectedValue];
}
HashSet<string> GetSceneNames ()
{
List<EditorBuildSettingsScene> scenes = EditorBuildSettings.scenes.Where (scene => scene.enabled).ToList ();
HashSet<string> sceneNames = new HashSet<string> ();
scenes.ForEach (scene => {
sceneNames.Add (scene.path.Substring (scene.path.LastIndexOf ("/") + 1).Replace (".unity", string.Empty));
});
return sceneNames;
}
void SetSceneNambers (int[] sceneNumbers, string[] sceneNamesArray)
{
for (int i =0; i<sceneNamesArray.Length; i++) {
sceneNumbers [i] = i;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment