Advertisement
Guest User

Untitled

a guest
Dec 15th, 2015
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.11 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System;
  4.  
  5. [Serializable]
  6. public class MultilangObject
  7. {
  8.     public MultilangElementJSON[] elements;
  9. }
  10. [Serializable]
  11. public class MultilangElementJSON
  12. {
  13.     [SerializeField]
  14.     public string marker;
  15.     [SerializeField]
  16.     public string text;
  17. }
  18.  
  19. public class Multilang : MonoBehaviour {
  20.    
  21.  
  22.     public TextAsset[] langPacks;
  23.     public string objectTag = "MultiLang";
  24.     public int langIndex = 0;
  25.     public int langsCount = 0;
  26.     private Dictionary<string, string> dictonary = new Dictionary<string, string>();
  27.     private bool firstChange = false;
  28.  
  29.     void Awake () {
  30.         langsCount = langPacks.Length;
  31.         ChangeLang(langIndex);
  32.     }
  33.    
  34.     public bool ChangeLang(int i)
  35.     {
  36.         if ((i!=langIndex || !firstChange) && i < langsCount){
  37.             firstChange = true;
  38.             string raw = langPacks[i].text;
  39.             Debug.Log(raw);
  40.  
  41.             if (raw != "")
  42.             {
  43.                 var _dict = ParseLang(raw);
  44.                 if (_dict != null)
  45.                 {
  46.                     dictonary = _dict;
  47.                     Debug.Log("[LANG] Lang changed frm:"+langIndex+" to:"+i);
  48.  
  49.                     langIndex = i;
  50.                     return true;
  51.                 }
  52.             }
  53.         }
  54.         return false;
  55.     }
  56.  
  57.     public string GetTranslate(string marker)
  58.     {
  59.         string ret="";
  60.         if (dictonary.Count == 0)
  61.             Awake();
  62.         if (!dictonary.ContainsKey(marker))
  63.             Debug.LogAssertion(marker + " not found!!");
  64.         else
  65.             ret = dictonary[marker];
  66.         return ret;
  67.     }
  68.  
  69.     public Dictionary<string, string> ParseLang(string str) {
  70.         Dictionary<string, string> ret = new Dictionary<string, string>();
  71.         MultilangObject mo = JsonUtility.FromJson<MultilangObject>(str);
  72.         if (mo == null)
  73.             return null;
  74.         foreach(MultilangElementJSON e in mo.elements)
  75.         {
  76.             Debug.Log(e.marker + ":" + e.text);
  77.             dictonary.Add(e.marker, e.text);
  78.         }
  79.         return ret;
  80.     }
  81.  
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement