Created
July 7, 2020 22:13
-
-
Save kankikuchi/dfb44938abb07f2fc3c084ba6624e108 to your computer and use it in GitHub Desktop.
実績のローカライズデータ(xls OR xlsx)をvdfファイル変換するクラス【Unity】【Steam】
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
// AchievementLocalizedDataConverter.cs | |
// http://kan-kikuchi.hatenablog.com/entry/AchievementLocalizedDataConverter | |
// | |
// Created by kan.kikuchi on 2020.07.08. | |
using UnityEditor; | |
using UnityEngine; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Collections.Generic; | |
using NPOI.SS.UserModel; | |
using NPOI.XSSF.UserModel; | |
using NPOI.HSSF.UserModel; | |
/// <summary> | |
/// 実績のローカライズデータ(xls OR xlsx)をvdfファイル変換するクラス | |
/// </summary> | |
public static class AchievementLocalizedDataConverter{ | |
//変換したデータの文字列をまとめるやつ | |
private static StringBuilder _stringBuilder; | |
//================================================================================= | |
//変換 | |
//================================================================================= | |
//データを変換 | |
[MenuItem("Tools/Convert/Achievement Localized Data")] | |
private static void Convert() { | |
Debug.Log("実績のローカライズデータの変換実行"); | |
//データの最初の部分設定 | |
_stringBuilder = new StringBuilder(); | |
_stringBuilder.AppendLine("\"lang\""); | |
_stringBuilder.AppendLine("{"); | |
//データを変換 | |
ConvertBook("Assets/AchievementLocalizedData.xls"); | |
//データの最後の括弧を追加して書き出し | |
_stringBuilder.AppendLine("}"); | |
File.WriteAllText ("AchievementLocalizedData.vdf", _stringBuilder.ToString()); | |
} | |
//言語ごとの変換 | |
private static void ConvertByLanguage(string language, List<List<string>> cellTextsList) { | |
//言語ごとの前半部分追加 | |
_stringBuilder.AppendLine($"\"{language}\""); | |
_stringBuilder.AppendLine("{"); | |
_stringBuilder.AppendLine("\"Tokens\""); | |
_stringBuilder.AppendLine("{"); | |
//各データ部分の設定 | |
foreach (var cellTexts in cellTextsList) { | |
if (cellTexts.Count == 0) { | |
continue; | |
} | |
var id = cellTexts[0]; | |
var displayName = cellTextsList.Count <= 1 ? "" : cellTexts[1]; | |
_stringBuilder.Append($"\"NEW_ACHIEVEMENT_{id}_NAME\" \"{displayName}\""); | |
var description = cellTextsList.Count <= 2 ? "" : cellTexts[2]; | |
_stringBuilder.Append($"\"NEW_ACHIEVEMENT_{id}_DESC\" \"{description}\""); | |
} | |
//言語ごとの後半部分追加 | |
_stringBuilder.AppendLine("}"); | |
_stringBuilder.AppendLine("}"); | |
} | |
//================================================================================= | |
//エクセル変換 | |
//================================================================================= | |
//エクセルのパスを指定して変換 | |
private static void ConvertBook(string excelFilePath){ | |
//エクセルを読み込み、変換 | |
using (var stream = File.Open (excelFilePath, FileMode.Open, FileAccess.Read)) { | |
switch (System.IO.Path.GetExtension (excelFilePath)) { | |
case ".xls": | |
ConvertBook(new HSSFWorkbook (stream)); | |
break; | |
case ".xlsx": | |
ConvertBook(new XSSFWorkbook (stream)); | |
break; | |
default: | |
Debug.LogError($"{excelFilePath}は変換出来ません。xlsかxlsxファイルである必要があります。"); | |
break; | |
} | |
} | |
} | |
//エクセルのファイル単位で変換 | |
private static void ConvertBook(IWorkbook book){ | |
//各シートを取得し、変換 | |
for (var i = 0; i < book.NumberOfSheets; i++) { | |
ConvertSheet(book.GetSheetAt (i)); | |
} | |
} | |
//シート単位で変換 | |
private static void ConvertSheet(ISheet sheet){ | |
//何もシートに書かれていない場合はスルー | |
if(sheet.LastRowNum == 0){ | |
return; | |
} | |
//各行を取得していく(一番最初の行はスルー) | |
var cellTextsList = new List<List<string>>(); | |
for (int i = 1; i <= sheet.LastRowNum; i++) { | |
cellTextsList.Add(ConvertRow(sheet.GetRow (i))); | |
} | |
//言語別の変換を行う | |
ConvertByLanguage(sheet.SheetName, cellTextsList); | |
} | |
//行単位で変換 | |
private static List<string> ConvertRow(IRow row) { | |
return row == null ? new List<string>() : row.Cells.Select(t => t.ToString()).ToList(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment