GameDesign 板


LINE

目前剛學習UNITY不久,想跟各位大德請教一些問題,希望各位可以稍微開導我一下 我遇到的問題是無論我怎麼重新載入場景,我從excel上抓出來匯入的text檔,怎樣都不會改變他的值 簡而言之,就是我想要每次載入獲得的資料都不同! 但目前不知道是哪裡出了問題.想請教各位! 以下是我掛在text下的程式碼 手機排版 傷眼抱歉 #if UNITY_EDITOR using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using UnityEditor; #endif using System; using System.CodeDom.Compiler; using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Diagnostics; using System.IO; using System.Linq; using System.Runtime.CompilerServices; using System.Security.Cryptography.X509Certificates; using Random = System.Random; using UnityEngine.SceneManagement; using System.Threading; using Application = UnityEngine.Application; using System.ComponentModel.Design; [RequireComponent(typeof(AudioSource))] public class LocalizedTextAsset : MonoBehaviour { public static string filename = "/NAMI TENTOU/Localization Tools/Resources/localization.xls"; private static TextAsset fileAsTextAsset; public static Random RNG = new Random(); public static int languageInt = RNG.Next(1, 255); public static string[,] grid; public static bool Initialized = true; public int index; public int langIntView; [SerializeField] private int setSize; public void NewScene() { SceneManager.LoadScene("ChangeScene"); } public static int LanguageInt { get { if (grid == null) CreateLocalizationObject(); return languageInt; } set { if (grid == null) CreateLocalizationObject(); if (value < 1) value = 1; languageInt = value; } } private string[] keyNames = new string[] { "left shift", "right shift", "left ctrl", "right ctrl", "left alt", "right alt", "space", "up", "down", "right", "left", "escape", "f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "`", "!", "@", "#", "$", "^", "&", "*", "(", ")", "-", "_", "=", "+", "[", "]", "\\", ";", ":", "'", "\"", ",", "<", ".", ">", "/", "?", "tab", "backspace", "delete", "home", "end", "insert", "page up", "page down", "caps lock", "numlock", "scroll lock", "pause", "clear", "return", }; private void Start () { UpdateText(); } public GameObject target; public GameObject target2; public AudioClip impact, impact2; private AudioSource audiosource; public void Update () { audiosource = GetComponent<AudioSource>(); foreach (var keyName in keyNames) { if (Input.GetKeyDown(keyName)) { if (keyName.Equals(grid[languageInt, 6], StringComparison.OrdinalIgnoreCase) == true) { UnityEngine.Debug.Log("Correct"); target.SetActive(true); audiosource.PlayOneShot(impact); GetComponent<UnityEngine.UI.Text>().text = ""; Invoke("NewScene", 1f); } else { UnityEngine.Debug.Log("Wrong"); target2.SetActive(true); audiosource.PlayOneShot(impact2); GetComponent<UnityEngine.UI.Text>().text = ""; Invoke("NewScene", 1f); } } } } private void UpdateText () { if (GetComponent<GUIText>() != null) { GetComponent<GUIText>().text = GetLocalizedText(index); } if (GetComponent<TextMesh>() != null) { GetComponent<TextMesh>().text = GetLocalizedText(index); } if (GetComponent<UnityEngine.UI.Text>() != null) { GetComponent<UnityEngine.UI.Text>().text = GetLocalizedText(index); } } public static int gridXLength { get { if (grid == null) CreateLocalizationObject(); return grid.GetLength(0); } } public static void CreateLocalizationObject() { grid = readXLS(); } public static string[,] readXLS() { string[,] returnedGrid = new string[0, 0]; string name = filename; string fileCSV = ""; #if UNITY_EDITOR //Generates your temp file in case that you happen to have your XLS open at the same time FileUtil.DeleteFileOrDirectory(Application.dataPath + filename + "temp"); if (!File.Exists(Application.dataPath + filename)) { createNewFile(); } FileUtil.CopyFileOrDirectory(Application.dataPath + filename, Application.dataPath + filename + "temp"); using (FileStream stream = File.Open(Application.dataPath + filename + "temp", FileMode.Open, FileAccess.Read)) { IWorkbook book = new HSSFWorkbook(stream); //Multiple Sheet Support? Maybe? ISheet sheet = book.GetSheetAt(0); int gridX = sheet.GetRow(0).LastCellNum; int gridY = sheet.LastRowNum + 1; returnedGrid = new string[gridX, gridY]; for (int y = 0; y < gridY; y++) { IRow row = sheet.GetRow(y); for (int x = 0; x < gridX; x++) { if (row == null) continue; if (row.PhysicalNumberOfCells == 0) continue; ICell cell = row.GetCell(x); if (cell != null) { cell.SetCellType(CellType.String); returnedGrid[x, y] = cell.StringCellValue; //Replacing | with @p since that's our "comma" seperator string tempCell = cell.StringCellValue.Replace("|", ",,,p"); //Replacing @ with @a since that's our new line seperator tempCell = tempCell.Replace("@", ",,,a"); if (x + 1 != gridX) fileCSV += tempCell + "|"; else fileCSV += tempCell; } if (cell == null) { returnedGrid[x, y] = " "; string tempCell = " "; if (x + 1 != gridX) fileCSV += tempCell + "|"; else fileCSV += tempCell; } } if (y + 1 != gridY) fileCSV += "@"; } File.WriteAllText(Application.dataPath + "/NAMI TENTOU/Localization Tools/Resources/compiledbuild.txt", fileCSV); } #endif if (Application.isPlaying) { TextAsset ta = Resources.Load("compiledbuild") as TextAsset; fileCSV = ta.text; } string[] tempGrid = fileCSV.Split('@'); int width = tempGrid[0].Split('|').Length; int height = tempGrid.Length; returnedGrid = new string[width, height]; for (int y = 0; y < height; y++) { string[] tempGrid2 = tempGrid[y].Split('|'); for (int x = 0; x < tempGrid2.Length; x++) { string tempString = tempGrid2[x].Replace(",,,a", "@"); tempString = tempString.Replace(",,,p", "|"); returnedGrid[x, y] = tempString; } } return returnedGrid; } public static void createNewFile() { #if UNITY_EDITOR using (FileStream stream = File.Create(Application.dataPath + filename)) { IWorkbook book = new HSSFWorkbook(); //Multiple SHeet Support? ISheet sheet = book.CreateSheet(); IRow row = sheet.CreateRow(0); ICell cell = row.CreateCell(0); cell.SetCellType(CellType.String); cell.SetCellValue(Application.productName + " Localization"); ICell cell2 = row.CreateCell(1); cell2.SetCellType(CellType.String); cell2.SetCellValue("English"); ICell cell3 = row.CreateCell(2); cell3.SetCellType(CellType.String); cell3.SetCellValue("French"); ICell cell4 = row.CreateCell(3); cell4.SetCellType(CellType.String); cell4.SetCellValue("Italian"); ICell cell5 = row.CreateCell(4); cell5.SetCellType(CellType.String); cell5.SetCellValue("German"); ICell cell6 = row.CreateCell(5); cell6.SetCellType(CellType.String); cell6.SetCellValue("Spanish"); IRow row2 = sheet.CreateRow(1); for (int i = 0; i < row.Cells.Count; i++) { ICell newCell = row2.CreateCell(i); newCell.SetCellType(CellType.String); newCell.SetCellValue(" "); } sheet.CreateRow(1); book.Write(stream); } #endif } private static string[,] CrossPlatformReader(string s) { string[,] result; string[] arrayY = s.Split('@'); int gridY = arrayY.Length; string[] arrayX = arrayY[0].Split('|'); int gridX = arrayX.Length; result = new string[gridX, gridY]; //This is reading our simple txt csv file so that the file can be read across multiple platforms for (int y = 0; y < gridY; y++) { } return result; } public static void writeToFile() { #if UNITY_EDITOR using (FileStream stream = File.Open(Application.dataPath + filename, FileMode.Open, FileAccess.Write)) { IWorkbook book = new HSSFWorkbook(); //Multiple SHeet Support? ISheet sheet = book.CreateSheet(); int gridX = grid.GetLength(0); int gridY = grid.GetLength(1); for (int y = 0; y < gridY; y++) { IRow row = sheet.CreateRow(y); for (int x = 0; x < gridX; x++) { ICell cell = row.CreateCell(x); cell.SetCellType(CellType.String); cell.SetCellValue(grid[x, y]); } } book.Write(stream); } readXLS(); #endif } public static void AddNewRow() { #if UNITY_EDITOR using (FileStream stream = File.Open(Application.dataPath + filename, FileMode.Open, FileAccess.Write)) { IWorkbook book = new HSSFWorkbook(); //Multiple SHeet Support? ISheet sheet = book.CreateSheet(); int gridX = grid.GetLength(0); int gridY = grid.GetLength(1); for (int y = 0; y < gridY; y++) { IRow row = sheet.CreateRow(y); for (int x = 0; x < gridX; x++) { ICell cell = row.CreateCell(x); cell.SetCellType(CellType.String); cell.SetCellValue(grid[x, y]); } } sheet.CreateRow(gridY); book.Write(stream); } readXLS(); #endif } public static int getColumnLength() { if (grid == null) CreateLocalizationObject(); if (grid.GetLength(1) == 0) return -1; return grid.GetLength(1) - 1; } /// <summary> /// Reads text from specific line from document /// </summary> public static string GetLocalizedText(int row) { if (row < 0) row = 0; string newString = ""; if (grid == null) CreateLocalizationObject(); if (row < grid.GetLength(1)) { newString = grid[languageInt, row]; } return newString; } /// <summary> /// Reads text from specific line from document while allowing you to manually specify language /// </summary> public static string GetLocalizedText(int langInt, int row) { if (row < 0) row = 0; string newString = ""; if (grid == null) CreateLocalizationObject(); if (row < grid.GetLength(0)) { newString = grid[langInt, row]; } return newString; } public static string GetLanguageName(int i) { return grid[i, 0]; } } 還請各位幫忙! 謝謝GameDesign版 謝謝各位 --



※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 101.10.30.213 (臺灣)
※ 文章網址: https://webptt.com/m.aspx?n=bbs/GameDesign/M.1603209031.A.162.html
1F:推 cooper6334: 沒有特別需求的話,先轉成csv檔再讀? 10/21 00:26
2F:→ cooper6334: 原來你在程式裡面做了,不能事先轉好檔案嗎? 10/21 00:32
3F:推 ura1210: 我建議把程式碼放在github分享比較好看 10/21 12:11
4F:→ sirius65482: 你是卡在GetLocalizedText()回傳的字串不正常?? 10/21 16:51
5F:→ legendyelon: 謝謝大家意見提供 10/22 01:37
6F:→ legendyelon: 成功解決這個問題了 10/22 01:37







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:Boy-Girl站內搜尋

TOP