项目中有使用到了很多对称的图片,于是美术哥为了减少图片占用的大小,提出要程序来将做对称,美术只需要出一半的图,剩余的一半有程序实现对称。参考文章:点击打开链接。

自己实现了一版本,将这个组件挂在拥有Image的对象上就可以了,要弄清楚几个点:

1.RectTransform中各项数值的意思

2.ModifyMesh调用时机(UGUI填充了图元数据后,会调用虚函数ModifyMesh给用户修改数据的机会)

3.获取的顶点数据的方法(VertexHelper)

4.Sprite的类型(Simple,Sliced)

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;public class MirrorSprite : BaseMeshEffect
{public enum eMirrorType{eHor,eVer,}[SerializeField]private eMirrorType mMirrorType;private List<UIVertex> mVerts = new List<UIVertex>();private List<int> mIndices = new List<int>();private UIVertex mVertex = new UIVertex();protected override void Awake(){mMirrorType = eMirrorType.eHor;}public override void ModifyMesh(VertexHelper vh){mVerts.Clear();for (int i = 0; i < vh.currentVertCount; i++){vh.PopulateUIVertex(ref mVertex, i);mVerts.Add(mVertex);}if(graphic is Image){Image image = graphic as Image;switch (image.type){case Image.Type.Simple:{DrawSimple();}break;case Image.Type.Sliced:{DrawSliced();}break;case Image.Type.Tiled:{}break;}}vh.Clear();vh.AddUIVertexStream(mVerts, mIndices);}public void SetIndice(){mIndices.Clear();for (int i = 0; i < mVerts.Count;){mIndices.Add(i);mIndices.Add(i + 1);mIndices.Add(i + 2);mIndices.Add(i);mIndices.Add(i + 2);mIndices.Add(i + 3);i = i + 4;}}//为了解决拼接处的缝隙问题,让uv进行偏移public void FixUV(){for (int i = 0; i < mVerts.Count; i++){var vertex = mVerts[i];Vector2 uv = vertex.uv0;int remainder = i % 4;if (mMirrorType == eMirrorType.eHor){if (remainder == 2 || remainder == 3){uv.x -= 1.5f / Screen.width;}}if (mMirrorType == eMirrorType.eVer){if (remainder == 1 || remainder == 2){uv.y -= 1.5f / Screen.height;}}vertex.uv0 = uv;mVerts[i] = vertex;}}//放缩//public void SimpleScale()//{//    Rect rect = graphic.GetPixelAdjustedRect();//    RectTransform rt = graphic.rectTransform;//    for (int i = 0; i < mVerts.Count; i++)//    {//        var vertex = mVerts[i];//        Vector3 position = vertex.position;//        if(mMirrorType == eMirrorType.eHor)//        {//            position.x = (position.x + rect.x) * 0.5f;//        }//        if (mMirrorType == eMirrorType.eVer)//        {//            position.y = (position.y + rect.y) * 0.5f;//        }//        vertex.position = position;//        mVerts[i] = vertex;//    }//}//镜像图片public void Mirror(){Rect rect = graphic.GetPixelAdjustedRect();switch (mMirrorType){case eMirrorType.eHor:{int count = mVerts.Count;for(int i = 0; i < count; i++){//公式: x1 - x = x - x2 => x1 = 2x - x2 (x1在对称轴x的右侧,x2在对称轴x的左侧)var vertex = mVerts[i];var position = vertex.position;position.x = rect.max.x * 2 - position.x;vertex.position = position;mVerts.Add(vertex);}}break;case eMirrorType.eVer:{int count = mVerts.Count;for (int i = 0; i < count; i++){var vertex = mVerts[i];var position = vertex.position;position.y = rect.max.y * 2 - position.y;vertex.position = position;mVerts.Add(vertex);}}break;}}public void DrawSimple(){FixUV();Mirror();SetIndice();}public void DrawSliced(){FixUV();Mirror();SetIndice();}
}

Unity 镜像sprite相关推荐

  1. unity镜像_通过镜像学习Unity Multiplayer Basics

    unity镜像 Unity is one of the most well-known and established engines for game development, and Mirror ...

  2. unity image sprite 赋值不显示

    Unity Image.sprite 与 Image.overrideSprite 的区别 https://blog.csdn.net/techfield/article/details/830104 ...

  3. 【Unity】Sprite Atlas功能讲解

    目录 SpriteAtlas创建方法 1.Type: Maskter母版 Variant变体 2.Include Build: 勾选时运行游戏时自动加载入内存中,否则需要手动加载(使用到的时候才会加载 ...

  4. [Unity][2D]Sprite精灵多个多边形切割

    一张大图中 含有多个 小图,如何把这些小图切割成精灵图片 这种背景图片是黑色背景的,最后切割分离出来的精灵还是会有黑色描边. 导入图片到Unity项目,选择Sprite,Sprite Mode 为Mu ...

  5. unity使用Sprite Editor图片切割功能减少性能损耗

    在unity里利用Sprite Editor切割本图片为多张图片,让Batches和SetPass calls的值处于最小值,用以减少性能损耗. 上图可以见到,尽管图片有十多张,但是Batches和S ...

  6. Unity 将Sprite打包进图集

    前言: 打包图集是一个很简单的过程,但是如果没有这样的经验,可能不知道如何下手 本博客大概说一下打包流程,以及一些基本原理 1.为什么要打包图集 首先要了解一点,在UGUI中即使你什么都不干,在项目打 ...

  7. Unity切割Sprite图集转换为多张图片

    为了可以使用Unity自带的精灵切割,要将纹理类型改成"Sprite","Sprite Mode"改成"Multiple","Fo ...

  8. Unity 创建Sprite导致的内存溢出奔溃问题

    在进行Sprite动态加载创建的时候,会碰到这样的代码: VideoMipmap.sprite = Sprite.Create(downloadTexture, new Rect(0, 0, down ...

  9. Unity 获取Sprite的宽和高

    float bound_x = go.GetComponent<SpriteRenderer>().bounds.size.x; float bound_y = go.GetCompone ...

最新文章

  1. 后缀的形容词_巧记形容词后缀 第三弹
  2. React开发(117):ant design 新方式
  3. 聊聊Spring Cloud版本的那些事儿
  4. MySQL占用系统进程_MySQL的Sleep进程占用大量连接解决方法
  5. NOIP2016普及组第一题:买铅笔
  6. 华为ac控制器web配置手册_欧姆龙AC伺服系统1S系列产品型号说明及功能介绍
  7. servlet3.1
  8. M6315模块连接阿里云物联网MQTT通讯
  9. react刷新页面_【跟着官网学React】Hello World
  10. micropython文件上传软件_MicroPython
  11. 基于微信小程序的比赛报名系统
  12. java离职证明模板word_离职证明模板Word免费版下载
  13. PCB LAYOUT 设计民睿科技有限公司承接PCBLAYOUT项目
  14. eNSP创建局域网vlan隔离
  15. 关于ARM的22个常用概念--的确经典
  16. 关于技术部管理的一些思考
  17. js页面加载实现loading提示效果
  18. qmcflac转flac的python实现
  19. JQuery制作3D导航栏切换动画
  20. 通过adb安装apk到android手机

热门文章

  1. 堆外内存(off-heap),堆内存(on-heap)
  2. Pranava Pra 使用教程
  3. Mac 使用音频工具分析音频数据
  4. h5 invoke android,uniapp安卓版本11.0.0以上真机调试App: onLaunch have been invoked
  5. 计算机组成存储器实验心得,《计算机组成原理》存储器读写实验报告
  6. Centos使用chrony做时间同步
  7. 【数学分析入门】R语言独立性检验方法
  8. HTML+CSS模仿百度首页(gird+flex布局)
  9. 云原生之使用Docker部署BookBrowser电子书浏览器
  10. C14:Unity3D制作智能家居设计软件——双屏互动室内漫游