Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections.Generic;
- using System;
- [Serializable]
- public class MultilangObject
- {
- public MultilangElementJSON[] elements;
- }
- [Serializable]
- public class MultilangElementJSON
- {
- [SerializeField]
- public string marker;
- [SerializeField]
- public string text;
- }
- public class Multilang : MonoBehaviour {
- public TextAsset[] langPacks;
- public string objectTag = "MultiLang";
- public int langIndex = 0;
- public int langsCount = 0;
- private Dictionary<string, string> dictonary = new Dictionary<string, string>();
- private bool firstChange = false;
- void Awake () {
- langsCount = langPacks.Length;
- ChangeLang(langIndex);
- }
- public bool ChangeLang(int i)
- {
- if ((i!=langIndex || !firstChange) && i < langsCount){
- firstChange = true;
- string raw = langPacks[i].text;
- Debug.Log(raw);
- if (raw != "")
- {
- var _dict = ParseLang(raw);
- if (_dict != null)
- {
- dictonary = _dict;
- Debug.Log("[LANG] Lang changed frm:"+langIndex+" to:"+i);
- langIndex = i;
- return true;
- }
- }
- }
- return false;
- }
- public string GetTranslate(string marker)
- {
- string ret="";
- if (dictonary.Count == 0)
- Awake();
- if (!dictonary.ContainsKey(marker))
- Debug.LogAssertion(marker + " not found!!");
- else
- ret = dictonary[marker];
- return ret;
- }
- public Dictionary<string, string> ParseLang(string str) {
- Dictionary<string, string> ret = new Dictionary<string, string>();
- MultilangObject mo = JsonUtility.FromJson<MultilangObject>(str);
- if (mo == null)
- return null;
- foreach(MultilangElementJSON e in mo.elements)
- {
- Debug.Log(e.marker + ":" + e.text);
- dictonary.Add(e.marker, e.text);
- }
- return ret;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement