Created
December 10, 2012 16:36
-
-
Save anchan828/4251675 to your computer and use it in GitHub Desktop.
インスペクターでシーン名をEnumで選択できるプロパティ拡張
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 UnityEngine; | |
public class SceneAttribute : PropertyAttribute | |
{ | |
public int selectedValue = 0; | |
public SceneAttribute () | |
{ | |
} | |
} |
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 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