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

Unity UGUI图文混排源码(一)的更多相关文章

  1. Unity UGUI图文混排源码(三) -- 动态表情

    这里是根据图文混排源码(二)进一步修改的,其他链接也不贴了,就贴一个链接就好了,第一次看这文章的同学可以先去看看其他几篇文章 Unity UGUI图文混排源码(二):http://blog.csdn. ...

  2. Unity UGUI图文混排源码(二)

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

  3. Unity UGUI图文混排源码(四) -- 聊天气泡

    这里有同学建议在做聊天气泡时,可以更改为一张图集对应多个Text,这样能节省资源,不过我突然想到每个Text一个图集,可以随时更换图集,这样表情图更丰富一些,于是我就先将现有的聊天demo改为了聊天气 ...

  4. Unity UGUI图文混排(六) -- 超链接

    图文混排更新到超链接这儿,好像也差不多了,不过就在最后一点,博主也表现得相当不专业,直接整合了山中双木林同学提供的超链接的解决方案,博主甚至没来得及细看就直接复制了,但感觉还是挺好用的. 博主已经将超 ...

  5. Unity UGUI图文混排(七) -- 下划线

    之前更新超链接的时候,忘了搭配实现一个下划线的功能,这篇文章就是来补上这一个功能,时间有点长,一方面没有很好的思路,一方面也没多少时间. 先在网上收集了一下下划线的实现操作,一种是在文本下再创建一个文 ...

  6. Unity UGUI图文混排(五) -- 一张图集对应多个Text

    继上一篇说的更新了一张图集对应多个Text的功能,为了节省资源嘛 这里,但是也没有舍弃之前的一个Text一个图集,因为我感觉应该两个都有用,于是我重新写了一个脚本 1.其实大体跟前面的都没变,解析标签 ...

  7. Unity琐碎(3) UGUI 图文混排解决方案和优化

    感觉使用Unity之后总能看到各种各样解决混排的方案,只能说明Unity不够体恤下情啊.这篇文章主要讲一下个人在使用过程中方案选择和优化过程,已做记录.顺便提下,开源很多意味着坑,还是要开实际需求. ...

  8. [UGUI]图文混排(二):Text源码分析

    UGUI源码: https://bitbucket.org/Unity-Technologies/ui/downloads/?tab=tags 首先下载一份UGUI源码,这里我下载的版本是5.3.2f ...

  9. [UGUI]图文混排(三):资源管理

    1.图文混排中的资源,主要是图片. 2.所谓的资源管理,可以分为资源对象池和资源加载这两部分.这里是为图文混排单独做一套资源管理,当然也可以改为调用项目中的资源管理. RichTextResource ...

随机推荐

  1. leetcode刷题笔记342 4的幂

    题目描述: 给定一个整数 (32位有符整数型),请写出一个函数来检验它是否是4的幂. 示例:当 num = 16 时 ,返回 true . 当 num = 5时,返回 false. 问题进阶:你能不使 ...

  2. PHP MySQL Update

    UPDATE 语句用于中修改数据库表中的数据. 更新数据库中的数据 UPDATE 语句用于更新数据库表中已存在的记录. 语法 UPDATE table_name SET column1=value, ...

  3. How to Change Default Web ADI Upload Parameters for FlexField Import / Validation

    How to Change Default Web ADI Upload Parameters for FlexField Import / Validation (文档 ID 553345.1) 转 ...

  4. SQL Server 扩展事件(Extented Events)从入门到进阶(4)——扩展事件引擎——基本概念

    本文属于 SQL Server 扩展事件(Extented Events)从入门到进阶 系列 在第一二节中,我们创建了一些简单的.类似典型SQL Trace的扩展事件会话.在此过程中,介绍了很多扩展事 ...

  5. java.lang.ClassNotFoundException:org.springframework.web.context.ContextLoaderListener问题解决

    今天搭建SSH项目的时候出现了如下错误: 严重: Error configuring application listener of class org.springframework.web.con ...

  6. Leetcode解题-链表(2.2.3)PartitionList

    题目:2.2.3 Partition List Given a linked list and a value x, partition it such that all nodes less tha ...

  7. 21 RadioGroup ListFragment

    结构 MainActivity.java package com.qf.day21_radiogroupfragment_demo3; import java.util.ArrayList; impo ...

  8. Swift的print不换行打印的方法

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) swift大多数情况下我们直接用默认的print函数打印就可以 ...

  9. [ExtJS5学习笔记]第五节 使用fontawesome给你的extjs5应用增加字体图标

    本文地址:http://blog.csdn.net/sushengmiyan/article/details/38458411本文作者:sushengmiyan-------------------- ...

  10. JVM的GC(概念与深入)

    深入浅出了解什么是GC: http://my.oschina.net/xianggao/blog/86985 GC策略详解: http://blog.csdn.net/winniepu/article ...