Like most japanese styled ADV(galgame), i want implement text display character by character.
I just finished first step, but i don’t know rich text has depth.
Here is the code i’m just finished.
IEnumerator NextLine() {
// On reading.
isReading = true;
// Empty string.
linesPanel.text = "";
// Get line & Each character
string line = lines [lineOffset];
List<FormatText> formatList = ParseFormat (line);
formatList.Sort(delegate(FormatText a, FormatText b) {
return a.startAt.CompareTo(b.startAt);
});
line = Regex.Replace (line, "(<.*?>)", "");
if (skipRead) {
linesPanel.text = line;
} else {
string text = "";
for (int readOffset = 0; readOffset < line.Length; readOffset++) {
text += line [readOffset];
if (jumpToEnd) {
linesPanel.text = line;
break;
}
string temp = text;
int offset = 0;
foreach ( FormatText format in formatList ) {
if (format.startAt < readOffset) {
temp = temp.Insert ( (format.startAt + offset), format.formatStart );
offset += format.formatStart.Length;
if (format.endAt >= readOffset) {
temp += format.formatEnd;
}
}
if (format.endAt < readOffset) {
temp = temp.Insert ( (format.endAt + offset), format.formatEnd );
offset += format.formatEnd.Length;
}
print (temp.Replace("<", "[").Replace(">","]"));
}
linesPanel.text = temp;
yield return new WaitForSeconds (.05f);
}
}
// Reset state.
jumpToEnd = false;
isReading = false;
}
class FormatText {
public int startAt;
public int endAt;
public string formatStart;
public string formatEnd;
public bool isWrapped () {
return
(startAt != null) &&
(endAt != null) &&
(formatStart != null) &&
(formatEnd != null );
}
}
List<FormatText> ParseFormat( string sourceText ) {
string tagExp = @"(<.*?>)";
string[] splited = Regex.Split (sourceText, tagExp);
List<FormatText> formatList = new List<FormatText> ();
List<FormatText> unwrappedList = new List<FormatText> ();
int textCounter = 0;
// Check if text styled
if ( splited.Length > 1 ) {
foreach ( string text in splited ) {
if ( Regex.Match ( text, tagExp ).Success ) {
if ( text.StartsWith("</") ) {
// Check rich text tag end
unwrappedList[unwrappedList.Count - 1].formatEnd = text;
unwrappedList[unwrappedList.Count - 1].endAt = textCounter;
} else {
// Check rich text tag start
unwrappedList.Add( new FormatText () );
unwrappedList[unwrappedList.Count - 1].formatStart = text;
unwrappedList[unwrappedList.Count - 1].startAt = textCounter;
}
} else {
textCounter += (text.Length);
}
if (unwrappedList.Count > 0) {
if (unwrappedList[unwrappedList.Count - 1].isWrapped()) {
FormatText formatText = unwrappedList[unwrappedList.Count - 1];
formatList.Add(formatText);
unwrappedList.RemoveAt(unwrappedList.Count - 1);
}
}
}
}
return formatList;
}
When i want to parse its like this , it will be wrong.
So how to implement it?