Last active
March 29, 2018 01:04
-
-
Save kankikuchi/6233619aa5a6b3661171ee3fc83490e1 to your computer and use it in GitHub Desktop.
インポートされたファイルの拡張子を自動で書き換える【Unity】【エディタ拡張】
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
// ExtensionConverter.cs | |
// http://kan-kikuchi.hatenablog.com/entry/ExtensionConverter | |
// | |
// Created by kan.kikuchi on 2018.03.03. | |
using System.IO; | |
using System.Collections.Generic; | |
using UnityEditor; | |
using UnityEngine; | |
/// <summary> | |
/// 拡張子を変換するクラス | |
/// </summary> | |
public class ExtensionConverter : AssetPostprocessor { | |
//変換対象 | |
private static readonly Dictionary<string, string> TARGET_DICT = new Dictionary<string, string>() { | |
{".xxx", ".txt"}, //.xxx → .txtに変更 | |
{".yyy", ".jpg"}, //.yyy → .jpgに変更 | |
}; | |
//================================================================================= | |
//監視 | |
//================================================================================= | |
#if !UNITY_CLOUD_BUILD && UNITY_EDITOR_OSX | |
//アセットのインポートやリネームが完了した時等に呼び出される | |
private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths) { | |
//インポートやリネームされたファイルで変換する必要があるものだけ変換する | |
ConvertIfneeded(importedAssets); | |
} | |
#endif | |
//================================================================================= | |
//変換 | |
//================================================================================= | |
//変換する必要があるファイルだけ変換する | |
private static void ConvertIfneeded(string[] filePaths) { | |
bool isConverted = false; | |
//インポートやリネームされたファイルを変換するかチェック | |
foreach (string filePath in filePaths) { | |
isConverted = ConvertIfneeded(filePath) || isConverted; | |
} | |
//変換があれば、変更を反映 | |
if (isConverted) { | |
AssetDatabase.Refresh(); | |
} | |
} | |
//変換する必要があれば変換する | |
private static bool ConvertIfneeded(string filePath) { | |
//拡張子を取得し、対象かどうか判定 | |
string extension = Path.GetExtension(filePath); | |
if (!TARGET_DICT.ContainsKey(extension)) { | |
return false; | |
} | |
//新しいパスを作成し、拡張子を変更 | |
string newFilePath = Path.ChangeExtension(filePath, TARGET_DICT[extension]); | |
File.Move(filePath, newFilePath); | |
//変更をログで通知 | |
Debug.Log(filePath + "を" + newFilePath + "に変更しました!"); | |
return true; | |
} | |
//プロジェクト内の対象の拡張子を一括変換する | |
[MenuItem("Tools/Convert/Extension")] | |
private static void ConvertExtension(){ | |
//Assets以下の全ファイルを取得し、変換する必要があるものだけ変換する | |
string[] filePathArray = Directory.GetFiles("Assets/", "*", SearchOption.AllDirectories); | |
ConvertIfneeded(filePathArray); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://kan-kikuchi.hatenablog.com/entry/ExtensionConverter