// Use this for initialization void Awake() { RichTextArrayList = new ArrayList(); RichTextStruct text = new RichTextStruct("SSS", "10", "#FF0000FF"); RichTextArrayList.Add(text); text = new RichTextStruct("SSSS", "10", "#FF0000FF"); RichTextArrayList.Add(text); }
/// <summary> /// 创建超链接 /// </summary> /// <param name="data"></param> private void CreateEventLabel(RichTextStruct data, bool bTypewriter, string lastStr = "") { /* * GameObject go = CreateLabel(); * Text ul = go.GetComponent<Text>(); * * ul.width = (int)(LineWidth_ - PositionX_); * * string tempStr = data.info; * if( !string.IsNullOrEmpty( lastStr ) ) * tempStr = lastStr; * * ul.text = tempStr; * string sbstr = ul.processedText; * ul.text = "[u][url=" + data.hyperlinksType + "/" + data.data + "]" + sbstr + "[/url][/u]"; * * // 处理碰撞的区域和大小 * BoxCollider box = go.GetComponent<BoxCollider>(); * if( box == null ) * box = go.AddComponent<BoxCollider>(); * box.center = new Vector3( ul.printedSize.x / 2, -ul.printedSize.y / 2 ); * box.size = new Vector3( ul.printedSize.x, ul.printedSize.y ); * go.AddComponent<RichTextClickEvent>(); * * AddGameObjectToLine( false, PositionX_, go, RichTextInfoType.Select ); * PositionX_ += ul.printedSize.x + LabelSpacingX_; * * if( bTypewriter ) * go.SetActive( false ); * * tempStr = tempStr.Remove( 0, sbstr.Length ); * if( tempStr.Length >= 1 ) { * AddGameObjectToLine( true, 0, null ); * PositionX_ = 0; * CreateEventLabel( data, bTypewriter, tempStr ); * } */ }
/// <summary> /// 解析 /// </summary> /// <param name="str"></param> private void Analyze(string str) { // 把换行替换 //str = str.Replace( "\\n", "*#%%%#*" ); str = str.Replace("\n", "*#%%%#*"); // 遍历字符串 while (str.Length > 0) { // 如果有换行或富文本 StartIndex_ = str.IndexOf("*#"); if (StartIndex_ >= 0) { // 如果不是第0个开始,说明前面有纯文本 if (StartIndex_ > 0) { RichTextStruct temp = new RichTextStruct(); temp.bNewLine = false; string vText = str.Substring(0, StartIndex_); temp.info = vText; temp.type = RichTextInfoType.Text; RichTextlist_.Add(temp); str = str.Remove(0, StartIndex_); } // 获得结尾字符 EndIndex_ = str.IndexOf("#*"); // 有的配对的结尾符 if (EndIndex_ > 0) { // 得到内容 Command_ = str.Substring(2, EndIndex_ - 2); // 移除结尾符 str = str.Remove(0, EndIndex_ + 2); } // 没有配对的 else { // 直接入表,是纯文本 RichTextStruct temp = new RichTextStruct(); temp.bNewLine = false; temp.info = str; temp.type = RichTextInfoType.Text; RichTextlist_.Add(temp); str = str.Remove(0, str.Length); } // 有内容,得到是否有换行 if (!string.IsNullOrEmpty(Command_)) { EqualIndex_ = Command_.IndexOf("%%%"); } else { // 没有内容,继续循环 continue; } // 如果有换行的 if (EqualIndex_ >= 0) { RichTextStruct temp = new RichTextStruct(); temp.bNewLine = true; RichTextlist_.Add(temp); } else { // 其他富文本 if (Command_.IndexOf("=") > 0) { CutString(Command_, "=", out Command_first_, out Command_last_); } RichTextStruct temp = new RichTextStruct(); temp.bNewLine = false; switch (Command_first_) { case "img": temp.info = Command_last_; temp.type = RichTextInfoType.Sprite; break; case "select": string vText = ""; string vEventFunc = ""; string vValue = ""; if (Command_last_.IndexOf(",") > -1) { // 取得点击参数 CutString(Command_last_, ",", out vText, out vEventFunc); } if (vEventFunc.IndexOf(",") > -1) { CutString(vEventFunc, ",", out vEventFunc, out vValue); } if (vText.Length <= 0) { temp.info = Command_last_; temp.type = RichTextInfoType.Text; } else { temp.info = vText; temp.type = RichTextInfoType.Select; temp.hyperlinksType = vEventFunc; temp.data = vValue; } break; default: temp.info = Command_; temp.type = RichTextInfoType.Text; break; } RichTextlist_.Add(temp); } } else { RichTextStruct temp = new RichTextStruct(); temp.bNewLine = false; temp.info = str; temp.type = RichTextInfoType.Text; RichTextlist_.Add(temp); str = str.Remove(0, str.Length); } } }