Search Unity

How to reload Scene in Unity and change my excel variables?

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