Last active
October 13, 2015 19:18
-
-
Save anchan828/4243887 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
/// <summary> | |
/// https://gist.github.com/4243887 | |
/// http://anchan828.tumblr.com/post/37544410340 | |
/// </summary> | |
using UnityEngine; | |
using UnityEditor; | |
using System.Linq; | |
using System.Collections.Generic; | |
using System; | |
[InitializeOnLoad] | |
public class SceneNameCreator : Editor | |
{ | |
private const string SCENENAME_HASH_KEY = "SceneName_Hash"; | |
[MenuItem("Assets/SceneNameCreator")] | |
public static void _SceneNameCreator () | |
{ | |
if (EditorApplication.isPlaying || Application.isPlaying) | |
return; | |
BuildSceneName (); | |
} | |
static SceneNameCreator () | |
{ | |
if (EditorApplication.isPlaying || Application.isPlaying) | |
return; | |
BuildSceneName (); | |
} | |
static void BuildSceneName () | |
{ | |
System.Text.StringBuilder builder = new System.Text.StringBuilder (); | |
builder = WriteSceneManagerClass (builder); | |
string text = builder.ToString ().Replace (",}", "}"); | |
string assetPath = currentFolderPath + "../SceneName.cs"; | |
if (AssetDatabase.LoadAssetAtPath (assetPath.Replace ("/Editor/..", ""), typeof(UnityEngine.Object)) != null && EditorPrefs.GetInt (SCENENAME_HASH_KEY, 0) == text.GetHashCode ()) | |
return; | |
System.IO.File.WriteAllText (assetPath, text); | |
EditorPrefs.SetInt (SCENENAME_HASH_KEY, text.GetHashCode ()); | |
AssetDatabase.Refresh (ImportAssetOptions.ImportRecursive); | |
} | |
static System.Text.StringBuilder WriteSceneManagerClass (System.Text.StringBuilder builder) | |
{ | |
List<EditorBuildSettingsScene> enableScenes = GetEnableScenes (); | |
builder.AppendLine ("public class SceneName"); | |
builder.AppendLine ("{"); | |
{ | |
WriteSceneNameFunction (builder, enableScenes); | |
} | |
{ | |
WriteSceneNameArray (builder, enableScenes); | |
} | |
builder.AppendLine ("}"); | |
return builder; | |
} | |
static void WriteSceneNameFunction (System.Text.StringBuilder builder, List<EditorBuildSettingsScene> enableScenes) | |
{ | |
HashSet<string> sceneNames = new HashSet<string> (); | |
enableScenes.ForEach (scene => sceneNames.Add (ExtractSceneName (scene.path))); | |
//Add SceneName Function | |
sceneNames.ToList ().ForEach (sceneName => | |
{ | |
builder.Append ("\t").AppendLine ("/// <summary>"); | |
builder.Append ("\t").AppendFormat ("/// return \"{0}\"", sceneName).AppendLine (); | |
builder.Append ("\t").AppendLine ("/// </summary>"); | |
builder.Append ("\t").AppendFormat (@"public static string @{0} = ""{1}"";", Replace (sceneName), sceneName).AppendLine (); | |
}); | |
} | |
static void WriteSceneNameArray (System.Text.StringBuilder builder, List<EditorBuildSettingsScene> enableScenes) | |
{ | |
// Add LEVELS | |
builder.Append ("\t").Append ("public static readonly string[] LEVELS = new string[]{"); | |
enableScenes.ForEach (scene => builder.AppendFormat (@"""{0}"",", ExtractSceneName (scene.path))); | |
builder.AppendLine ("};"); | |
} | |
static string ExtractSceneName (string scenePath) | |
{ | |
return scenePath.Substring (scenePath.LastIndexOf ('/') + 1).Replace (".unity", string.Empty); | |
} | |
static List<EditorBuildSettingsScene> GetEnableScenes () | |
{ | |
return EditorBuildSettings.scenes.Where (scene => scene.enabled).ToList (); | |
} | |
static string Replace (string name) | |
{ | |
string[] invalidChar = new string[] {" ", "!", "\"", "#", "$", "%", "&", "\'", "(", ")", "-", "=", "^", "~", "¥", "|", "[", "{", "@", "`", "]", "}", ":", "*", ";", "+", "/", "?", ".", ">", ",", "<"}; | |
invalidChar.ToList ().ForEach (s => name = name.Replace (s, string.Empty)); | |
return name; | |
} | |
static string currentFolderPath { | |
get { | |
string currentFilePath = new System.Diagnostics.StackTrace (true).GetFrame (0).GetFileName (); | |
return "Assets" + currentFilePath.Substring (0, currentFilePath.LastIndexOf ("/") + 1).Replace (Application.dataPath, string.Empty); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment