Unity UGUI 图片 轴对称效果 减少资源

制作UI的过程中,为了节省资源,对称的图一般美术切一半给我们

手动拼图 有时会出现拼接处出现裂缝或重叠

image

调整大小时也不方便 得一块一块调整

 

所以就用BaseMeshEffect 的ModifyMesh写了一个脚本

效果是

image

这样调整这种拼接的UI会方便一些

 using UnityEngine;

using System.Collections.Generic;
using UnityEngine.UI;

[AddComponentMenu( " UI/Effects/TestImageGhost ")]
public  class TestImageGhost : BaseMeshEffect
{
     public  enum Type
    {
        Double,
        Quad,
    }

    [SerializeField]
     private Type m_type = Type.Double;

    [SerializeField]
     private  bool m_UseGraphicAlpha =  true;

     private Vector2 xy;

     public Type GhostType
    {
         get {  return m_type; }
         set
        {
             if (m_type == value)
                 return;
            m_type = value;

             if (graphic !=  null)
                graphic.SetVerticesDirty();
        }
    }

#if UNITY_EDITOR
     protected  override  void OnValidate()
    {
        xy =  this.GetComponent<RectTransform>().sizeDelta;
         base.OnValidate();
         if (graphic !=  null)
            graphic.SetVerticesDirty();
    }
#endif
     protected  override  void OnEnable()
    {
         base.OnEnable();
         if (graphic !=  null)
            graphic.SetVerticesDirty();
        xy =  this.GetComponent<RectTransform>().sizeDelta;
        Debug.Log( " dfsdf ");
    }

     public  bool useGraphicAlpha
    {
         get {  return m_UseGraphicAlpha; }
         set
        {
            m_UseGraphicAlpha = value;
             if (graphic !=  null)
                graphic.SetVerticesDirty();
        }
    }

     protected  void ApplyGhostDouble(List<UIVertex> verts,  int start,  int end,  float x1,  float y1,  float x2,  float y2, bool self =  false)
    {
        UIVertex vt;

         var neededCpacity = verts.Count *  2;
         if (verts.Capacity < neededCpacity)
            verts.Capacity = neededCpacity;


         for ( int i = start; i < end; i++)
        {
            vt = verts[i];
             if(!self)
                verts.Add(vt); // 添加一遍mesh

            Vector3 v = vt.position;
             int offset = i %  6;
             switch (offset)
            {
                 case  0:
                 case  1:
                 case  5: v.x += x1;  break;
                 case  3:
                 case  4:
                 case  2: v.x += x2;  break;
            }
             switch (offset)
            {
                 case  1:
                 case  2:
                 case  3: v.y += y1;  break;
                 case  0:
                 case  4:
                 case  5: v.y += y2;  break;
            }

            vt.position = v;
            verts[i] = vt;
        }
    }

     protected  void ApplyGhost(List<UIVertex> verts,  int start,  int end)
    {
         if (m_type == Type.Double)
        {
             var neededCpacity = verts.Count *  2;
             if (verts.Capacity < neededCpacity)
                verts.Capacity = neededCpacity;
            ApplyGhostDouble(verts, start, end,  00, -xy.x /  2, 0);
            start = end;
            end = verts.Count;
            ApplyGhostDouble(verts, start, end, xy.x,  0, -xy.x /  20, true);
        }
         else
        {
             var neededCpacity = verts.Count *  4;
             if (verts.Capacity < neededCpacity)
                verts.Capacity = neededCpacity;
            start =  0;
            end = verts.Count;
            ApplyGhostDouble(verts, start, end,  0 ,  0, -xy.x /  2, xy.y /  2);
            start = end;
            end = verts.Count;
            ApplyGhostDouble(verts, start, end, xy.x , 0, -xy.x /  2, xy.y /  2);
            start = end;
            end = verts.Count;
            ApplyGhostDouble(verts, start, end,  0, -xy.y, -xy.x /  2, xy.y /  2);
            start = end;
            end = verts.Count;
            ApplyGhostDouble(verts, start, end, xy.x, -xy.y, -xy.x /  2, xy.y /  2true);
        }
    }

     public  override  void ModifyMesh(VertexHelper vh)
    {
         if (!IsActive())
             return;
        xy = GetComponent<RectTransform>().sizeDelta;

        List<UIVertex> output =  new List<UIVertex>();
        vh.GetUIVertexStream(output);
        ApplyGhost(output,  0, output.Count);
        vh.Clear();
        vh.AddUIVertexTriangleStream(output);
    }
}

 

转载于:https://www.cnblogs.com/rocbomb/p/5428345.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值