Unity uGUI原理解析-BaseMeshEffect

Unity uGUI原理解析-BaseMeshEffect

BaseMeshEffect是用于实现网格效果的抽象类实现IMeshModifier接口,是Shadow、Outline等效果的基类。
Unity uGUI原理解析-Graphic可以知道,Graphic 在执行 DoMeshGeneration 时会获取到当前GameObject上的所有实现 IMeshModifier 的组件。 并且会调用 ModifyMesh 方法来修改当前Graphic 的Mesh数据。BaseMeshEffect 的类结构图如下:

BaseMeshEffect

BaseMeshEffect 源码解析

BaseMeshEffect 源码特别简单大致分为三个部分:

  1. 获取当前 GameObject 上的 Graphic 组件。
  2. OnEnable OnDisable OnDidApplyAnimationProperties 等时候使用 SetVerticesDirty 方法标记。
  3. 实现IMeshModifierModifyMesh方法。当然这里是使用抽象方法进行实现。后续会有 Shadow Outline PositionAsUV1 等子类来实现
[ExecuteAlways]
public abstract class BaseMeshEffect : UIBehaviour, IMeshModifier
{
    [NonSerialized]
    private Graphic m_Graphic;

    /// <summary>
    /// The graphic component that the Mesh Effect will aplly to.
    /// </summary>
    protected Graphic graphic
    {
        get
        {
            if (m_Graphic == null)
                m_Graphic = GetComponent<Graphic>();
            return m_Graphic;
        }
    }

    protected override void OnEnable()
    {
        base.OnEnable();
        if (graphic != null)
            graphic.SetVerticesDirty();
    }

    protected override void OnDisable()
    {
        if (graphic != null)
            graphic.SetVerticesDirty();
        base.OnDisable();
    }

    /// <summary>
    /// Called from the native side any time a animation property is changed.
    /// </summary>
    protected override void OnDidApplyAnimationProperties()
    {
        if (graphic != null)
            graphic.SetVerticesDirty();
        base.OnDidApplyAnimationProperties();
    }
    public abstract void ModifyMesh(VertexHelper vh);
}

Shadow源码解析

通过源码可以看到 Shadow 继承抽象类 BaseMeshEffect。 另外在设置 effectColoreffectDistance以及 useGraphicAlpha 时调用 graphic.SetVerticesDirty 方法来标记需要更新。

那么是如何绘制这个阴影的呢? 可以看下是是如何实现 ModifyMesh 方法的。 部分代码如下:

public class Shadow : BaseMeshEffect
{
    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) // 遍历从 start 到 end 的顶点
        {
            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)
    {
    	// ...
        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);
    }
}

可以看到 ApplyShadowZeroAlloc 这个方法是具体的实现地方。
实际的操作也很简单:

  1. 遍历所有顶点数据
  2. 拷贝一份顶点数据
  3. 修改顶点的偏移,对应 EffectDistance 这个参数
  4. 修改颜色信息,对应 Effect Color 以及 UseGraphicAlpha这两个参数
  5. 更新旧顶点数据

Outline源码解析

从Outline源码中可以看到是继承自Shadow这个类。Outline的实现简单来说就是在四周在绘制四遍。不用说这个方案顶点数量变成了5倍,优化方案可以看这篇文章

public class Outline : Shadow
{

    public override void ModifyMesh(VertexHelper vh)
    {
        // ...
        var verts = ListPool<UIVertex>.Get();
        vh.GetUIVertexStream(verts);

        // 由于需要绘制
        var neededCpacity = verts.Count * 5;
        if (verts.Capacity < neededCpacity)
            verts.Capacity = neededCpacity;

        var start = 0;
        var end = verts.Count;
        ApplyShadowZeroAlloc(verts, effectColor, start, verts.Count, effectDistance.x, effectDistance.y);

        start = end;
        end = verts.Count;
        ApplyShadowZeroAlloc(verts, effectColor, start, verts.Count, effectDistance.x, -effectDistance.y);

        start = end;
        end = verts.Count;
        ApplyShadowZeroAlloc(verts, effectColor, start, verts.Count, -effectDistance.x, effectDistance.y);

        start = end;
        end = verts.Count;
        ApplyShadowZeroAlloc(verts, effectColor, start, verts.Count, -effectDistance.x, -effectDistance.y);

        vh.Clear();
        vh.AddUIVertexTriangleStream(verts);
        ListPool<UIVertex>.Release(verts);
    }
}

PositionAsUV1源码解析

将原始顶点位置设置为生成的顶点的uv1。主要是给shader提供数据的(法线贴图坐标),没什么好看的。

public class PositionAsUV1 : BaseMeshEffect
{
    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);
        }
    }
}

案例参考

Unity3D UGUI优化:制作镜像图片(1)
Unity3D UGUI优化:制作镜像图片(2)
Unity3D UGUI优化:制作镜像图片(2)
UGUI Text渐变、弯曲、文字跳动(BaseMeshEffect拓展)

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity3D UGUI 专用文字特效插件 Text Effects 1.15 UI Text Effects are a set of effects for the standard Unity UI (uGUI) Text. - All script and shader sources included! - Extremely easy to use - just choose the effect from the component menu, and it's applied. - Add fancy titles, custom text appearance, mix multiple effects. - Rich Text support. - Mobile platform support. 12 extremely valuable effects: - Better Outline: a more continuous outline than the standard one. - Gradient Color: global/local, vertical/horizontal, override/additive/multiply. - Depth Effect: add thickness to text. - Soft Shadow: blurry shadow. - Outer Bevel: add outer lit and shaded edges. - Skew Effect: add horizontal+vertical transformations and perspective. - Curve Effect: bend or distort text vertically. - Character Spacing: increase or decrease the distance between individual characters. - Limit Visible Characters: hide characters, make a typewriter. - Overlay Texture: add an image overlay, local/global, override/additive/multiply. - Inner Bevel: add lit and shaded edges inside the characters, override/additive/multiply (only "override" on SM2 level GPUs). - Inner Outline: add outline inside the characters, override/additive/multiply. - With this package, you can finally echo effects from Photoshop or Word, while still using the standard Unity UI Text. - Suggestions for new effects are very welcome. The newly made effects will be added to the package. 仅供学习交流使用,如有侵权请告知删除。
Unity UGUIUnity游戏开发中常用的用户界面实现方法。UGUIUnity自带的用户界面系统,可以通过创建UI元素来构建游戏的界面。在Unity中,UGUI可通过创建Button、Text、Image等UI组件来实现交互和展示。 在UGUI中,通常选择ISO模型进行2D平面开发。通过锁定界面可以防止镜头发生偏移,确保界面的稳定性。 添加Button点击事件的方法如下: 1. 在Hierarchy面板中创建一个空物体GameObject。 2. 在Project面板中创建一个新的C#脚本,命名为OnClickButton,并将该脚本添加到GameObject上。 3. 双击OnClickButton脚本,在打开的脚本编辑器中输入相应的代码。该代码的作用是在点击按钮后输出"You click the button."和若干次"Yes"。 4. 在Click方法中使用print函数输出相关信息,并通过循环打印"Yes"特定次数。 5. 完成脚本的编写后,将脚本保存并返回Unity编辑器界面。 以上是关于Unity UGUI的简要介绍和Button点击事件的添加方法。希望对你有所帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [超详细的Unity UGUI教学](https://blog.csdn.net/qq_37701948/article/details/106682377)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值