Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Unity Beginner.. need help ! I want to load some data from xls, but when I reload always the same..

Discussion in '2D' started by amaiz742, Oct 20, 2020.

  1. amaiz742

    amaiz742

    Joined:
    Oct 20, 2020
    Posts:
    3
    Code (CSharp):
    1. #if UNITY_EDITOR
    2.  
    3. using NPOI.HSSF.UserModel;
    4. using NPOI.SS.UserModel;
    5. using UnityEditor;
    6.  
    7. #endif
    8.  
    9. using System;
    10. using System.CodeDom.Compiler;
    11. using System.Collections;
    12. using System.Collections.Generic;
    13. using UnityEngine;
    14. using System.Diagnostics;
    15. using System.IO;
    16. using System.Linq;
    17. using System.Runtime.CompilerServices;
    18. using System.Security.Cryptography.X509Certificates;
    19. using Random = System.Random;
    20. using UnityEngine.SceneManagement;
    21. using System.Threading;
    22. using Application = UnityEngine.Application;
    23. using System.ComponentModel.Design;
    24.  
    25. [RequireComponent(typeof(AudioSource))]
    26.  
    27. public class LocalizedTextAsset : MonoBehaviour
    28. {
    29.     public static string filename = "/NAMI TENTOU/Localization Tools/Resources/localization.xls";
    30.  
    31.     private static TextAsset fileAsTextAsset;
    32.  
    33.     public static Random RNG = new Random();
    34.  
    35.     public static int languageInt = RNG.Next(1, 255);
    36.  
    37.     public static string[,] grid;
    38.  
    39.     public static bool Initialized = true;
    40.  
    41.     public int
    42.             index;
    43.  
    44.     public int langIntView;
    45.  
    46.     [SerializeField]
    47.     private int setSize;
    48.     public void NewScene()
    49.     {
    50.         SceneManager.LoadScene("ChangeScene");
    51.     }
    52.  
    53.     public static int LanguageInt
    54.     {
    55.         get
    56.         {
    57.             if (grid == null) CreateLocalizationObject();
    58.             return languageInt;
    59.         }
    60.  
    61.         set
    62.         {
    63.             if (grid == null) CreateLocalizationObject();
    64.             if (value < 1) value = 1;
    65.             languageInt = value;
    66.         }
    67.  
    68.     }
    69.  
    70.     private string[] keyNames = new string[]
    71.     {
    72.  
    73.         "left shift", "right shift",
    74.         "left ctrl", "right ctrl",
    75.         "left alt", "right alt",
    76.         "space",
    77.         "up", "down", "right", "left",
    78.         "escape",
    79.         "f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8",
    80.         "f9", "f10", "f11", "f12", "f13", "f14", "f15",
    81.         "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
    82.         "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
    83.         "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
    84.         "`", "!", "@", "#", "$", "^", "&", "*", "(", ")", "-", "_", "=", "+",
    85.         "[", "]", "\\", ";", ":", "'", "\"", ",", "<", ".", ">", "/", "?",
    86.         "tab", "backspace", "delete",
    87.         "home", "end", "insert",
    88.         "page up", "page down",
    89.         "caps lock",
    90.         "numlock",
    91.         "scroll lock",
    92.         "pause", "clear", "return",
    93.     };
    94.  
    95.  
    96.     private void Start ()
    97.     {
    98.         UpdateText();
    99.     }
    100.  
    101.     public GameObject target;
    102.     public GameObject target2;
    103.  
    104.     public AudioClip impact, impact2;
    105.     private AudioSource audiosource;
    106.  
    107.  
    108.     public void Update ()
    109.     {
    110.         audiosource = GetComponent<AudioSource>();
    111.         foreach (var keyName in keyNames)
    112.         {
    113.             if (Input.GetKeyDown(keyName))
    114.             {
    115.                 if (keyName.Equals(grid[languageInt, 6], StringComparison.OrdinalIgnoreCase) == true)
    116.                 {
    117.                     UnityEngine.Debug.Log("Correct");
    118.                     target.SetActive(true);
    119.                     audiosource.PlayOneShot(impact);
    120.                     GetComponent<UnityEngine.UI.Text>().text = "";
    121.                     SceneManager.LoadScene("ChangeScene", LoadSceneMode.Additive);
    122.                     SceneManager.UnloadSceneAsync("SampleScene");
    123.  
    124.                 }
    125.                 else
    126.                 {
    127.                     UnityEngine.Debug.Log("Wrong");
    128.                     target2.SetActive(true);
    129.                     audiosource.PlayOneShot(impact2);
    130.                     GetComponent<UnityEngine.UI.Text>().text = "";
    131.                     SceneManager.LoadScene("ChangeScene", LoadSceneMode.Additive);
    132.                     SceneManager.UnloadSceneAsync("SampleScene");
    133.                 }
    134.             }
    135.         }
    136.     }
    137.  
    138.     private void UpdateText ()
    139.     {
    140.         if (GetComponent<GUIText>() != null)
    141.         {
    142.             GetComponent<GUIText>().text = GetLocalizedText(index);
    143.         }
    144.         if (GetComponent<TextMesh>() != null)
    145.         {
    146.             GetComponent<TextMesh>().text = GetLocalizedText(index);
    147.         }
    148.         if (GetComponent<UnityEngine.UI.Text>() != null)
    149.         {
    150.             GetComponent<UnityEngine.UI.Text>().text = GetLocalizedText(index);
    151.         }
    152.     }
    153.  
    154.     public static int gridXLength
    155.     {
    156.         get
    157.         {
    158.             if (grid == null) CreateLocalizationObject();
    159.             return grid.GetLength(0);
    160.         }
    161.     }
    162.  
    163.     public static void CreateLocalizationObject()
    164.     {
    165.         grid = readXLS();
    166.     }
    167.  
    168.     public static string[,] readXLS()
    169.     {
    170.         string[,] returnedGrid = new string[0, 0];
    171.         string name = filename;
    172.         string fileCSV = "";
    173.  
    174. #if UNITY_EDITOR
    175.         //Generates your temp file in case that you happen to have your XLS open at the same time
    176.         FileUtil.DeleteFileOrDirectory(Application.dataPath + filename + "temp");
    177.  
    178.         if (!File.Exists(Application.dataPath + filename))
    179.         {
    180.             createNewFile();
    181.         }
    182.  
    183.         FileUtil.CopyFileOrDirectory(Application.dataPath + filename, Application.dataPath + filename + "temp");
    184.  
    185.         using (FileStream stream = File.Open(Application.dataPath + filename + "temp", FileMode.Open, FileAccess.Read))
    186.         {
    187.             IWorkbook book = new HSSFWorkbook(stream);
    188.  
    189.             //Multiple Sheet Support? Maybe?
    190.             ISheet sheet = book.GetSheetAt(0);
    191.             int gridX = sheet.GetRow(0).LastCellNum;
    192.             int gridY = sheet.LastRowNum + 1;
    193.  
    194.             returnedGrid = new string[gridX, gridY];
    195.  
    196.             for (int y = 0; y < gridY; y++)
    197.             {
    198.                 IRow row = sheet.GetRow(y);
    199.  
    200.                 for (int x = 0; x < gridX; x++)
    201.                 {
    202.                     if (row == null) continue;
    203.                     if (row.PhysicalNumberOfCells == 0) continue;
    204.  
    205.                     ICell cell = row.GetCell(x);
    206.                     if (cell != null)
    207.                     {
    208.                         cell.SetCellType(CellType.String);
    209.                         returnedGrid[x, y] = cell.StringCellValue;
    210.  
    211.                         //Replacing | with @p since that's our "comma" seperator
    212.                         string tempCell = cell.StringCellValue.Replace("|", ",,,p");
    213.                         //Replacing @ with @a since that's our new line seperator
    214.                         tempCell = tempCell.Replace("@", ",,,a");
    215.  
    216.                         if (x + 1 != gridX) fileCSV += tempCell + "|";
    217.                         else fileCSV += tempCell;
    218.                     }
    219.                     if (cell == null)
    220.                     {
    221.                         returnedGrid[x, y] = " ";
    222.                         string tempCell = " ";
    223.  
    224.                         if (x + 1 != gridX) fileCSV += tempCell + "|";
    225.                         else fileCSV += tempCell;
    226.                     }
    227.                 }
    228.  
    229.                 if (y + 1 != gridY) fileCSV += "@";
    230.             }
    231.             File.WriteAllText(Application.dataPath + "/NAMI TENTOU/Localization Tools/Resources/compiledbuild.txt", fileCSV);
    232.         }
    233.  
    234. #endif
    235.         if (Application.isPlaying)
    236.         {
    237.             TextAsset ta = Resources.Load("compiledbuild") as TextAsset;
    238.  
    239.             fileCSV = ta.text;
    240.         }
    241.         string[] tempGrid = fileCSV.Split('@');
    242.         int width = tempGrid[0].Split('|').Length;
    243.         int height = tempGrid.Length;
    244.  
    245.         returnedGrid = new string[width, height];
    246.  
    247.         for (int y = 0; y < height; y++)
    248.         {
    249.             string[] tempGrid2 = tempGrid[y].Split('|');
    250.             for (int x = 0; x < tempGrid2.Length; x++)
    251.             {
    252.                 string tempString = tempGrid2[x].Replace(",,,a", "@");
    253.                 tempString = tempString.Replace(",,,p", "|");
    254.  
    255.                 returnedGrid[x, y] = tempString;
    256.             }
    257.         }
    258.  
    259.         return returnedGrid;
    260.     }
    261.  
    262.     public static void createNewFile()
    263.     {
    264. #if UNITY_EDITOR
    265.  
    266.         using (FileStream stream = File.Create(Application.dataPath + filename))
    267.         {
    268.             IWorkbook book = new HSSFWorkbook();
    269.  
    270.             //Multiple SHeet Support?
    271.             ISheet sheet = book.CreateSheet();
    272.             IRow row = sheet.CreateRow(0);
    273.             ICell cell = row.CreateCell(0);
    274.             cell.SetCellType(CellType.String);
    275.             cell.SetCellValue(Application.productName + " Localization");
    276.  
    277.             ICell cell2 = row.CreateCell(1);
    278.             cell2.SetCellType(CellType.String);
    279.             cell2.SetCellValue("English");
    280.             ICell cell3 = row.CreateCell(2);
    281.             cell3.SetCellType(CellType.String);
    282.             cell3.SetCellValue("French");
    283.             ICell cell4 = row.CreateCell(3);
    284.             cell4.SetCellType(CellType.String);
    285.             cell4.SetCellValue("Italian");
    286.             ICell cell5 = row.CreateCell(4);
    287.             cell5.SetCellType(CellType.String);
    288.             cell5.SetCellValue("German");
    289.             ICell cell6 = row.CreateCell(5);
    290.             cell6.SetCellType(CellType.String);
    291.             cell6.SetCellValue("Spanish");
    292.  
    293.             IRow row2 = sheet.CreateRow(1);
    294.  
    295.             for (int i = 0; i < row.Cells.Count; i++)
    296.             {
    297.                 ICell newCell = row2.CreateCell(i);
    298.                 newCell.SetCellType(CellType.String);
    299.                 newCell.SetCellValue(" ");
    300.             }
    301.  
    302.             sheet.CreateRow(1);
    303.  
    304.             book.Write(stream);
    305.         }
    306. #endif
    307.     }
    308.  
    309.     private static string[,] CrossPlatformReader(string s)
    310.     {
    311.         string[,] result;
    312.  
    313.         string[] arrayY = s.Split('@');
    314.         int gridY = arrayY.Length;
    315.         string[] arrayX = arrayY[0].Split('|');
    316.         int gridX = arrayX.Length;
    317.  
    318.         result = new string[gridX, gridY];
    319.  
    320.         //This is reading our simple txt csv file so that the file can be read across multiple platforms
    321.         for (int y = 0; y < gridY; y++)
    322.         {
    323.         }
    324.  
    325.         return result;
    326.     }
    327.  
    328.     public static void writeToFile()
    329.     {
    330. #if UNITY_EDITOR
    331.         using (FileStream stream = File.Open(Application.dataPath + filename, FileMode.Open, FileAccess.Write))
    332.         {
    333.             IWorkbook book = new HSSFWorkbook();
    334.  
    335.             //Multiple SHeet Support?
    336.             ISheet sheet = book.CreateSheet();
    337.             int gridX = grid.GetLength(0);
    338.             int gridY = grid.GetLength(1);
    339.  
    340.             for (int y = 0; y < gridY; y++)
    341.             {
    342.                 IRow row = sheet.CreateRow(y);
    343.                 for (int x = 0; x < gridX; x++)
    344.                 {
    345.                     ICell cell = row.CreateCell(x);
    346.  
    347.                     cell.SetCellType(CellType.String);
    348.                     cell.SetCellValue(grid[x, y]);
    349.                 }
    350.             }
    351.  
    352.             book.Write(stream);
    353.         }
    354.  
    355.         readXLS();
    356.  
    357. #endif
    358.     }
    359.  
    360.     public static void AddNewRow()
    361.     {
    362. #if UNITY_EDITOR
    363.         using (FileStream stream = File.Open(Application.dataPath + filename, FileMode.Open, FileAccess.Write))
    364.         {
    365.             IWorkbook book = new HSSFWorkbook();
    366.  
    367.             //Multiple SHeet Support?
    368.             ISheet sheet = book.CreateSheet();
    369.             int gridX = grid.GetLength(0);
    370.             int gridY = grid.GetLength(1);
    371.  
    372.             for (int y = 0; y < gridY; y++)
    373.             {
    374.                 IRow row = sheet.CreateRow(y);
    375.                 for (int x = 0; x < gridX; x++)
    376.                 {
    377.                     ICell cell = row.CreateCell(x);
    378.  
    379.                     cell.SetCellType(CellType.String);
    380.                     cell.SetCellValue(grid[x, y]);
    381.                 }
    382.             }
    383.  
    384.             sheet.CreateRow(gridY);
    385.  
    386.             book.Write(stream);
    387.         }
    388.  
    389.         readXLS();
    390.  
    391. #endif
    392.     }
    393.  
    394.     public static int getColumnLength()
    395.     {
    396.         if (grid == null) CreateLocalizationObject();
    397.         if (grid.GetLength(1) == 0) return -1;
    398.         return grid.GetLength(1) - 1;
    399.     }
    400.  
    401.  
    402.     /// <summary>
    403.     /// Reads text from specific line from document
    404.     /// </summary>
    405.     public static string GetLocalizedText(int row)
    406.     {
    407.         if (row < 0) row = 0;
    408.  
    409.         string newString = "";
    410.         if (grid == null) CreateLocalizationObject();
    411.  
    412.         if (row < grid.GetLength(1))
    413.         {
    414.             newString = grid[languageInt, row];
    415.         }
    416.  
    417.         return newString;
    418.     }
    419.  
    420.     /// <summary>
    421.     /// Reads text from specific line from document while allowing you to manually specify language
    422.     /// </summary>
    423.     public static string GetLocalizedText(int langInt, int row)
    424.     {
    425.         if (row < 0) row = 0;
    426.  
    427.         string newString = "";
    428.         if (grid == null) CreateLocalizationObject();
    429.         if (row < grid.GetLength(0))
    430.         {
    431.             newString = grid[langInt, row];
    432.         }
    433.         return newString;
    434.  
    435.     }
    436.  
    437.     public static string GetLanguageName(int i)
    438.     {
    439.         return grid[i, 0];
    440.  
    441.     }
    442.  
    443. }
    444.  
    and this is my script , how can i reload xls data at my text? maybe i shouldn't use this method?
     
    Last edited: Oct 20, 2020