NGUI文本之中穿插图片

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class RichTextProcessing : MonoBehaviour
{
    public enum RichTextInfoType 
    {
        Invalid = -1,
        Text,               // 纯文本
        Hero              // 图片
    }
    
    public class RichTextStruct 
    {
        public bool             bNewLine;
        public string           info;
        public RichTextInfoType type;
        public GameObject       gobj;
        public float            posX;
    }

    public class LineStruct
    {
        public int                      maxHeight;
        public List     RichList = new List();
    }

    public  UIAtlas[]               AtlastList;
    public  Font                    UIFont;
    public GameObject               prefab;

    private List    RichTextlist = new List();
    private List        RichLineList = new List();

    private int                     Depth;
    private int                     LabelSpacingX;
    private int                     LabelSpacingY;
    private int                     LineHeight;
    private float                   LineWidth;
    private float                   PositionX = 0;

    private void Awake() 
    {
        UIPanel content = gameObject.GetComponent();
        Depth = content.depth + 1;
        LineHeight = 14;
        LabelSpacingX = 0;
        LabelSpacingY = 3;
        LineWidth = content.width;

        UIFont = Resources.Load("UI/Atlas/Font/FZY3JW") as Font;
        prefab = Resources.Load("UI/UIData/UI_ItemSp") as GameObject;
    }

    // for test
//    void Start()
//    {
//        //string str = "测试测试
测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试"; // string str = @"活动一、首次登陆送豪礼
//活动时间:6.15~6.30
//活动内容:活动期间玩家首次登陆游戏即可获得x500x20000、x1000

//活动二、每日登陆领奖励
//活动时间:6.15~6.21
//活动内容:活动期间,每日登陆即可领取登陆奖励一份。
//6.15日:登陆可领取x500、x10、x100、x5
//6.16日:登陆可领取x500、x10、x100、x5
//6.17日:登陆可领取x500、x10、x100、x5
//6.18日:登陆可领取x500、x10、x100、x5
//6.19日:登陆可领取x500、x10、x100、x5
//6.20日:登陆可领取x500、x10、x100、x5
//6.21日:登陆可领取x500、x10、x100、x5
"; // StartCoroutine(ShowStr(str)); // } //文本 // [ffffff][6fccf8]活动一、首次登陆送好礼[-] // 活动时间:[049418]6.15~6.30[-] // 活动内容:活动期间,玩家首次登陆游戏即可获得×500、×20000、×1000 // [6fccf8]活动五、公会发展送奖励[-] // 活动时间:[049418]6.15-6.30[-] // 活动内容:玩家建立公会成为会长后,创建公会QQ群并邀请官方管理员加入,即可获得30个公会发展礼包激活码,激活后可获得公会发展礼包,每个玩家仅能激活1次; // 公会发展礼包:×100000、×10、×100 // 2015.6.14[-] public void SetContent(string content) { StartCoroutine(ShowStr(content)); } /// /// 解析字符串并显示排版 /// /// 传入的字符串 /// 是否显示打字机效果(一个一个字的显示) /// public IEnumerator ShowStr(string str) { // 如果空,返回 if(string.IsNullOrEmpty(str)) yield break; Reset(); Analyze(str); CreateAll(); Sort(); } private void Reset() { PositionX = 0; RichTextlist.Clear(); RichLineList.Clear(); AddGameObjectToLine(true, null); //ClearChild(gameObject); } /// /// 解析 /// /// private void Analyze(string str) { int StartIndex =0; int EndIndex = 0; int EqualIndex = 0; string Command = ""; // 把换行替换 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(1, EndIndex - 1); // 移除结尾符 str = str.Remove(0, EndIndex + 1); } // 没有配对的 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("br"); } else { // 没有内容,继续循环 continue; } // 如果有换行的 if(EqualIndex >= 0) { RichTextStruct temp = new RichTextStruct(); temp.bNewLine = true; RichTextlist.Add(temp); } else { string Command_first = ""; string Command_last = ""; // 其他富文本 if( Command.IndexOf("=") > 0 ) { CutString(Command, "=", out Command_first, out Command_last); } RichTextStruct temp = new RichTextStruct(); temp.bNewLine = false; switch(Command_first) { case "hero": temp.info = Command_last; temp.type = RichTextInfoType.Hero; 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); } } } private void CreateAll() { for(int i = 0; i < RichTextlist.Count; i++) { if(RichTextlist[i].bNewLine) { PositionX = 0; AddGameObjectToLine(true, null); } switch(RichTextlist[i].type) { case RichTextInfoType.Text: CreateTextLabel(RichTextlist[i]); break; case RichTextInfoType.Hero: CreateSprite(RichTextlist[i]); break; default: break; } } } /// /// 创建普通文本 /// /// private void CreateTextLabel(RichTextStruct rts) { string str = rts.info; if(string.IsNullOrEmpty(str)) return; GameObject go = NGUITools.AddChild(gameObject); go.name = "Label"; UILabel label = go.AddComponent(); label.trueTypeFont = UIFont; label.pivot = UIWidget.Pivot.TopLeft; label.depth = Depth; label.overflowMethod = UILabel.Overflow.ResizeHeight; label.fontSize = LineHeight; label.spacingX = LabelSpacingX; label.spacingY = LabelSpacingY; label.maxLineCount = 1; label.width = (int)(LineWidth - PositionX); label.text = str; string sbstr = label.processedText; label.text = sbstr; rts.gobj = go; rts.posX = PositionX; AddGameObjectToLine(false, rts); PositionX += label.printedSize.x + LabelSpacingX; str = str.Remove(0, sbstr.Length); if(str.Length >= 1) { AddGameObjectToLine(true, rts); PositionX = 0; RichTextStruct newText = new RichTextStruct(); newText.info = str; newText.type = RichTextInfoType.Text; CreateTextLabel(newText); } } /// /// 创建图片 /// /// private void CreateSprite(RichTextStruct rts) { string str = rts.info; if(string.IsNullOrEmpty(str)) return; GameObject go = NGUITools.AddChild(gameObject, prefab); go.name = rts.info; UISprite[] sprites = go.GetComponentsInChildren(); float maxWidth = 0; for (int i = 0; i < sprites.Length; i++) { //sprites[i].depth = Depth; sprites[i].pivot = UIWidget.Pivot.TopLeft; if(i == 0) { // 设置图片 SetSprite(sprites[i], str, false); } maxWidth = sprites[i].width > maxWidth ? sprites[i].width : maxWidth; } rts.gobj = go; // 加的6是为了外边那个表示等级的框,不然挤一起了 rts.posX = PositionX + 6; // 如果超过宽度,先换行 if(PositionX + maxWidth > LineWidth) { PositionX = 0; AddGameObjectToLine(true, null); } AddGameObjectToLine(false, rts); PositionX += maxWidth + LabelSpacingX; } private void SetSprite(UISprite sprite, string spriteName, bool bReset) { if( sprite == null ) return; /* // 图集没加载 if(string.IsNullOrEmpty( spriteName)) { sprite.spriteName = ""; sprite.enabled = false; return; } UISpriteData tempSprite = null; UIAtlas atlas = null; for(int i = 0; i < AtlastList.Length; i++) { atlas = AtlastList[i]; if(atlas == null) continue; tempSprite = atlas.GetSprite(spriteName); if(tempSprite != null) break; } if(tempSprite == null) { sprite.spriteName = ""; sprite.enabled = false; return; } sprite.atlas = atlas; */ sprite.spriteName = spriteName; sprite.enabled = true; if(bReset) { sprite.MakePixelPerfect(); } } private void AddGameObjectToLine(bool bNewLine, RichTextStruct rts) { if(bNewLine) { LineStruct newLine = new LineStruct(); // 这里处理,第一行的特殊情况 newLine.maxHeight = LineHeight; RichLineList.Add(newLine); return; } if(rts == null) return; LineStruct line = RichLineList[RichLineList.Count - 1]; line.RichList.Add(rts); UISprite sprite = rts.gobj.GetComponent(); if(sprite != null) { if(line.maxHeight < sprite.height) line.maxHeight = sprite.height; } } private void Sort() { float y = 0; UIWidget widget =gameObject.GetComponent(); for(int i = 0; i < RichLineList.Count; i++) { if(i > 0) { y -= (RichLineList[i - 1].maxHeight + LabelSpacingY); } foreach(RichTextStruct iter in RichLineList[i].RichList) { float tempY = y; if(iter.type == RichTextInfoType.Text) { tempY -= (RichLineList[i].maxHeight - LineHeight); } iter.gobj.transform.localPosition = new Vector3(iter.posX, tempY); } } } private void ClearChild(GameObject go) { if(!go) return; List goList = new List(); for( int i = 0; i < go.transform.childCount; i++ ) goList.Add(go.transform.GetChild(i).gameObject); go.transform.DetachChildren(); for(int i = goList.Count - 1; i >= 0; i--) { NGUITools.Destroy(goList[i]); goList.RemoveAt(i); } } private void CutString( string iString, string iCommand, out string oFirstStr, out string oLastStr ) { int EqualIndex = iString.IndexOf(iCommand); oFirstStr = iString.Substring(0, EqualIndex); oLastStr = iString.Substring(EqualIndex + iCommand.Length, iString.Length - (EqualIndex + iCommand.Length)); } }

你可能感兴趣的:(Unity,UI)