基于NGUI的富文本实现--终成正果

第一次,连续做了这么多天的功能,第一次把我想要的结果完整无误的呈现出来,又有几人能明白这种付出后看到成果展现在眼前的那种心情。

今天服务器维护,很早就赶到公司去上班了!一天都没睡几个钟头,实在是很疲、很困。
代码是写完了,可是还有很多bug,甚至还很黄很暴力。
特别有几个地方实在是用了不是办法的办法。

    // 第一个让人觉得恐怖而且不可靠的地方 RichCompSortHelper.CalculatePrintedSize
    private static Vector2 CalculatePrintedSize(UILabel label, string text)
    {
        var old_text = label.text;
        label.text = text;
        var size = label.localSize;
        label.text = old_text;

        Debug.Log(text + "   size : " + size.ToString());

        return size;
    }
    // 不知道你们看出了什么没有。那个大小的计算方法,是不是有点不可理喻
    // 本来以为调用NGUIText的CalculatePrintedSize方法,就能够计算出大小,结果不知道是什么地方出现了问题,一直没有办法拿到正确的值。
    // 虽然说Unity底层会采用数据缓冲池类似的技术,去实现延迟渲染。这个text根本不会被渲染出来,但是这种做法也是挺取巧的。
    // 实在是还没有研究透NGUI那个方法出现了什么问题,所以才采用这种方式。

    // 也不是没有什么补偿,就是在调用这段代码的方法里的顺序遍历方法,改善成了二分查找法,算是尽可能提高效率吧
    // 第二个地方 就是RichLabel的换行切割方法   自己调用自己生成对象
    // 本来是想做一个克隆方法的,所有的处理都在外面调用层去处理,结果写着写着,就成为内部处理,外部处理,一起上了。
    public RichLabel[] SubComponent(string[] lines)
    {
        if (lines != null && lines.Length > 1)
        {
            var list = new List<RichLabel>();
            for (int i = 0; i < lines.Length; ++i)
            {
                if (i == 0)
                {
                    this.m_label.text = lines[i];
                    this.m_text = lines[i];
                    this.m_rich_event.Init();
                }
                else
                {
                    var temp_rich_comp = new RichLabel(this.m_ctrl.GameObj.transform, this.m_ctrl, this.m_font, this.m_fontsize, this.m_fontstyle, lines[i], this.m_rich_event);
                    if (temp_rich_comp != null) list.Add(temp_rich_comp);
                }
            }
            return list.ToArray();
        }
        return null;
    }

最后附上一张整体效果图和调用数据

    private string rich_txt = @"
    <label id = '111',can_click = 'true',font = 'SciFi/SciFi Font - Header', fontsize = '20',fontstyle = 'Normal',text = 'First LabelFirstLabelsdfsdfadfasdfaewoooooooooooooooppppppppppppppsssssssssssssssscccccccccccccc222222222222' />
    <sprite atlas_name = 'SciFi/SciFi Atlas',sprite_name = 'NGUI',width = '30',height = '18'/>
    <spamin atlas_name = 'Wooden Atlas',sprite_name = 'Button',prefix_sprite_name = 'Button',loop = 'true'/>
    <texture texture_name = 'Wooden Atlas',width = '25',height = '18'/>
    <label font = 'SciFi/SciFi Font - Normal', fontsize = '22',fontstyle = 'Normal',text = 'S
n
e
n
c
o
nnd Label' />
    <texture texture_name = 'Wooden Atlas',width = '65',height = '108'/>
    ";
    void OnGUI()
    {
        if(GUILayout.Button("TestRich"))
        {
            RichLabelMgr.CreateNewRichLab(rich_txt, this.transform, new Vector2(200, 200), NGUIText.Alignment.Left, (string id) =>
            {
                Debug.Log("richevent  " + id);
            });
        }
    }

成果图

ps:工程已经全部上传到了GIT,感兴趣的可以下载

猜你喜欢

转载自blog.csdn.net/biospc/article/details/80542087
今日推荐