Example #1
0
        private IEnumerator ParseSingleLocale(string file)
        {
            // данные должны храниться в Assets\StreamingAssets

            string filePath = "";

#if UNITY_STANDALONE_WIN
//#if UNITY_EDITOR
            filePath = "file://" + Path.Combine(Application.streamingAssetsPath, file + ".csv");
#elif UNITY_STANDALONE_OSX
            filePath = "file://" + Application.streamingAssetsPath + "/" + file + ".csv";
#elif UNITY_ANDROID
            // на андройде не работал Application.streamingAssetsPath
            filePath = Application.streamingAssetsPath + "/" + file + ".csv";
#elif UNITY_IOS
            filePath = "file:" + Application.dataPath + "/Raw/" + file + ".csv";
#elif UNITY_WEBGL
            filePath = Application.streamingAssetsPath + "/" + file + ".csv";
#else
            filePath = "file:/" + Path.Combine(Application.streamingAssetsPath, file + ".csv");
#endif
            //Debug.Log("Localization file: " + filePath);

            // на сколько понял из документации в андройде данные находятся в сжатом виде, и лучшим способом будет считывать данные через WWW.
            WWW www = new WWW(filePath);

            yield return(www);

            if (String.IsNullOrEmpty(www.error))
            {
                CsvData data = CsvData.Parse(www.text);
                if (data == null)
                {
                    Debug.LogError("Ошибка!");
                }
                else
                {
                    if (!data.Headers.Contains("Keys"))
                    {
                        Debug.LogError("Отсутвует системный заголовок Keys");
                        //continue;
                        yield return(null);
                    }
                    if (!data.Headers.Contains(CurrentLocale))
                    {
                        Debug.LogError("Отсутствует необходимая локализация: " + CurrentLocale);
                        //continue;
                        yield return(null);
                    }
                    else
                    {
                        // заполняем основной словарь данными выбранной локализации
                        List <string> keys = data.GetColumn(data.Headers.IndexOf("Keys"));
                        List <string> loc  = data.GetColumn(data.Headers.IndexOf(CurrentLocale));

                        for (int i = 0; i < keys.Count; i++)
                        {
                            if (_localizedText.ContainsKey(keys[i]))
                            {
                                //Debug.LogWarning(
                                //    System.String.Format(
                                //        "Ключ {0} уже встречается в локализации, будет произведена замена!", keys[i]));

                                continue;
                            }
                            try
                            {
                                if (!string.IsNullOrEmpty(keys[i]) && (!System.String.IsNullOrEmpty(loc[i])))
                                {
                                    _localizedText.Add(keys[i], loc[i]);
                                }
                            }
                            catch (Exception e)
                            {
                                Debug.LogError("Localization add " + file + " in key dictionary " + keys[i] +
                                               " exception: " + e.Message);
                            }
                        }
                    }
                }
                //Debug.Log("Data loaded, dictionary contains: " + _localizedText.Count + " entries");
            }
            else
            {
                Debug.LogError("Cannot found localization file " + filePath + ": " + www.error);
            }

            www.Dispose();
        }