Unity UGUI图文混排源码(一)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq992817263/article/details/51112304

Unity UGUI图文混排源码(一):http://blog.csdn.net/qq992817263/article/details/51112304

Unity UGUI图文混排源码(二):http://blog.csdn.net/qq992817263/article/details/51112311


我从一开始想到的图文混排的概念都是通过文字间的空隙去粘贴一张图片,这样确定图片前面文字的最后一个位置变成了最主要的参数,接下来就给出两种解决方案

首先,先发UGUI源码的一个链接,很多东西可以参考他本身代码和一些可获取的属性:https://bitbucket.org/Unity-Technologies/ui


图文混排解决方案一:

通过获取preferredWidth和preferredHeight来确定他的位置,再通过空格,给图片留下位置,这里只提一下关键点,具体建议使用第二种解决方案

1.通过自定义标签,将文本中的文字分为几段

void DealTextInfor(string value)
    {
        if (m_spriteAsset == null)
            return;
        if (m_spriteAsset.listSpriteInfor.Count <= 0)
            return;

        if (value.Contains("<sprite="))
        {
            //获取到<sprite前面的所有内容 并确定最后一个字符的位置信息 再+四个空格 赋值给m_text
            int iNum = value.IndexOf("<sprite=");
            m_text.text += value.Substring(0, iNum);
            Debug.Log("m_Text preferredWidth:" + m_text.preferredWidth + "  m_Text preferredHeight:" + m_text.preferredHeight);
            //Debug.Log("m_Text line count:" + m_TextGenerator.GetLinesArray().Length);
            UILineInfo[] linesInfo = m_textGenerator.GetLinesArray();
            for (int i = 0; i < linesInfo.Length; i++)
            {
                Debug.Log("start char Index:" + linesInfo[i].startCharIdx + "start char:" + m_text.text.ToCharArray()[linesInfo[i].startCharIdx] + "     line height:" + linesInfo[i].height);
            }
            Vector3 textpos = Vector3.zero;
            int startCharIdx = linesInfo[linesInfo.Length - 1].startCharIdx;
            textpos.x = m_textGenerator.GetPreferredWidth(m_text.text.Substring(startCharIdx, m_text.text.Length - startCharIdx), m_textSetting);
            textpos.y = -(m_text.preferredHeight);
            Debug.Log("text pos:" + textpos);

            m_text.text += "    ";
            //获取到精灵索引的ID 并去掉精灵标签
            value = value.Substring(iNum, value.Length - iNum);
            Debug.Log("value:" + value);
            iNum = value.IndexOf("/>");
            int iSpriteIndex = int.Parse(value.Substring(0, iNum).Trim().Replace("<sprite=", ""));
            SaveSpriteInfor(textpos, m_textGenerator.GetPreferredWidth("    ", m_textSetting), iSpriteIndex);
            Debug.Log("sprite index:" + iSpriteIndex);

            value = value.Substring(iNum + 2, value.Length - iNum - 2);
            DealTextInfor(value);
        }
        else
        {
            m_text.text += value;
            DrawSprite();
        }
    }

2.将当前图片的前面的几段文字集合,通过获取这段文字的preferredWidth和preferredHeight来确定他的位置,这个局限性就在于文本最处于左上角或者左下角的排列位置,才能方便计算,同时如果文字的字体差异较大之后,preferredWidth和preferredHeight的位置并不能体现很好的效果

3.获取preferredWidth和preferredHeight的方式,可以通过Text自身的属性去直接获取,也可以通过代码去计算

 Text m_Text = GetComponent<Text>();

        TextGenerator m_TextGenerator = m_Text.cachedTextGeneratorForLayout;
        TextGenerationSettings m_TextGenerationSettings = m_Text.GetGenerationSettings(Vector2.zero);
        float fWidth = m_TextGenerator.GetPreferredWidth("一段文字,获取所占宽度", m_TextGenerationSettings);

        m_TextGenerationSettings = m_Text.GetGenerationSettings(new Vector2(m_Text.rectTransform.rect.x,0.0f));
        float fHeight = m_TextGenerator.GetPreferredHeight("一段文字,获取所占高度", m_TextGenerationSettings);

4.参考源码计算的方式

public virtual float preferredWidth
        {
            get
            {
                var settings = GetGenerationSettings(Vector2.zero);
                return cachedTextGeneratorForLayout.GetPreferredWidth(m_Text, settings) / pixelsPerUnit;
            }
        }
        
        public virtual float preferredHeight
        {
            get
            {
                var settings = GetGenerationSettings(new Vector2(rectTransform.rect.size.x, 0.0f));
                return cachedTextGeneratorForLayout.GetPreferredHeight(m_Text, settings) / pixelsPerUnit;
            }
        }

5.具体的表现效果参考上一篇文章:http://blog.csdn.net/qq992817263/article/details/51000744

猜你喜欢

转载自blog.csdn.net/qq992817263/article/details/51112304