UGUI Mirror Image 镜像扩展图片

生成一张镜像图片扩展在原图片上,这样能使左右对称的图片只需要在资源中保存一半,节省了存储空间

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

[RequireComponent(typeof(Image))]
public class MyMirrorImage : BaseMeshEffect
{
    List<UIVertex> vertexs = new List<UIVertex>();
    public override void ModifyMesh(VertexHelper vh)
    {
        vertexs.Clear();
        vh.GetUIVertexStream(vertexs);
        ModifyVertices(vertexs);
        vh.Clear();
        vh.AddUIVertexTriangleStream(vertexs);
        

    }

    private void ModifyVertices(List<UIVertex> vertexs)
    {
        if (!IsActive() || vertexs.Count < 6)
        {
            return;
        }
        var image = GetComponent<Image>();
        if (image.sprite == null)
            return;
        var startPos = vertexs[0].position;
        var color = vertexs[0].color;
        var endPos = vertexs[vertexs.Count - 2].position;
        var tempVertexs = new List<UIVertex>();
        for (int i = 0; i < vertexs.Count; i++) //全部向左缩小一半 并且拷贝一份顶点
        {
            var v = vertexs[i];
            v.position.x = (v.position.x + startPos.x) / 2f;
            vertexs[i] = v;
            v.position.x += (endPos.x - startPos.x)/2f;
            tempVertexs.Add(v);
        }
        //将拷贝的一份镜像翻转
        {
            //1和23交换
            var v1 = tempVertexs[1];
            var v2 = tempVertexs[2];
            var v3 = tempVertexs[3];
            v2.position = v1.position;
            v1.position = v3.position;
            v3.position = v2.position;
            tempVertexs[1] = v1;
            tempVertexs[2] = v2;
            tempVertexs[3] = v3;
            //4和05交换
            v1 = tempVertexs[4];
            v2 = tempVertexs[0];
            v3 = tempVertexs[5];
            v2.position = v1.position;
            v1.position = v3.position;
            v3.position = v2.position;
            tempVertexs[4] = v1;
            tempVertexs[0] = v2;
            tempVertexs[5] = v3;
        }
        vertexs.AddRange(tempVertexs);
    }
}

效果

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张_0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值