前兩天,有位大佬問我這個富文本它支不支持嵌套。 本來就想過開發嵌套功能和解析嵌套功能的富文本,既然這樣,索性就幹吧。 經過我這兩天的奮鬥,總算是把這解析和嵌套功能給搞出來了,並且兼容到了之前的富文本之上。 順便調試了一番,調試過程當然很不順利,練手作品哪有盡善盡美的,調試過程也順手修復了很多bug吧! 總結一下,問題出的最多的地方就在解析和容錯。 現在這個功能插件雖然好用不少,可依舊還是有很多的bug,有時間或者有哪位熱心的朋友一起改吧!
先說一下我解析這塊的思路吧!
解析代碼的文件組織結構:
// 經過分析,發現能夠嵌套的類型就只有label和URL類型
// 先檢查txt的開始類型是否是url 或者是 label類型
// 如果是就這一部分的數據壓進棧裏,並且剪切出剩下部分的數據,並且解析剩下數據
// 如果發現不是嵌套類型,就查看棧裏面的嵌套類型,並且把嵌套類型的屬性複製到這個元素上面。
// 關鍵的迭代代碼部分 在 DisintegrateUrlAndLabel 裏
// Class RichString
public void Disintegrate(string txt)
{
txt = txt.TrimStart(' ').TrimEnd(' ');
if (string.IsNullOrEmpty(txt)) return;
// 截取自己的那一串字符串
// 如果是基礎類型,就壓棧處理,在尾部需要出棧操作
m_string_type = RichStringTools.CheckType(txt);
m_text = RichStringTools.SpliteRichStringByType(m_string_type, txt);
var temp_txt = txt;
var new_txt = RichStringTools.RemoveRichStringByType(m_string_type, txt, m_text);
if (m_string_type == RichStringType.Lab || m_string_type == RichStringType.Url) DisintegrateUrlAndLabel(temp_txt, new_txt);
else DisntegarateOtherSign(new_txt);
}
// Class RichString
private void DisintegrateUrlAndLabel(string temp_txt, string new_txt)
{
var _my_txt = m_text;
m_text = RichStringTools.ChangeLabAndUrlEndSign(m_text, m_string_type);
// 接取自己的字符串,如果還剩下有字符串,則做分割
var sign = RichStringTools.match_type_dict[m_string_type];
// 完整的元素內部的數據
var _whole_str = RichStringTools.SpliteWholeRichStringByType(m_string_type, temp_txt);
var start_symbol = _my_txt.Replace(sign.EndSign, "");
var end_symbol = _whole_str.Replace(start_symbol, "");
var _c_txt = end_symbol.Remove(end_symbol.LastIndexOf(sign.EndSign), sign.EndSign.Length);
if (!string.IsNullOrEmpty(_c_txt)) // 這裏面是關鍵的迭代代碼
{
this.m_warp.RichStringStack.Push(this);
this.m_warp.SetText(_c_txt);
this.m_warp.RichStringStack.Pop();
// 元素剩餘的數據解析
new_txt = temp_txt.Remove(0, _whole_str.Length);
this.m_warp.SetText(new_txt);
}
}
此外,這個程序還使用了大量的字符串拼接,CPU的佔用很明顯。 下面就是解析數據和成果圖
<label font = 'SciFi/SciFi Font - Header'| fontsize = '20'|fontstyle = 'Normal'> First LabelFirstLabelsdfsdfadfasdfaewoooooooooooooooppppppppppppppsssssssssssssssscccccccccccccc222222222222 <url id = '333'| can_click = 'true'><sprite atlas_name = 'SciFi/SciFi Atlas'|sprite_name = 'NGUI'|width = '30'|height = '18'/><label fontsize = '66'>66BigNumber</label><spamin atlas_name = 'Wooden Atlas'|sprite_name = 'Button'|prefix_sprite_name = 'Button'|loop = 'true'/>sdfsdfsdf</url><texture texture_name = 'mm'|width = '25'|height = '18'/><label fontsize = '30'|color = 'A18545'>Second Label</label> sdfssdfdsssss </label><texture texture_name = 'mm'|width = '65'|height = '108'/>
<label font = 'SciFi/SciFi Font - Header'| fontsize = '20'|fontstyle = 'Normal'> First LabelFirstLabelsdfsdfadfasdfaewoooooooooooo
<label fontsize = '50'>oooppppppppppppppsssssssssssssssscccccccccccccc22222</label>
2222222
<url id = '331'| can_click = 'true'>
<sprite atlas_name = 'SciFi/SciFi Atlas'|sprite_name = 'NGUI'|width = '30'|height = '18'/>
<label fontsize = '66'>66BigNumber</label>
<spamin atlas_name = 'Wooden Atlas'|sprite_name = 'Button'|prefix_sprite_name = 'Button'|loop = 'true'/>
sdfsdfsdf</url>
<texture texture_name = 'mm'|width = '25'|height = '18'/>
<label fontsize = '30'|color = 'A18545'>Second Label</label>
sdfssdfdsssss </label>
<texture texture_name = 'mm'|width = '108'|height = '65'/>
<label font = 'SciFi/SciFi Font - Header'| fontsize = '20'|fontstyle = 'Normal'> First LabelFirstLabelsdfsdfadfasdfaewoooooooooooo
<label fontsize = '50'>oooppppppppppppppsssssssssssssssscccccccccccccc22222</label>
2222222
<url id = '332'| can_click = 'true'>
<sprite atlas_name = 'SciFi/SciFi Atlas'|sprite_name = 'NGUI'|width = '30'|height = '18'/>
<label fontsize = '66'>66BigNumber</label>
<spamin atlas_name = 'Wooden Atlas'|sprite_name = 'Button'|prefix_sprite_name = 'Button'|loop = 'true'/>
sdfsdfsdf</url>
<texture texture_name = 'mm'|width = '25'|height = '18'/>
<label fontsize = '30'|color = 'A18545'>Second Label</label>
sdfssdfdsssss </label>
<texture texture_name = 'mm'|width = '108'|height = '65'/>
<label font = 'SciFi/SciFi Font - Header'| fontsize = '20'|fontstyle = 'Normal'> First LabelFirstLabelsdfsdfadfasdfaewoooooooooooo
<label fontsize = '50'>oooppppppppppppppsssssssssssssssscccccccccccccc22222</label>
2222222
<url id = '333'| can_click = 'true'>
<sprite atlas_name = 'SciFi/SciFi Atlas'|sprite_name = 'NGUI'|width = '30'|height = '18'/>
<label fontsize = '66'>66BigNumber</label>
<spamin atlas_name = 'Wooden Atlas'|sprite_name = 'Button'|prefix_sprite_name = 'Button'|loop = 'true'/>
sdfsdfsdf</url>
<texture texture_name = 'mm'|width = '25'|height = '18'/>
<label fontsize = '30'|color = 'A18545'>Second Label</label>
sdfssdfdsssss </label>
<texture texture_name = 'mm'|width = '108'|height = '65'/>
ps:所有的代碼都已經上傳到了git。