public void StashScripts() { foreach (var source in Sources) { var sourcePath = AssetUtil.CombinePath(ProjectInfo.RootPath, source.AssetPath); var destPath = AssetUtil.CombinePath(_destRoot, source.AssetPath); AssetUtil.CopyFile(sourcePath, destPath, true); } foreach (var source in Sources) { AssetDatabase.DeleteAsset(source.AssetPath); } var directories = Sources.Select(x => Path.GetDirectoryName(x.AssetPath)).Distinct(); foreach (var directory in directories) { var files = AssetDatabase.FindAssets(string.Empty, new[] { directory }); if (!files.Any()) { AssetDatabase.DeleteAsset(directory); } } }
public string GetCachedScriptPath() { return(AssetUtil.CombinePath( ProjectInfo.RootPath, _settings.SourceCodeCacheRoot, _assemblyName, AssetPath)); }
public static FileImportSettings Find() { var settingsPath = AssetUtil.CombinePath( Path.GetDirectoryName(AssetUtil.FindScriptPath <FileImportSettings>()), "settings.asset"); return(File.Exists(settingsPath) ? AssetDatabase.LoadAssetAtPath <FileImportSettings>(settingsPath) : null); }
public static void ImportExcel() { var inputFile = AssetDatabase.GetAssetPath(Selection.activeObject); var outputDirectory = AssetUtil.CombinePath( Path.GetDirectoryName(inputFile), Path.GetFileNameWithoutExtension(inputFile)); Translator.UpdateDictionaries(inputFile, outputDirectory); }
public void SaveToFile() { CleanupCache(); Directory.CreateDirectory(_destRoot); var configPath = AssetUtil.CombinePath( _destRoot, Path.GetFileNameWithoutExtension(Assembly) + ".json"); var jsonStr = JsonUtility.ToJson(this); File.WriteAllText(configPath, jsonStr); }
public string BuildScripts(string[] scripts, string assemblyName) { if (scripts.Length == 0) { return(null); } // スクリプトのパスをいちいち指定してたらコマンドラインが文字数オーバーするかもしれないから // 一旦テンポラリに全部コピーして *.cs で指定できるようにする。 var tmpScriptsDir = AssetUtil.CombinePath(Application.temporaryCachePath, assemblyName); AssetUtil.CleanupDirectory(tmpScriptsDir); foreach (var script in scripts) { var source = AssetUtil.CombinePath(ProjectInfo.RootPath, script); var dest = AssetUtil.CombinePath(tmpScriptsDir, Path.GetFileName(script)); AssetUtil.CopyFile(source, dest, true); } // 既にビルド済みアセンブリがいたら消す var outputPath = AssetUtil.CombinePath(_settings.AssemblyRoot, assemblyName, assemblyName + ".dll"); AssetUtil.CleanupAssetDirectory(Path.GetDirectoryName(outputPath)); var outputAssemblyPath = AssetUtil.CombinePath(ProjectInfo.RootPath, outputPath); // ビルド var buildSuccess = ScriptBuilder.Build( outputAssemblyPath, new[] { tmpScriptsDir + "/*.cs" }, output => Debug.Log(output), error => Debug.LogError(error)); if (!buildSuccess) { AssetUtil.DeleteFile(outputAssemblyPath); return(null); } // ビルドしたアセンブリをインポートして、ソースコードを退避 AssetDatabase.ImportAsset(outputPath); var info = new AssemblyInfo(_settings, outputPath, scripts); info.SaveToFile(); info.StashScripts(); return(outputPath); }
public static bool Build(string outputPath, string[] scripts, Action <string> onOutputReceived, Action <string> onErrorReceived) { var result = true; AssetUtil.DeleteFile(outputPath); AssetUtil.CleanupDirectory(Path.GetDirectoryName(outputPath)); using (var process = Process.Start(new ProcessStartInfo() { #if UNITY_EDITOR_WIN FileName = EditorUtil.Preference.MonoDirectory + "/bin/smcs.bat", #elif UNITY_EDITOR_OSX FileName = EditorUtil.Preference.MonoDirectory + "/bin/smcs", #endif Arguments = string.Join( " ", new[] { string.Format("-r:\"{0}\"", AssetUtil.CombinePath(ProjectInfo.UnityAssemblyRoot, "UnityEngine.dll")), string.Format("-r:\"{0}\"", AssetUtil.CombinePath(ProjectInfo.UnityAssemblyRoot, "UnityEditor.dll")), "-target:library", "-warnaserror+", string.Format("-out:\"{0}\"", outputPath), string.Join(" ", scripts.Select(x => string.Format("\"{0}\"", x)).ToArray()), }), CreateNoWindow = true, UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true, })) { if (onOutputReceived != null) { process.OutputDataReceived += (_, e) => onOutputReceived(e.Data); } if (onErrorReceived != null) { process.ErrorDataReceived += (_, e) => { result = false; onErrorReceived(e.Data); }; } process.WaitForExit(); } return(result && File.Exists(outputPath)); }
public static FileImportSettings FindOrCreate() { FileImportSettings settings; var settingsPath = AssetUtil.CombinePath( Path.GetDirectoryName(AssetUtil.FindScriptPath <FileImportSettings>()), "settings.asset"); if (!ScriptableObjectUtil.FindOrCreateObject(out settings, settingsPath)) { settings._items = new List <_Item>(); } return(settings); }
public AssemblyInfo(BuildSettings settings, string assembly, string[] sources) { Assembly = assembly; var assemblyName = Path.GetFileNameWithoutExtension(Assembly); Sources = sources.Select(x => new SourceInfo(x, settings, assemblyName)) .ToArray(); _settings = settings; _destRoot = AssetUtil.CombinePath( ProjectInfo.RootPath, _settings.SourceCodeCacheRoot, Path.GetFileNameWithoutExtension(assembly)); }
public string[] RevertToScripts(BuildSettings settings, string assemblyName) { var info = AssemblyInfo.LoadFromFile(settings, assemblyName); var error = false; // ソースコードの書き戻し先をチェック foreach (var source in info.Sources) { var scriptPath = source.GetCachedScriptPath(); var fileInfo = new FileInfo(scriptPath); if (!fileInfo.Exists) { Debug.LogErrorFormat("the CONFLICT is found at {0}", scriptPath); error = true; } } if (error) { return(null); } // ソースコードを書き戻す foreach (var source in info.Sources) { var scriptPath = source.GetCachedScriptPath(); var destAssetPath = source.AssetPath; var destPath = AssetUtil.CombinePath(ProjectInfo.RootPath, destAssetPath); AssetUtil.CopyFile(scriptPath, destPath, true); AssetDatabase.ImportAsset(destAssetPath); } // アセンブリを消す AssetDatabase.DeleteAsset(info.Assembly); info.CleanupCache(); return(info.Sources.Select(x => x.AssetPath).ToArray()); }
public static void OnPostprocessAllAssets(string[] importedAssets, string[] _1, string[] _2, string[] _3) { if (importedAssets.Length == 0) { return; } var settings = FileImportSettings.Find(); if (settings == null) { return; } var excelPath = importedAssets.FirstOrDefault(file => settings.IsEnabledFile(file)); if (excelPath == null) { return; } var outputDirectory = AssetUtil.CombinePath( Path.GetDirectoryName(excelPath), Path.GetFileNameWithoutExtension(excelPath)); Translator.UpdateDictionaries(excelPath, outputDirectory); var languages = new[] { "ja-JP", "en-US" }; foreach (var lang in languages) { var dicPath = AssetUtil.CombinePath(outputDirectory, lang + ".asset"); var dic = AssetDatabase.LoadAssetAtPath <DictionarySet>(dicPath); foreach (var page in dic.EnumeratePages()) { foreach (var key in dic.EnumerateKeys(page)) { Debug.LogFormat("{0} {1} {2} {3}", lang, page, key, dic.GetText(page, key)); } } } }
public static AssemblyInfo LoadFromFile(BuildSettings settings, string assemblyName) { var destRoot = AssetUtil.CombinePath( ProjectInfo.RootPath, settings.SourceCodeCacheRoot, assemblyName); var configPath = AssetUtil.CombinePath(destRoot, assemblyName + ".json"); var jsonStr = File.ReadAllText(configPath); var instance = JsonUtility.FromJson <AssemblyInfo>(jsonStr); instance._settings = settings; instance._destRoot = destRoot; foreach (var source in instance.Sources) { source.SetupInternal(settings, assemblyName); } return(instance); }
public static void ExportPackage() { var paths = Selection.objects.Select(obj => AssetDatabase.GetAssetPath(obj)) .ToArray(); var exportPath = AssetUtil.CombinePath( Preference.ExportDirectory, Preference.GetAssemblyName(paths) + ".unitypackage"); AssetUtil.DeleteFile(exportPath); if (!Directory.Exists(Path.GetDirectoryName(exportPath))) { Directory.CreateDirectory(Path.GetDirectoryName(exportPath)); } AssetDatabase.ExportPackage( paths, exportPath, ExportPackageOptions.Recurse | ExportPackageOptions.IncludeDependencies); }
public static bool UpdateDictionaries(string inputFile, string outputDirectory) { // とりえあずエクセルをそのまま読む var sheets = new Dictionary <string, Dictionary <string, Dictionary <string, string> > >(); using (var reader = new ExcelReader(inputFile)) { foreach (var sheet in reader.EnumerateSheets()) { sheets[sheet.Name] = sheet.ToDictionary(cell => cell.Header, cell => cell.StringValue); } } var pages = sheets.Keys.ToArray(); if (pages.Length == 0) { return(false); } // エクセルの中身を言語ごとにばらす using (new LockReloadAssetScope()) { var dicSets = new Dictionary <string, DictionarySet>(); string[] languages; { var firstSheet = sheets.First().Value; var firstRow = firstSheet.Values.First(); languages = firstRow.Keys.ToArray(); } var updated = false; // 言語ごとの辞書をつくる foreach (var lang in languages) { var path = AssetUtil.CombinePath(outputDirectory, lang + ".asset"); DictionarySet dicSet; if (File.Exists(path)) { dicSet = AssetDatabase.LoadAssetAtPath <DictionarySet>(path); } else { dicSet = DictionarySet.Create(path, pages); updated = true; } dicSets[lang] = dicSet; } // 辞書ごとに差分更新する foreach (var sheet in sheets) { var page = sheet.Key; var itemDics = sheet.Value; var currentItems = new List <string>(); foreach (var itemsDic in itemDics) { var itemName = itemsDic.Key; // テキスト名 var items = itemsDic.Value; // 全言語分のテキスト foreach (var dicSetItem in dicSets) { var dicLang = dicSetItem.Key; var dicSet = dicSetItem.Value; updated |= dicSet.SetText(page, itemName, items[dicLang]); currentItems.Add(itemName); } } currentItems.Distinct(); foreach (var dicSetItem in dicSets) { var dicSet = dicSetItem.Value; var removedKeys = dicSet.EnumerateKeys(page) .Except(currentItems) .ToArray(); foreach (var removedKey in removedKeys) { dicSet.RemoveText(sheet.Key, removedKey); updated = true; } } } if (updated) { AssetDatabase.SaveAssets(); } return(updated); } }