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/cn.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灯, 水草

请输入看板名称,例如:Gossiping站内搜寻

TOP