elsemTim

Singltone

Sep 25th, 2016
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.52 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Resources;
  3. using Assets.Scripts.Helpers;
  4. using UnityEngine;
  5. using SimpleJson;
  6.  
  7. namespace Assets.Scripts
  8. {
  9.     public sealed class LangSingleton
  10.     {
  11.         private readonly Dictionary<string, string> ourText = new Dictionary<string, string>();
  12.         private static readonly LangSingleton instance = new LangSingleton();
  13.        
  14.         public static LangSingleton Instance
  15.         {
  16.            
  17.             get { return instance; }
  18.         }
  19.  
  20.  
  21.         protected LangSingleton()
  22.         {
  23.             ChangeLang("eng");
  24.         }
  25.  
  26.         private void GetText(TextAsset asset)
  27.         {
  28.             if (asset == null)
  29.             {
  30.                 return;
  31.             }
  32.  
  33.             ourText.Clear();
  34.             var jsonText = JSON.Parse((asset.ToString()));
  35.             foreach (var i in jsonText)
  36.             {
  37.                 ourText.Add(
  38.                     jsonText[i]["key"],
  39.                     jsonText[i]["value"]);
  40.             }
  41.         }
  42.  
  43.         public string GetValue(string key)
  44.         {
  45.             return ourText.ContainsKey(key)?ourText(key):string.empty;
  46.         }
  47.  
  48.         public void ChangeLang(string lng)
  49.         {
  50.             TextAsset asset;
  51.             if (lng = "ru")
  52.             {
  53.                 asset = Resources.Load("ru") as TextAsset;
  54.             }
  55.             else
  56.             {
  57.                 asset = Resources.Load("eng") as TextAsset;
  58.             }
  59.             GetText(asset);
  60.         }
  61.  
  62.  
  63.  
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment