Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by hollym16 · Dec 01, 2017 at 01:52 PM · arraytextassetbundlelocalizationcsv

Add asset to array during runtime

I have an existing script that translates my text using a CSV file that is assigned to a public array in the Inspector.

I now want to store this CSV file on an external server, pull it down when the App starts, and apply it to the array so the other scripts can still reference it in the same way.

I know how to download the asset, I just need some help on how to add it to the array once its downloaded. EDIT: Here are 2 scripts that reference the CSV file: This script is where the CSV file can be assigned in the Inspector.

 #if UNITY_EDITOR
 using System.IO;
 using UnityEditor;
 #endif
 using UnityEngine;
 using UnityEngine.Networking;
 
 public class TextLocalizationFiles : ScriptableObject
 {
     public TextAsset[] files;
 
 #if UNITY_EDITOR
     [MenuItem("Component/UI/Localization Asset")]
     public static void CreateLocalizationAsset()
     {
         CreateAsset<TextLocalizationFiles>();
     }
     private static void CreateAsset<T>() where T : ScriptableObject
     {
         T asset = CreateInstance<T>();
 
         string path = AssetDatabase.GetAssetPath(Selection.activeObject);
         if (string.IsNullOrEmpty(path))
         {
             path = "Assets";
         }
         else if (!string.IsNullOrEmpty(Path.GetExtension(path)))
         {
             path = Path.GetDirectoryName(path);
         }
 
         if (!path.Contains("Resources"))
         {
             string newpath = Path.Combine(path, "Resources");
             if (!Directory.Exists(newpath))
                 AssetDatabase.CreateFolder(path, "Resources");
             path = newpath;
         }
 
         string assetPathAndName = AssetDatabase.GenerateUniqueAssetPath(Path.Combine(path, "Localization.asset"));
         if (File.Exists(assetPathAndName))
         {
             Debug.LogWarning(string.Format("An asset called Localization was already found at {0}. There can be only one.", path));
             return;
         }
         //Debug.Log("Creating asset " + assetPathAndName + " at " + path);
         AssetDatabase.CreateAsset(asset, assetPathAndName);
 
         AssetDatabase.SaveAssets();
         AssetDatabase.Refresh();
         EditorUtility.FocusProjectWindow();
         Selection.activeObject = asset;
     }
 #endif
 }


This script references the 'files' array:

 using System.Runtime.InteropServices;
 using UnityEngine;
 using System.Collections.Generic;
 using UnityEngine.EventSystems;
 
 public static class TextLocalization
 {
     public static event Localize LocalizationChanged;
 
     private static readonly Dictionary<string, string[]> dictionary = new Dictionary<string, string[]>();
 
     private static TextLocalizationFiles localizationAsset;
     private static int nbLocaFiles;
 
     private static int currentLanguageIndex = -1;
     
     public static string[] AvailableLanguages { get; private set; }
     public static string CurrentLanguage { get { return AvailableLanguages[currentLanguageIndex]; } }
     public static List<string> Keys { get { return new List<string>(dictionary.Keys); }}  
 
     public static void Init()
     {
         localizationAsset = Resources.Load<TextLocalizationFiles>("Localization");
         if (nbLocaFiles != localizationAsset.files.Length || dictionary.Count == 0)
         {
             dictionary.Clear();
             foreach (TextAsset asset in localizationAsset.files)
                 Load(asset);
             nbLocaFiles = localizationAsset.files.Length;
         }
     }
     public static bool Load(TextAsset txt)
     {
         if (txt != null && LoadCSV(txt)) 
             return true;
         return false;
     }
     private static bool LoadCSV(TextAsset asset)
     {
         CSVReader reader = new CSVReader(asset);
 
         // The first line should contain "KEY", followed by languages.
         List<string> temp = reader.ReadCSV();
 
         // There must be at least two columns in a valid CSV file
         if (temp.Count < 2) return false;
 
         // Ensure that the first value is what we expect
         if (!string.Equals(temp[0], "KEY"))
         {
             Debug.LogWarning("Invalid localization CSV file. The first value is expected to be 'KEY', followed by language columns.\n" +
                 "Instead found '" + temp[0] + "'", asset);
             return false;
         }
         if (AvailableLanguages == null)
         {
             AvailableLanguages = new string[temp.Count - 1];
             for (int i = 1; i < temp.Count; ++i)
                 AvailableLanguages[i - 1] = temp[i];
         }
         else
         {
             if (AvailableLanguages.Length != temp.Count - 1)
             {
                 Debug.LogError(
                     string.Format("Localization file {0} does not have the correct amount of languages, expected {1} but got {2}",
                         asset.name, AvailableLanguages.Length, temp.Count - 1));
                 return false;
             }
         }
         // Read the entire CSV file into memory
         temp = reader.ReadCSV(); 
         while (temp != null)
         {
             string[] values = new string[temp.Count - 1];
             for (int i = 0; i < values.Length; ++i) values[i] = temp[i + 1];
             try
             {
                 dictionary.Add(temp[0], values);
             }
             catch (System.Exception)
             {
                 Debug.LogError(string.Format("Unable to add '{0}' to the Localization dictionary. Found the double in {1}", temp[0], asset.name));
             }
             temp = reader.ReadCSV();
         }
         return true;
     }
     /// <summary>
     /// Select the specified language from the previously loaded CSV file.
     /// </summary>
 
     public static bool SelectLanguage(string alanguage)
     {
         currentLanguageIndex = -1;
         for(int i = 0; i < AvailableLanguages.Length; ++i)
             if (AvailableLanguages[i] == alanguage)
             {
                 currentLanguageIndex = i;
                 break;
             }
         if (Application.isPlaying)
         {
             if(LocalizationChanged != null)
                 LocalizationChanged();
         }
         return currentLanguageIndex >= 0;
     }
     public static string Get(string key)
     {
         string[] vals;
         if (currentLanguageIndex != -1 && dictionary.TryGetValue(key, out vals))
         {
             if (currentLanguageIndex < vals.Length)
                 return vals[currentLanguageIndex];
         }
 #if UNITY_EDITOR
         Debug.LogWarning("Localization key not found: '" + key + "'");
 #endif
         return key;
     }
     public static string[] GetAll(string key)
     {
         string[] vals;
         if (dictionary.TryGetValue(key, out vals))
             return vals;
 
 #if UNITY_EDITOR
         Debug.LogWarning("Localization key not found: '" + key + "'");
 #endif
         return null;
     }
     public static bool Exists(string key)
     {
         return dictionary.ContainsKey(key);
     }
 }
 public delegate void Localize();
 
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image HarshadK · Dec 01, 2017 at 01:59 PM 0
Share

Can you post your current script for people to take a look at it? It will allow us to provide you with a proper solution rather than perfor$$anonymous$$g guess work as to how your script might be doing something and how to achieve what you are looking for.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

87 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

TMP shows default system font instead of drawing box. 0 Answers

How to read text file from resources and store into 2d array 3 Answers

displaying text typed in 1 Answer

Read variable from txt to Vector3 1 Answer

How to draw text based on text above it? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges