Created
March 1, 2014 07:09
-
-
Save baba-s/9286333 to your computer and use it in GitHub Desktop.
This file contains hidden or 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; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using UnityEditor; | |
using UnityEngine; | |
/// <summary> | |
/// AudioClipの名前を定数で管理するクラスを作成するスクリプト | |
/// </summary> | |
public static class AudioClipNameCreator | |
{ | |
// 無効な文字を管理する配列 | |
private static readonly string[] INVALUD_CHARS = | |
{ | |
" ", "!", "\"", "#", "$", | |
"%", "&", "\'", "(", ")", | |
"-", "=", "^", "~", "\", | |
"|", "[", "{", "@", "`", | |
"]", "}", ":", "*", ";", | |
"+", "/", "?", ".", ">", | |
",", "<" | |
}; | |
private const string ITEM_NAME = "Tools/Create/Audio Clip Name"; // コマンド名 | |
private const string PATH = "Assets/Scripts/AudioClipName.cs"; // ファイルパス | |
private static readonly string FILENAME = Path.GetFileName(PATH); // ファイル名(拡張子あり) | |
private static readonly string FILENAME_WITHOUT_EXTENSION = Path.GetFileNameWithoutExtension(PATH); // ファイル名(拡張子なし) | |
/// <summary> | |
/// AudioClipの名前を定数で管理するクラスを作成します | |
/// </summary> | |
[MenuItem(ITEM_NAME)] | |
public static void CreateAudioClipName() | |
{ | |
if (!CanCreate()) | |
{ | |
return; | |
} | |
CreateScript(); | |
EditorUtility.DisplayDialog(FILENAME, "作成が完了しました", "OK"); | |
} | |
/// <summary> | |
/// スクリプトを作成します | |
/// </summary> | |
public static void CreateScript() | |
{ | |
var builder = new StringBuilder(); | |
builder.AppendLine("/// <summary>"); | |
builder.AppendLine("/// AudioClipの名前を定数で管理するクラス"); | |
builder.AppendLine("/// </summary>"); | |
builder.AppendFormat("public static class {0}", FILENAME_WITHOUT_EXTENSION).AppendLine(); | |
builder.AppendLine("{"); | |
foreach (var n in GetAssets<AudioClip>() | |
.Select(c => Path.GetFileNameWithoutExtension(c.name)) | |
.Select(c => new { var = RemoveInvalidChars(c), val = c })) | |
{ | |
builder.Append("\t").AppendFormat(@"public const string {0} = ""{1}"";", n.var, n.val).AppendLine(); | |
} | |
builder.AppendLine("}"); | |
File.WriteAllText(PATH, builder.ToString(), Encoding.UTF8); | |
AssetDatabase.Refresh(ImportAssetOptions.ImportRecursive); | |
} | |
/// <summary> | |
/// Audio Clipの名前を定数で管理するクラスを作成できるかどうかを取得します | |
/// </summary> | |
[MenuItem(ITEM_NAME, true)] | |
public static bool CanCreate() | |
{ | |
return !EditorApplication.isPlaying && !Application.isPlaying && !EditorApplication.isCompiling; | |
} | |
/// <summary> | |
/// 無効な文字を削除します | |
/// </summary> | |
public static string RemoveInvalidChars(string str) | |
{ | |
Array.ForEach(INVALUD_CHARS, c => str = str.Replace(c, string.Empty)); | |
return str; | |
} | |
/// <summary> | |
/// 指定されたすべてのアセットを取得します | |
/// </summary> | |
/// <returns>指定されたすべてのアセット</returns> | |
public static IEnumerable<T> GetAssets<T>() where T : UnityEngine.Object | |
{ | |
return AssetDatabase.GetAllAssetPaths().Select(c => AssetDatabase.LoadAssetAtPath(c, typeof (T))).OfType<T>(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment