其他分享
首页 > 其他分享> > 「Unity3D」UGUI Text <quad/> 使用详解:一种实现图文混排的策略

「Unity3D」UGUI Text <quad/> 使用详解:一种实现图文混排的策略

作者:互联网

首先,我们看一下官方说明。

This is only useful for text meshes and renders an image inline with
the text.

关键词在于:text meshesimage inline ——这说明,<quad/>设计是用在TextMesh而不是Text,并且它是一个文本内嵌图片,即可以自动图文混排

其次,看一下官方例子。

<quad material=1 size=20 x=0.1 y=0.1 width=0.5 height=0.5>

This selects the material at position in the renderer’s material array
and sets the height of the image to 20 pixels.

The rectangular area of image starts at given by the x, y, width and height values, which are all given as a fraction of the unscaled width and height of the texture.

<quad material=1 size=0300 width=2    height=1 />
<quad material=1 size=0300 width=1    height=1 />
<quad material=1 size=0300 width=0.5  height=1 />

在这里插入图片描述

<quad material=1 size=0300 width=1  height=2/>
<quad material=1 size=0300 width=1  height=1/>
<quad material=1 size=0300 width=1  height=0.5/>

在这里插入图片描述
因此,我们可以看出:像素宽度 = size * (width / height)。也就是说,只要width和height保持1:1,像素宽度就不变,同时width越大显示宽度越大,height越大显示高度越小,反之width越小显示宽度越小,height越小显示高度越大。

<quad material=1 size=0300 width=2   height=2/>
<quad material=1 size=0300 width=1   height=1 />
<quad material=1 size=0300 width=0.5 height=0.5 />

在这里插入图片描述

接着,在Text中使用<quad/>的情况。

但好的一面就是:<quad/>的属性可以正确设置图片大小,而<quad/>的大小会自动排版,那么我们就可以用<quad/>来做占位,进行图文混排的实现。

然后,使用<quad/>在Text中图文混排,需要解决的问题。

第一个问题,可以使用重写Textprotected override void OnPopulateMesh(VertexHelper toFill)方法来解决,即找到<quad/>占位的四个顶点,然后将四个顶点的坐标设置成同一个即可。

第二个问题,可以利用<quad/>顶点的position直接,将正确的图片替换到正确的未知。

如何确定<quad/>的顶点,在toFill中索引?

这里有一个技巧,就是如果<quad material=1,2,3,4,5,6,7 />即material!=0,那么所有<quad/>顶点的就会集中在toFill的尾部——这样我们就可以,把Text中的<quad/>的数组按照顺序,从toFill的尾部替换即可。

核心的实现代码如下:

protected override void OnPopulateMesh(VertexHelper toFill)
{
    base.OnPopulateMesh(toFill);

    var count = this.spriteInfoList.Count;

    if (count > 0)
    {
        var vertexLastIndex = toFill.currentVertCount - 1;
        var spriteLastIndex = count                   - 1;
        var leftBottom      = new UIVertex();

        // if <quad material!=0/> then the <quad/> data at the end of toFill
        for (var i = spriteLastIndex; i > -1; --i)
        {
            // remove <quad/> display from last
            var index = vertexLastIndex - (spriteLastIndex - i) * 4;

            toFill.PopulateUIVertex(ref leftBottom, index);     // LB
            toFill.SetUIVertex     (    leftBottom, index - 1); // RB
            toFill.SetUIVertex     (    leftBottom, index - 2); // RT
            toFill.SetUIVertex     (    leftBottom, index - 3); // LT

            // get image from last
            var imageRT = this.transform.GetChild(i).GetComponent<Image>().rectTransform;
            var pos     = (Vector2) leftBottom.position + imageRT.sizeDelta / 2;
            // set sprite pos by <quad/> vertex pos
            imageRT.SetLocalPositionXY(pos);
        }                  
    }
}
}

最后,使用<quad/>实现Text图文混排的限制。

这些限制,其实是Text实现的bug。

标签:Unity3D,Text,material,height,width,图文混排,var,toFill
来源: https://blog.csdn.net/tom_221x/article/details/113456808