UGUI——Text和TextEffect

OnPopulateMesh

PopulateWithErrors

https://img-blog.csdnimg.cn/20210114201020265.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Z6dGZ6dGZ6dA==,size_16,color_FFFFFF,t_70

TextGenerator is used to generate text for rendering, and caches vertices, character information, and line information to save memory.

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    public Font font;
    void Start()
    {
        TextGenerationSettings settings = new TextGenerationSettings();
        settings.textAnchor = TextAnchor.MiddleCenter;
        settings.color = Color.red;
        settings.generationExtents = new Vector2(500.0F, 200.0F);
        settings.pivot = Vector2.zero;
        settings.richText = true;
        settings.font = font;
        settings.fontSize = 32;
        settings.fontStyle = FontStyle.Normal;
        settings.verticalOverflow = VerticalWrapMode.Overflow;
        TextGenerator generator = new TextGenerator();
        generator.Populate("I am a string", settings);
        Debug.Log("I generated: " + generator.vertexCount + " verts!");
    }
}

https://img-blog.csdnimg.cn/20210114201522886.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Z6dGZ6dGZ6dA==,size_16,color_FFFFFF,t_70

BaseMeshEffect

Inherited from UIBehaviour, IMeshModifier IMeshModifier: Provides ModifyMesh interface, call flow: called in Graphic

 

Shadow

Add an outline to the image, which is the shadow effect

 protected void ApplyShadowZeroAlloc(List<UIVertex> verts, Color32 color, int start, int end, float x, float y)
  {
      UIVertex vt;

      var neededCapacity = verts.Count + end - start;
      if (verts.Capacity < neededCapacity)
          verts.Capacity = neededCapacity;

      for (int i = start; i < end; ++i)
      {
          vt = verts[i];
          verts.Add(vt);

          Vector3 v = vt.position;
          v.x += x;
          v.y += y;
          vt.position = v;
          var newColor = color;
          if (m_UseGraphicAlpha)
              newColor.a = (byte)((newColor.a * verts[i].color.a) / 255);
          vt.color = newColor;
          verts[i] = vt;
      }
  }

  /// <summary>
  /// Duplicate vertices from start to end and turn them into shadows with the given offset.
  /// </summary>
  /// <param name="verts">Vert list to copy</param>
  /// <param name="color">Shadow color</param>
  /// <param name="start">The start index in the verts list</param>
  /// <param name="end">The end index in the vers list</param>
  /// <param name="x">The shadows x offset</param>
  /// <param name="y">The shadows y offset</param>
  protected void ApplyShadow(List<UIVertex> verts, Color32 color, int start, int end, float x, float y)
  {
      ApplyShadowZeroAlloc(verts, color, start, end, x, y);
  }

  public override void ModifyMesh(VertexHelper vh)
  {
      if (!IsActive())
          return;

      var output = ListPool<UIVertex>.Get();
      vh.GetUIVertexStream(output);

      ApplyShadow(output, effectColor, 0, output.Count, effectDistance.x, effectDistance.y);
      vh.Clear();
      vh.AddUIVertexTriangleStream(output);
      ListPool<UIVertex>.Release(output);
  }
}

my notion notes

ModifyMesh

The call used to modify the grid. Called when the shape is filling the grid.

https://img-blog.csdnimg.cn/20210114205813591.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Z6dGZ6dGZ6dA==,size_16,color_FFFFFF,t_70

GetUIVertexStream

Get all vertex data

ApplyShadow

Copy all the vertices, modify the x, y of the copied vertices, add the effectDistance offset, modify the color, and add the vertex data

outLine

Inherit Shadow, which is Shadow in 4 directions, copying vertices 4 times

PositionAsUV1

An IVertexModifier that sets the original vertex position in UV1 of the generated vertex.

public override void ModifyMesh(VertexHelper vh)
{
   UIVertex vert = new UIVertex();
   for (int i = 0; i < vh.currentVertCount; i++)
   {
       vh.PopulateUIVertex(ref vert, i);
       vert.uv1 =  new Vector2(vert.position.x, vert.position.y);
       vh.SetUIVertex(vert, i);
   }
}

Guess you like

Origin blog.csdn.net/fztfztfzt/article/details/114023136