翻墙看来的代码,其实自己写也不会很麻烦,就是花点时间而已(我只是代码的搬运工。。。)

水平翻转图片

  Texture2D FlipTexture(Texture2D orig) {Texture2D flip = new Texture2D(orig.width,orig.height);int xN = orig.width;int yN = orig.height;for(int i=0; i < xN; i++) {for(int j=0; j < yN; j++) {flip.SetPixel(xN-i-1, j, orig.GetPixel(i,j));}}flip.Apply();return flip;}

垂直翻转图片

  Texture2D FlipTexture(Texture2D orig) {Texture2D flip = new Texture2D(orig.width,orig.height);int xN = orig.width;int yN = orig.height;for(int i=0; i < xN; i++) {for(int j=0; j < yN; j++) {flip.SetPixel(i, yN - j - 1, orig.GetPixel(i,j));}}flip.Apply();return flip;}

缩放TextureScale.cs

using UnityEngine;using UnityEngine.UI;
using System.Collections;
using System.Threading;public class TextureScale : MonoBehaviour {public class ThreadData{public int start;public int end;public ThreadData(int s, int e){start = s;end = e;}}private static Color[] texColors;private static Color[] newColors;private static int w;private static float ratioX;private static float ratioY;private static int w2;private static int finishCount;private static Mutex mutex;public static Texture2D Point(Texture2D tex, int newWidth, int newHeight,bool flipH,bool flipV){return ThreadedScale(tex, newWidth, newHeight, false,flipH,flipV);}public static Texture2D Bilinear(Texture2D tex, int newWidth, int newHeight,bool flipH,bool flipV){return ThreadedScale(tex, newWidth, newHeight, true,flipH, flipV);}private static Texture2D ThreadedScale(Texture2D tex, int newWidth, int newHeight, bool useBilinear,bool flipH,bool flipV){texColors = tex.GetPixels();newColors = new Color[newWidth * newHeight];if (useBilinear){ratioX = 1.0f / ((float)newWidth / (tex.width - 1));ratioY = 1.0f / ((float)newHeight / (tex.height - 1));}else{ratioX = ((float)tex.width) / newWidth;ratioY = ((float)tex.height) / newHeight;}w = tex.width;w2 = newWidth;var cores = Mathf.Min(SystemInfo.processorCount, newHeight);var slice = newHeight / cores;finishCount = 0;if (mutex == null){mutex = new Mutex(false);}if (cores > 1){int i = 0;ThreadData threadData;for (i = 0; i < cores - 1; i++){threadData = new ThreadData(slice * i, slice * (i + 1));ParameterizedThreadStart ts = useBilinear ? new ParameterizedThreadStart(BilinearScale) : new ParameterizedThreadStart(PointScale);Thread thread = new Thread(ts);thread.Start(threadData);}threadData = new ThreadData(slice * i, newHeight);if (useBilinear){BilinearScale(threadData);}else{PointScale(threadData);}while (finishCount < cores){Thread.Sleep(1);}}else{ThreadData threadData = new ThreadData(0, newHeight);if (useBilinear){BilinearScale(threadData);}else{PointScale(threadData);}}tex.Resize(newWidth, newHeight);tex.SetPixels(newColors);tex.Apply();Texture2D orig = new Texture2D(tex.width, tex.height);if (flipV){int xN = tex.width;int yN = tex.width;for (int i = 0; i < xN; i++){for (int j = 0; j < yN; j++){// tex.SetPixel(xN - i - 1, j, orig.GetPixel(i, j));orig.SetPixel(i, yN - j - 1, tex.GetPixel(i, j));}}orig.Apply();}else if (flipH){int xN = tex.width;int yN = tex.width;for (int i = 0; i < xN; i++){for (int j = 0; j < yN; j++){// tex.SetPixel(xN - i - 1, j, orig.GetPixel(i, j));orig.SetPixel(xN - i - 1, j, tex.GetPixel(i, j));}}orig.Apply();}else{orig = tex;}return orig;}public static void BilinearScale(System.Object obj){ThreadData threadData = (ThreadData)obj;for (var y = threadData.start; y < threadData.end; y++){int yFloor = (int)Mathf.Floor(y * ratioY);var y1 = yFloor * w;var y2 = (yFloor + 1) * w;var yw = y * w2;for (var x = 0; x < w2; x++){int xFloor = (int)Mathf.Floor(x * ratioX);var xLerp = x * ratioX - xFloor;newColors[yw + x] = ColorLerpUnclamped(ColorLerpUnclamped(texColors[y1 + xFloor], texColors[y1 + xFloor + 1], xLerp),ColorLerpUnclamped(texColors[y2 + xFloor], texColors[y2 + xFloor + 1], xLerp),y * ratioY - yFloor);}}mutex.WaitOne();finishCount++;mutex.ReleaseMutex();}public static void PointScale(System.Object obj){ThreadData threadData = (ThreadData)obj;for (var y = threadData.start; y < threadData.end; y++){var thisY = (int)(ratioY * y) * w;var yw = y * w2;for (var x = 0; x < w2; x++){newColors[yw + x] = texColors[(int)(thisY + ratioX * x)];}}mutex.WaitOne();finishCount++;mutex.ReleaseMutex();}private static Color ColorLerpUnclamped(Color c1, Color c2, float value){return new Color(c1.r + (c2.r - c1.r) * value,c1.g + (c2.g - c1.g) * value,c1.b + (c2.b - c1.b) * value,c1.a + (c2.a - c1.a) * value);}}

原来只有缩放,我把翻转也给加进去了,静态方法,很容易调用

恩,大概就是这些了

Unity 使用C#翻转图片并缩放相关推荐

  1. 图片(旋转/缩放/翻转)变换效果(ccs3/滤镜/canvas)

    以前要实现图片的旋转或翻转,只能用ie的滤镜来实现,虽然canvas也实现,但ie不支持而且不是html标准. css3出来后,终于可以用标准的transform来实现变换,而canvas也已成为ht ...

  2. 设计图片转换html5,在HTML5中翻转图片

    貌似 HTML5 的 Canvas 只提供了图片的旋转.缩放功能,没有提供图片翻转(水平翻转或垂直翻转)的支持,搜索加试验之后,得到几种实现图片翻转的方法,记录一下. 第一种最简单的是使用 CSS,代 ...

  3. mac上使用sips命令快速裁剪、旋转、翻转图片

    mac上使用sips命令快速裁剪.旋转.翻转图片 日常开发工作中,经常碰到要对图片进行一些简单的处理,不需要动用PS,在mac上就有一个很好的命令行工具:sips 这里我们不具体展开讲,仅贴出几个常用 ...

  4. [Unity]利用Mesh在Unity中绘制扇形图片

    背景 最近碰到个功能, 要画一个扇形图案, 如下图: 美术原图: 需求是这个图形跟随角色, 在角色背后, 并且每个角色的扇形角度可能不同. So, NGUI和UGUI很好用的FilledType是用不 ...

  5. 《OpenCV3编程入门》学习笔记6 图像处理(六)图像金字塔与图片尺寸缩放

    6.6 图像金字塔与图片尺寸缩放 6.6.1 图像金字塔 1.图像金字塔是图像中多尺度表达的一种,主要用于图像分割,是一种以多分辨率解释图像的结构,通过梯次向下采样获得分辨率逐步降低的图象集合 2.分 ...

  6. canvas 图片不能缩放显示在画布的问题 忘记设置dw,dh

    在制作红包效果时,new image显示的图片由于没有设置drawImage的dw,dh,图片没有缩放在画布.是对drawImage没有认识好 context.drawImage(img,x,y) c ...

  7. iOS transform解决连续多次旋转缩放,实现图片旋转缩放效果

    一.需求 实现imageView的缩放旋转效果,一般有两种方式: 1.底层加scrollview,利用scrollview的属性实现.(推荐这种,这是我比较后发现的,手势做缩放旋转会有点弊端) 2.利 ...

  8. [转]图片自动缩放 js图片缩放

    转自:http://hi.baidu.com/crystalhx/blog/item/deba9b2320274340ac34de09.html 图片自动缩放 js图片缩放 2008-03-27 10 ...

  9. android 多点触控缩放,Android多点触控(图片的缩放Demo)

    本文主要介绍Android的多点触控,使用了一个图片缩放的实例,来更好的说明其原理.需要实现OnTouchListener接口,重写其中的onTouch方法. 实现效果图: 源代码: 布局文件: ac ...

最新文章

  1. 凶猛的飞禽 超跑奥迪
  2. 还不知道 Redis 分布式锁的背后原理?还不赶快学习一下
  3. .NET Core开发日志——结构化日志
  4. elementui图片上传php,vue+element-ui+富文本————图片上传
  5. 对象可以在栈上分配空间吗?_Java面试题之:Java中所有的对象都分配在堆中吗?...
  6. python解决数据不均衡,上采样方法解决
  7. 古风手机壁纸,国潮的你不可错过!
  8. @Override错误
  9. 单片机sprintf函数的用法_C++小知识之sprintf用法
  10. 综述摘要怎么写?(含7大容易被忽略的注意事项及80%综述文章常见句型汇总)...
  11. Linux之奇怪的知识---supervisor超级守护进程的意义和使用方法
  12. Laya Air+Unity3D双引擎带你做个天空球3D小游戏(下篇)
  13. 华为鸿蒙摄像头,随时随地看一看!华为首款鸿蒙智能摄像头发布
  14. 数据中台开源解决方案(一)
  15. Vue修改网页浏览器标签的标题和图标
  16. 直升飞机领衔、三百辆婚车开道,三十台巨型吊车,小伟婚礼超豪华
  17. Enterprise Library 4.1数据访问应用程序块快速入门【6】使用DataSet更新数据库
  18. xp计算机共享上限,Win7/xp系统下共享文件夹最大连接数限制怎么解除
  19. 自动化运维-Ansible 运维自动化 ( 配置管理工具 )
  20. HijackThis日志细解--清净网络(复杂详尽)

热门文章

  1. jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry for key PRIMARY异常解决办法
  2. 微信公众号,微信小程序开发运营
  3. 激活函数Relu 及 leakyRelu
  4. 中兴新支点操作系统加入腾讯发起的OpenCloudOS开源社区:希望百花齐放
  5. html div高度跟随父级,html – CSS – 相对定位父div不伸展到绝对子div高度
  6. BurpSuite pro v2020.1 最新版本,看到这些新功能后我心动了
  7. obs studio 在windows10 系统下编译安装过程
  8. 师范专业是考计算机二级vpf,「温馨提醒」计算机二级考试VFP上机操作指南
  9. jstree实现左右2棵树的联动
  10. 使用 Apache Commons CLI 解析命令行参数示例