Spreadsheet読み込み追加

This commit is contained in:
kimura 2021-09-09 19:24:53 +09:00
parent 9250e4da0b
commit eaee18ed40
8 changed files with 291 additions and 5 deletions

View File

@ -0,0 +1,132 @@
using UnityEditor;
using UnityEngine;
using System;
using System.IO;
using System.Collections.Generic;
public class DataDownloader {
private static readonly string WebServiceUrl = "https://script.google.com/macros/s/AKfycbyenMGJNEy3M_P37lWZavGe0gwLMI91qiew12XE6kmnsLdcwAI/exec";
private static readonly string SpreadsheetId = "15ReD1nkGL60QdvJdGOYbjwRXMPIBclhXD5vCWOmbmew";
private static readonly string LangSpreadsheetId = "1Eq7VNB2MqzdotBwaf71dqZ1MIU_HrEjPxIfNBeFlTr8";
private static readonly string Password = "hogehogehogeo";
private static readonly string DirectoryPath = "Assets/MyGame/Resources/Data";
// Googleに短時間アクセスで怒られるので仕方なくSleep
private static readonly int SleepTime = 1000;
[MenuItem("Tools/SpreadsheetTool/DataUpdate")]
static void DataUpdate(){
List<Tuple<string, string>> downloadedDataList = new List<Tuple<string, string>>();
int count = Const.BaseSheetNameList.Length;
bool isError = false;
foreach(Const.SheetData baseNameData in Const.BaseSheetNameList){
string fileName = baseNameData.name;
string connectionString = CreateConnectionString(fileName);
DataDownload(connectionString, fileName, (str, data) => {
downloadedDataList.Add(Tuple.Create<string, string>(fileName, data));
if(--count <= 0){
if(!isError){
Debug.Log("DataDownload is Done!!");
int result = SaveDownloadedDataList(downloadedDataList, DirectoryPath);
if(result > 0){
File.WriteAllText(string.Format("{0}/DownloadDate.txt", DirectoryPath), AvoEx.AesEncryptor.Encrypt(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")));
}
AssetDatabase.Refresh();
} else{
Debug.LogError("エラーデータがダウンロードされたので、再度DataUpdateを行なって下さい。");
}
}
});
System.Threading.Thread.Sleep(SleepTime);
}
}
[MenuItem("Tools/SpreadsheetTool/LanguageDataUpdate")]
static void LanguageDataUpdate(){
List<Tuple<string, string>> downloadedDataList = new List<Tuple<string, string>>();
int count = Const.LanguageSheetNameList.Length;
bool isError = false;
foreach(Const.SheetData LanguageData in Const.LanguageSheetNameList){
string fileName = LanguageData.name;
string connectionString = CreateConnectionString(LangSpreadsheetId,fileName);
DataDownload(connectionString, fileName, (str, data) => {
downloadedDataList.Add(Tuple.Create<string, string>(fileName, data));
if(--count <= 0){
if(!isError){
Debug.Log("DataDownload is Done!!");
int result = SaveDownloadedDataList(downloadedDataList, DirectoryPath);
if(result > 0){
File.WriteAllText(string.Format("{0}/DownloadDate.txt", DirectoryPath), AvoEx.AesEncryptor.Encrypt(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")));
}
AssetDatabase.Refresh();
} else{
Debug.LogError("エラーデータがダウンロードされたので、再度DataUpdateを行なって下さい。");
}
}
});
System.Threading.Thread.Sleep(SleepTime);
}
}
private static string CreateConnectionString(string sheetName){
return CreateConnectionString(SpreadsheetId, sheetName);
}
private static string CreateConnectionString(string spreadsheetId, string sheetName){
return WebServiceUrl + "?ssid=" + spreadsheetId + "&sheet=" + sheetName + "&pass=" + Password + "&action=GetData";
}
private static void DataDownload(string connectionString, string fileName, Action<string, string> callback){
Debug.LogFormat ("BeginDataDownload {0}", fileName);
WWW www = new WWW(connectionString);
ContinuationManager.Add(() => www.isDone, () => {
if(!string.IsNullOrEmpty(www.error)){
Debug.Log("WWW failed: " + www.error);
}
Debug.LogFormat("EndDataDownload {0}", fileName);
string result = www.text;
// Keyが空白なセル情報を破棄
while(result.Contains(",\"\":")){
int begin = result.IndexOf(",\"\":");
int end = result.IndexOf("}", begin);
result = result.Remove(begin, end - begin);
}
while(result.Contains(",{\"\":")){
int begin = result.IndexOf(",{\"\":");
int end = result.IndexOf("}", begin) + 1;
result = result.Remove(begin, end - begin);
}
Debug.Log(result);
callback(result, AvoEx.AesEncryptor.Encrypt(result));
});
}
private static int SaveDownloadedDataList(List<Tuple<string, string>> downloadedDataList, string directoryPath){
if(!Directory.Exists(directoryPath)){
Directory.CreateDirectory(directoryPath);
}
int updateCount = 0;
Debug.Log("BeginDataUpdate");
foreach(var data in downloadedDataList){
string path = string.Format(directoryPath + "/{0}.txt", data.Item1);
string newData = AvoEx.AesEncryptor.DecryptString(data.Item2);
string oldData = "";
try {
oldData = AvoEx.AesEncryptor.DecryptString(File.ReadAllText(path));
} catch(Exception){
}
if(newData != oldData){
++updateCount;
Debug.Log("Update " + data.Item1);
File.WriteAllText(path, data.Item2);
}
}
Debug.Log("EndDataUpdate updateCount:" + updateCount);
return updateCount;
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 2578de5608cf4a5aad2276cf86270849
timeCreated: 1631181808

View File

@ -3,11 +3,6 @@ using UniRx;
using UnityEngine;
using UnityEngine.UI;
public enum ShopItemType
{
Consumable,
NonConsumable,
}
public class ShopItemPurchaseButton : MonoBehaviour
{
private ShopItemType itemType;

View File

@ -6,6 +6,8 @@ public static class Const {
public static readonly string DefaultAppVersion = "0.1.0";
// parameter
// tag
public static readonly string GameDataTag = "GameData";
public static readonly string VibrationTag = "Vibration";
@ -16,4 +18,42 @@ public static class Const {
public static readonly string LineURL = "http://nav.cx/aVZ8nEK";
public static readonly string FacebookURL = "https://www.facebook.com/usayaApp/";
public static readonly string TwitterURL = "https://twitter.com/usayaApp";
// sheet
public static readonly string AvatarDataSheet = "AvatarData";
public static readonly string ShopDataSheet = "ShopData";
// language
public static readonly string LangJapanDataSheet = "LangJapanData";
public static readonly string LangEnglishDataSheet = "LangEnglishData";
public static readonly string LangChineseTraditionaldDataSheet = "LangChineseTraditionaldData";
public static readonly string LangChineseSimplifiedDataSheet = "LangChineseSimplifiedData";
public static readonly string UILangJapanDataSheet = "UILangJapanData";
public static readonly string UILangEnglishDataSheet = "UILangEnglishData";
public static readonly string UILangChineseTraditionaldDataSheet = "UILangChineseTraditionaldData";
public static readonly string UILangChineseSimplifiedDataSheet = "UILangChineseSimplifiedData";
public sealed class SheetData {
public string name;
public string className;
public SheetData(string name) : this(name, name){}
public SheetData(string name, string className){
this.name = name;
this.className = className;
}
}
public static readonly SheetData[] BaseSheetNameList = new SheetData[]{
// new SheetData(Const.AvatarDataSheet),
new SheetData(Const.ShopDataSheet),
};
public static readonly SheetData[] LanguageSheetNameList = new SheetData[]{
new SheetData(Const.LangJapanDataSheet, "LanguageData"),
// new SheetData(Const.LangEnglishDataSheet, "LanguageData"),
// new SheetData(Const.LangChineseTraditionaldDataSheet, "LanguageData"),
// new SheetData(Const.LangChineseSimplifiedDataSheet, "LanguageData"),
new SheetData(Const.UILangJapanDataSheet, "LanguageData"),
// new SheetData(Const.UILangEnglishDataSheet, "LanguageData"),
// new SheetData(Const.UILangChineseTraditionaldDataSheet, "LanguageData"),
// new SheetData(Const.UILangChineseSimplifiedDataSheet, "LanguageData"),
};
}

View File

@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public enum ItemCategory
{
Material = 1,
Recipe = 2,
Pan = 3,
}
public enum ShopItemType
{
Consumable,
NonConsumable,
}
[Serializable]
public sealed class ShopData {
public int id;
public int nameId;
public string name;
public int price;
public string text;
public int category;
public int consumeType;
public string prefabName;
public ItemCategory Category => (ItemCategory)category;
public ShopItemType ConsumeType => (ShopItemType)consumeType;
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: b66f3f846b214a52bb597ea8a8b2aca6
timeCreated: 1631174370

View File

@ -0,0 +1,79 @@
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using AvoEx;
public sealed class SpreadsheetDataManager : SingletonMonoBehaviour<SpreadsheetDataManager> {
private Dictionary<string, List<object>> dataDictionary = new Dictionary<string, List<object>>();
private string downloadDate;
public string DownloadDateString {
get{ return downloadDate; }
}
void Awake(){
if(CheckInstance()) return ;
downloadDate = LoadData("DownloadDate");
}
public bool LoadAndCache<T>(string worksheetName){
string typeName = typeof(T).ToString();
string json = LoadData(worksheetName);
dataDictionary[worksheetName] = JsonHelper.GetJsonArray(json, typeName).ToList();
return dataDictionary.ContainsKey(worksheetName);
}
public bool ExistsDataList(string worksheetName){
return dataDictionary.ContainsKey(worksheetName);
}
public List<T> GetBaseDataList<T>(string worksheetName){
if(!ExistsDataList(worksheetName)) LoadAndCache<T>(worksheetName);
// HACK
return dataDictionary[worksheetName].Cast<T>().ToList();
}
#if UNITY_EDITOR
public static List<T> Load<T>(string worksheetName){
string typeName = typeof(T).ToString();
string json = LoadData(worksheetName);
return JsonHelper.GetJsonArray(json, typeName).Cast<T>().ToList();
}
private static string LoadData(string assetName){
#else
private string LoadData(string assetName){
#endif
string path = "Data/" + assetName;
TextAsset textAsset = Resources.Load<TextAsset>(path);
return AesEncryptor.DecryptString(textAsset.text);
}
}
public static class JsonHelper{
public static object[] GetJsonArray(string json, string typeName){
return GetJsonArray(json, Type.GetType(typeName));
}
public static object[] GetJsonArray(string json, Type type){
// JsonHelper型
Type jh = typeof(JsonHelper);
// GetJsonArray Method
MethodInfo mi = jh.GetMethod("JsonToArray");
// GetJsonArray<Type> Method
MethodInfo miConstructed = mi.MakeGenericMethod(type);
string[] args = { json };
object result = miConstructed.Invoke(null, args);
return ((IEnumerable)result).Cast<object>().ToArray();
}
public static T[] JsonToArray<T>(string json){
string newJson = "{ \"array\": " + json + "}";
return JsonUtility.FromJson<Wrapper<T>>(newJson).array;
}
[Serializable]
private class Wrapper<T>{
public T[] array = null;
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 992ca0f9e68a447b8f3d9ea096088ae4
timeCreated: 1631173558