You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
112 lines
3.4 KiB
112 lines
3.4 KiB
using UnityEngine; |
|
|
|
using System; |
|
using System.Collections.Generic; |
|
|
|
public class GameTextManager |
|
{ |
|
//------------------------------------------------------------------ |
|
private Dictionary<int, string> m_gameTexts = new Dictionary<int, string>(); |
|
|
|
//------------------------------------------------------------------ |
|
public GameTextManager() |
|
{ |
|
ParseGameText(); |
|
} |
|
|
|
//------------------------------------------------------------------ |
|
public string GetGameText( int textId ) |
|
{ |
|
if (m_gameTexts.ContainsKey(textId)) |
|
return m_gameTexts[textId]; |
|
|
|
CommonFunctions.myassert(false, "Text not found! TextId " + textId.ToString()); |
|
return "no text"; |
|
} |
|
//------------------------------------------------------------------ |
|
private void ReplaceWord( string from, string to ) |
|
{ |
|
Dictionary<int, string> replacedTexts = new Dictionary<int, string>(); |
|
|
|
foreach ( KeyValuePair<int, string> textValue in m_gameTexts ) |
|
{ |
|
int fromIdx = textValue.Value.IndexOf(from); |
|
if (fromIdx>=0) |
|
{ |
|
string replacedString = textValue.Value.Replace(from, to); |
|
replacedTexts[textValue.Key] = replacedString; |
|
} |
|
} |
|
|
|
foreach (KeyValuePair<int, string> textValue in replacedTexts) |
|
{ |
|
m_gameTexts[textValue.Key] = textValue.Value; |
|
} |
|
} |
|
|
|
//------------------------------------------------------------------ |
|
private void ParseGameText() |
|
{ |
|
TextAsset textAsset = Resources.Load("data/game_texts" + GlobalsVar.gCurrentLangPostfix) as TextAsset; |
|
var lines = textAsset.text.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); |
|
|
|
if (lines.Length == 0) |
|
{ |
|
CommonFunctions.myassert(false, "File not found or can't read!"); |
|
return; |
|
} |
|
|
|
foreach( var line in lines ) |
|
{ |
|
if (line.Length == 0) |
|
continue; |
|
|
|
if (line.IndexOf("//") != -1 || line.IndexOf("--/") != -1) |
|
continue; |
|
|
|
string[] words = line.Split(' '); |
|
if (words.Length < 2) |
|
continue; |
|
else if (words[0] == string.Empty) |
|
continue; |
|
|
|
var textId = -1; |
|
|
|
var idx = line.IndexOf(' '); |
|
if (idx != -1) |
|
{ |
|
string strTextId = line.Substring(0, idx); |
|
bool succes = int.TryParse(strTextId, out textId); |
|
if ( !succes ) |
|
{ |
|
CommonFunctions.myassert(false); |
|
Debug.Log( "Wrong string in gametext" ); |
|
} |
|
|
|
var textStr = line.Substring(idx + 1, line.Length - idx - 1); |
|
string gameTxtStr = textStr; |
|
if (gameTxtStr != string.Empty) |
|
{ |
|
char lastSymbol = textStr[textStr.Length - 1]; |
|
if (lastSymbol == ' ') |
|
gameTxtStr = textStr.Substring(0, textStr.Length - 1); |
|
} |
|
|
|
if ( m_gameTexts.ContainsKey(textId) ) |
|
{ |
|
CommonFunctions.myassert(false); |
|
Debug.Log("Dublicate textId " + textId.ToString() ); |
|
} |
|
|
|
m_gameTexts[textId] = gameTxtStr; |
|
} |
|
else |
|
{ |
|
textId = Convert.ToInt32(line); |
|
m_gameTexts[textId] = " "; |
|
} |
|
} |
|
|
|
Debug.Log( "Game text contain " + (m_gameTexts.Count).ToString() + " strings "); |
|
} |
|
}
|
|
|