public static string GetBaseValue(string key) { if (!mLocalize) { mLocalize = LoadLocalizeObjectFromPath(); if (mLocalize) { mEditItemBase = new Item { header = new ItemHeader { text = mLocalizeTable[0].file, language = 0, isPlatform = false } }; LoadItem(mEditItemBase); } else //no localize setup { return(""); } } //grab value Data dat = null; return(mEditItemBase != null && mEditItemBase.items.TryGetValue(key, out dat) ? dat.value : ""); }
public static LocalizeFromTextAssetConfig Open(LocalizeFromTextAsset localize) { SetLocalize(localize, false); LocalizeFromTextAssetConfig win = EditorWindow.GetWindow(typeof(LocalizeFromTextAssetConfig)) as LocalizeFromTextAssetConfig; if (mLocalize) { //check if base path is already set if (mLocalizeTable != null && mLocalizeTable.Length > 0 && mLocalizeTable[0].file) { win.mMode = Mode.EditItems; } else { win.mMode = Mode.EditLanguages; } win.LoadAllItems(); GenerateBaseKeyItems(); win.InitCurrentMode(); } else { //ask to select component win.mMode = Mode.SelectComponent; } return(win); }
private static void SetLocalize(LocalizeFromTextAsset localize, bool forceSet) { if (localize) { if (mLocalize != localize && SaveLocalizeObjectPath(localize)) { mLocalize = LoadLocalizeObjectFromPath(); } } else { if (forceSet) { mLocalize = null; } else if (!mLocalize) //see if we can load from previous { mLocalize = LoadLocalizeObjectFromPath(); } } if (mLocalize) { var _lt = typeof(LocalizeFromTextAsset).GetField("tables", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); mLocalizeTable = _lt.GetValue(mLocalize) as LocalizeFromTextAsset.TableData[]; if (mLocalizeTable == null) { mLocalizeTable = new LocalizeFromTextAsset.TableData[1]; mLocalizeTable[0] = new LocalizeFromTextAsset.TableData(); _lt.SetValue(mLocalize, mLocalizeTable); EditorUtility.SetDirty(mLocalize); } } else { mLocalizeTable = null; } }
void LoadItem(TextAsset text, int lang, bool isPlatform, RuntimePlatform platform) { Item item = null; if (lang == 0 && !isPlatform) { if (mEditItemBase == null) { mEditItemBase = new Item { header = new ItemHeader { text = text, language = lang, isPlatform = isPlatform } } } ; item = mEditItemBase; } else { //grab existing item. if not, create it bool itemFound = false; for (int i = 0; i < mEditItems.Count; i++) { if (mEditItems[i].header.IsEqualTo(lang, isPlatform, platform)) { item = mEditItems[i]; itemFound = true; } } if (!itemFound) { item = new Item { header = new ItemHeader { text = text, language = lang, isPlatform = isPlatform, platform = platform } }; mEditItems.Add(item); } } LoadItem(item); } void SaveItem(Item item) { if (item.header.text) { //generate entries List <LocalizeFromTextAsset.Entry> tableEntries = new List <LocalizeFromTextAsset.Entry>(); foreach (var pair in item.items) { //if text is empty, do not save if we are not base or platform specific if ((item.header.language == 0 && !item.header.isPlatform) || !string.IsNullOrEmpty(pair.Value.value)) { LocalizeFromTextAsset.Entry entry = new LocalizeFromTextAsset.Entry { key = pair.Key, text = pair.Value.value, param = item != mEditItemBase && pair.Value.paramIsRef ? null : pair.Value.param }; tableEntries.Add(entry); } } //save string output = LocalizeFromTextAsset.EntryList.ToJSON(tableEntries, true); string path = AssetDatabase.GetAssetPath(item.header.text); File.WriteAllText(path, output); } item.changed = false; } bool RemoveInvalidItems() { bool ret = false; for (int i = 0; i < mEditItems.Count; i++) { if (mEditItems[i].header.language == 0) { if (mEditItems[i].header.isPlatform) { bool found = false; for (int p = 0; p < mLocalizeTable[0].platforms.Length; p++) { if (mLocalizeTable[0].platforms[p].platform == mEditItems[i].header.platform) { found = true; break; } } if (!found) { ret = true; mEditItems.RemoveAt(i); i--; } } } else { bool found = false; for (int l = 0; l < mLocalizeTable.Length; l++) { if (mEditItems[i].header.language == l) { if (mEditItems[i].header.isPlatform) { for (int p = 0; p < mLocalizeTable[l].platforms.Length; p++) { if (mLocalizeTable[l].platforms[p].platform == mEditItems[i].header.platform) { found = true; break; } } } else { found = true; } break; } } if (!found) { ret = true; mEditItems.RemoveAt(i); i--; } } } return(ret); } void GenerateLocalizedUIItems() { int c = mEditItems.Count; mEditItemTexts = new string[c]; mEditItemInds = new int[c]; for (int i = 0; i < c; i++) { mEditItemTexts[i] = mEditItems[i].header.isPlatform ? string.Format("{0} ({1})", mLocalizeTable[mEditItems[i].header.language].language, mEditItems[i].header.platform.ToString()) : mLocalizeTable[mEditItems[i].header.language].language; mEditItemInds[i] = i; } mEditItemInd = 0; } bool LoadItems() { bool ret = false; if (mEditItemLoads.Count > 0) { //load foreach (ItemHeader header in mEditItemLoads) { //load actual data from file if (header.text) { LoadItem(header.text, header.language, header.isPlatform, header.platform); } else //remove { for (int i = 0; i < mEditItems.Count; i++) { if (mEditItems[i].header.IsEqualTo(header)) { mEditItems.RemoveAt(i); break; } } } } mEditItemLoads.Clear(); ret = true; } return(ret); } void LoadAllItems() { mEditItemBase = null; mEditItemBaseKeyTexts = null; mEditItemBaseKeyInds = null; mEditItems.Clear(); if (mLocalize) { if (mLocalizeTable != null) { for (int i = 0; i < mLocalizeTable.Length; i++) { if (mLocalizeTable[i].file) { LoadItem(mLocalizeTable[i].file, i, false, RuntimePlatform.WindowsEditor); } if (mLocalizeTable[i].platforms != null) { for (int p = 0; p < mLocalizeTable[i].platforms.Length; p++) { if (mLocalizeTable[i].platforms[p].file) { LoadItem(mLocalizeTable[i].platforms[p].file, i, true, mLocalizeTable[i].platforms[p].platform); } } } } } } } void AddNewBaseKey() { string newKey = mEditItemBase.GenerateNewItem(ref mEditItemsCurKeyGen); int newCount = mEditItemBaseKeyTexts.Length + 1; System.Array.Resize(ref mEditItemBaseKeyTexts, newCount); System.Array.Resize(ref mEditItemBaseKeyInds, newCount); mEditItemBaseKeyTexts[newCount - 1] = newKey; mEditItemBaseKeyInds[newCount - 1] = newCount - 1; System.Array.Sort(mEditItemBaseKeyTexts); mEditItemBaseKeyInd = System.Array.IndexOf(mEditItemBaseKeyTexts, newKey); } void DrawItemData(string controlName, Item item, Data dat, ref string paramLastText) { //content GUILayout.Label("Value"); GUI.SetNextControlName(controlName); if (dat != null) { string editText = EditorGUILayout.TextArea(dat.value, GUILayout.MinHeight(valueTextAreaMinHeight), GUILayout.MaxHeight(valueTextAreaMaxHeight)); if (editText != dat.value) { dat.value = editText; item.changed = true; } //params GUILayout.BeginVertical(GUI.skin.box); item.paramFoldout = EditorGUILayout.Foldout(item.paramFoldout, "Params"); if (item.paramFoldout) { bool isRef; if (item != mEditItemBase) { isRef = GUILayout.Toggle(dat.paramIsRef, "Use Base"); if (dat.paramIsRef != isRef) { dat.paramIsRef = isRef; item.changed = true; } } else { isRef = false; } if (!isRef) { if (EditorExt.Utility.DrawStringArrayAlt(ref dat.param, ref paramLastText)) { item.changed = true; } } } GUILayout.EndVertical(); } else { EditorGUILayout.TextArea("", GUILayout.MinHeight(valueTextAreaMinHeight), GUILayout.MaxHeight(valueTextAreaMaxHeight)); GUILayout.BeginVertical(GUI.skin.box); EditorGUILayout.Foldout(false, "Params"); GUILayout.EndVertical(); } } void AddNewItem(string key, Data copyFrom) { mEditItemBase.AddNewItem(key, copyFrom); int newCount = mEditItemBaseKeyTexts.Length + 1; System.Array.Resize(ref mEditItemBaseKeyTexts, newCount); System.Array.Resize(ref mEditItemBaseKeyInds, newCount); mEditItemBaseKeyTexts[newCount - 1] = key; mEditItemBaseKeyInds[newCount - 1] = newCount - 1; System.Array.Sort(mEditItemBaseKeyTexts); mEditItemBaseKeyInd = System.Array.IndexOf(mEditItemBaseKeyTexts, key); } void DoEditItems() { if (mLocalizeTable == null || mLocalizeTable.Length == 0 || mLocalizeTable[0].file == null || mEditItemBase == null) { GUILayout.Label("Base File is not set!"); return; } mEditItemsScroll = GUILayout.BeginScrollView(mEditItemsScroll); string baseCurKey = null; //display base if (mEditItemBaseKeyTexts.Length > 0) { baseCurKey = mEditItemBaseKeyTexts[mEditItemBaseKeyInd]; Data baseCurDat = mEditItemBase.items[baseCurKey]; //key GUILayout.Label("Key"); if (mEditItemsKeyEdit) { GUILayout.BeginHorizontal(); GUI.SetNextControlName(editKeyControl); mEditItemsKeyText = EditorGUILayout.TextField(mEditItemsKeyText); GUI.enabled = !string.IsNullOrEmpty(mEditItemsKeyText) && !mEditItemBase.items.ContainsKey(mEditItemsKeyText); //change key if (EditorExt.Utility.DrawSimpleButton("√", "Accept")) { if (mEditItemsKeyEditIsNew) { AddNewItem(mEditItemsKeyText, null); } else { //remove old key mEditItemBase.items.Remove(baseCurKey); //regenerate keys mEditItemBaseKeyTexts[mEditItemBaseKeyInd] = mEditItemsKeyText; System.Array.Sort(mEditItemBaseKeyTexts); mEditItemBaseKeyInd = System.Array.IndexOf(mEditItemBaseKeyTexts, mEditItemsKeyText); //re-add to items mEditItemBase.items.Add(mEditItemsKeyText, baseCurDat); mEditItemBase.changed = true; } mEditItemsKeyEdit = false; mEditItemsKeyEditIsNew = false; EditorGUI.FocusTextInControl(editItemControl1); } GUI.enabled = true; //cancel if (EditorExt.Utility.DrawSimpleButton("X", "Cancel")) { mEditItemsKeyEdit = false; EditorGUI.FocusTextInControl(editItemControl1); } GUILayout.EndHorizontal(); } else { GUILayout.BeginHorizontal(); int ind = EditorGUILayout.IntPopup(mEditItemBaseKeyInd, mEditItemBaseKeyTexts, mEditItemBaseKeyInds); if (mEditItemBaseKeyInd != ind) { mEditItemBaseKeyInd = Mathf.Clamp(ind, 0, mEditItemBaseKeyTexts.Length - 1); mEditItemParamText = ""; } //edit key if (EditorExt.Utility.DrawSimpleButton("E", "Edit key text")) { mEditItemsKeyText = mEditItemBaseKeyTexts[mEditItemBaseKeyInd]; mEditItemsKeyEdit = true; EditorGUI.FocusTextInControl(editKeyControl); } //duplicate if (EditorExt.Utility.DrawSimpleButton("D", "Duplicate")) { //generate unique key string key = mEditItemBaseKeyTexts[mEditItemBaseKeyInd]; string newKey; int dupInd = 1; do { newKey = string.Format("{0}({1})", key, dupInd++); } while(mEditItemBase.items.ContainsKey(newKey)); AddNewItem(newKey, mEditItemBase.items[key]); } //add if (EditorExt.Utility.DrawAddButton()) { mEditItemsKeyText = ""; mEditItemsKeyEdit = true; mEditItemsKeyEditIsNew = true; EditorGUI.FocusTextInControl(editKeyControl); } //remove if (EditorExt.Utility.DrawRemoveButton()) { M8.ArrayUtil.RemoveAt(ref mEditItemBaseKeyTexts, mEditItemBaseKeyInd); M8.ArrayUtil.RemoveAt(ref mEditItemBaseKeyInds, mEditItemBaseKeyInd); for (int i = mEditItemBaseKeyInd; i < mEditItemBaseKeyInds.Length; i++) { mEditItemBaseKeyInds[i] = i; } if (mEditItemBaseKeyInd > 0 && mEditItemBaseKeyInd >= mEditItemBaseKeyInds.Length) { mEditItemBaseKeyInd--; } mEditItemBase.items.Remove(baseCurKey); mEditItemBase.changed = true; } GUILayout.EndHorizontal(); } GUI.enabled = !mEditItemsKeyEdit; DrawItemData(editItemControl1, mEditItemBase, baseCurDat, ref mEditItemParamText); GUI.enabled = true; } else if (GUILayout.Button("Add new item")) { AddNewBaseKey(); mEditItemsKeyText = ""; mEditItemsKeyEdit = true; EditorGUI.FocusTextInControl(editKeyControl); } //Localize mEditItemsLocalizeFoldout = EditorGUILayout.Foldout(mEditItemsLocalizeFoldout, "Localize"); if (mEditItemsLocalizeFoldout && mEditItems.Count > 0) { GUI.enabled = !(mEditItemsKeyEdit || string.IsNullOrEmpty(baseCurKey)); if (mEditItemInd >= mEditItemTexts.Length) { mEditItemInd = 0; } int ind = EditorGUILayout.IntPopup(mEditItemInd, mEditItemTexts, mEditItemInds); if (mEditItemInd != ind) { mEditItemInd = Mathf.Clamp(ind, 0, mEditItemTexts.Length - 1); mEditItemLocalizeParamText = ""; } Item localizeItem = mEditItems[mEditItemInd]; Data localizeDat = null; if (GUI.enabled) { if (!localizeItem.items.TryGetValue(baseCurKey, out localizeDat)) { localizeDat = new Data { value = "", param = null, paramIsRef = true }; localizeItem.items.Add(baseCurKey, localizeDat); } } DrawItemData(editItemControl2, localizeItem, localizeDat, ref mEditItemLocalizeParamText); GUI.enabled = true; } GUILayout.EndScrollView(); EditorExt.Utility.DrawSeparator(); //save, revert bool hasChanges; if (mEditItemsKeyEdit) { hasChanges = false; } else { hasChanges = mEditItemBase.changed; if (!hasChanges) { foreach (Item item in mEditItems) { hasChanges = item.changed; if (hasChanges) { break; } } } } GUI.enabled = hasChanges; GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); GUI.backgroundColor = Color.green; if (GUILayout.Button("Save", GUILayout.Width(100f))) { if (mEditItemBase.changed) { SaveItem(mEditItemBase); } foreach (Item item in mEditItems) { if (item.changed) { SaveItem(item); } } AssetDatabase.Refresh(); } GUILayout.Space(8f); GUI.backgroundColor = Color.red; if (GUILayout.Button("Revert", GUILayout.Width(100f))) { if (mEditItemBase.changed) { LoadItem(mEditItemBase); } foreach (Item item in mEditItems) { if (item.changed) { LoadItem(item); } } GUIUtility.keyboardControl = 0; GUIUtility.hotControl = 0; } GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); GUI.enabled = true; } void DoSelectComponent() { GUILayout.Label("Grab from a core prefab."); LocalizeFromTextAsset localize = EditorGUILayout.ObjectField(mLocalize, typeof(LocalizeFromTextAsset), false) as LocalizeFromTextAsset; if (mLocalize != localize) { SetLocalize(localize, true); LoadAllItems(); GenerateBaseKeyItems(); Repaint(); } } }
public static string DrawSelector(Rect position, string key) { //load localize if not yet set if (!mLocalize) { mLocalize = LoadLocalizeObjectFromPath(); if (mLocalize) { if (mLocalizeTable == null) //need to load base { SetLocalize(mLocalize, false); } } else //no localize setup { if (GUI.Button(position, "Configure Localization")) { Open(null); } return(""); } } //generate base data if (mEditItemBaseKeyTexts == null) { mEditItemBase = new Item { header = new ItemHeader { text = mLocalizeTable[0].file, language = 0, isPlatform = false } }; LoadItem(mEditItemBase); } //get current index int selectInd = -1; if (mEditItemBaseKeyTexts != null) { for (int i = 0; i < mEditItemBaseKeyTexts.Length; i++) { if (mEditItemBaseKeyTexts[i] == key) { selectInd = i; break; } } const float editSize = 20f; const float editSpace = 4f; var popUpPos = new Rect(position.x, position.y, position.width - editSize - editSpace, position.height); var editPos = new Rect(position.x + position.width - editSize, position.y, editSize, position.height); var editItemBaseKeyTexts = new List <string>(mEditItemBaseKeyTexts.Length + 1); editItemBaseKeyTexts.Add("-None-"); editItemBaseKeyTexts.AddRange(mEditItemBaseKeyTexts); selectInd = EditorGUI.IntPopup(popUpPos, selectInd + 1, editItemBaseKeyTexts.ToArray(), mEditItemBaseKeyInds) - 1; if (GUI.Button(editPos, new GUIContent("E", "Configure localization."), EditorStyles.toolbarButton)) { Open(null); mEditItemBaseKeyInd = selectInd >= 0 ? selectInd : 0; } } else { if (GUI.Button(position, "Configure Localization")) { Open(null); } return(""); } return(selectInd == -1 ? "" : mEditItemBaseKeyTexts[selectInd]); }
public static string DrawSelector(string key) { //load localize if not yet set if (!mLocalize) { mLocalize = LoadLocalizeObjectFromPath(); if (mLocalize) { if (mLocalizeTable == null) //need to load base { SetLocalize(mLocalize, false); } } else //no localize setup { if (GUILayout.Button("Configure Localization")) { Open(null); } Color prevClr = GUI.color; GUI.color = Color.red; GUILayout.Label("Localization is not found.", GUI.skin.box); GUI.color = prevClr; return(""); } } //generate base data if (mEditItemBaseKeyTexts == null) { mEditItemBase = new Item { header = new ItemHeader { text = mLocalizeTable[0].file, language = 0, isPlatform = false } }; LoadItem(mEditItemBase); } //get current index int selectInd = -1; if (mEditItemBaseKeyTexts != null) { for (int i = 0; i < mEditItemBaseKeyTexts.Length; i++) { if (mEditItemBaseKeyTexts[i] == key) { selectInd = i; break; } } //selection GUILayout.BeginHorizontal(); var editItemBaseKeyTexts = new List <string>(mEditItemBaseKeyTexts.Length + 1); editItemBaseKeyTexts.Add("-None-"); editItemBaseKeyTexts.AddRange(mEditItemBaseKeyTexts); selectInd = EditorGUILayout.IntPopup(selectInd + 1, editItemBaseKeyTexts.ToArray(), mEditItemBaseKeyInds) - 1; if (EditorExt.Utility.DrawSimpleButton("E", "Configure localization.")) { Open(null); mEditItemBaseKeyInd = selectInd >= 0 ? selectInd : 0; } GUILayout.EndHorizontal(); } else { if (GUILayout.Button("Configure Localization")) { Open(null); } return(""); } return(selectInd == -1 ? "" : mEditItemBaseKeyTexts[selectInd]); }