Unity使用C#不失真修改图片的分辨率
来源:互联网 发布:淘宝培训公司靠谱吗 编辑:程序博客网 时间:2024/05/17 08:53
unity每个项目都需要使用同一张图的不同的分辨率 icon,这样只要一个最大的分辨率图片就可以了,需要使用的时候直接修改图片的分辨率,用完还可以删除掉,是不是既方便又可以节省不少空间。不逼逼直接上脚本
修改图片分辨的方法
public static Texture2D ReSetTextureSize(Texture2D tex, int width, int height){ var rendTex = new RenderTexture(width, height, 24, RenderTextureFormat.ARGB32); rendTex.Create(); Graphics.SetRenderTarget(rendTex); GL.PushMatrix(); GL.Clear(true, true, Color.clear); GL.PopMatrix(); var mat = new Material(Shader.Find("Unlit/Transparent")); mat.mainTexture = tex; Graphics.SetRenderTarget(rendTex); GL.PushMatrix(); GL.LoadOrtho(); mat.SetPass(0); GL.Begin(GL.QUADS); GL.TexCoord2(0, 0); GL.Vertex3(0, 0, 0); GL.TexCoord2(0, 1); GL.Vertex3(0, 1, 0); GL.TexCoord2(1, 1); GL.Vertex3(1, 1, 0); GL.TexCoord2(1, 0); GL.Vertex3(1, 0, 0); GL.End(); GL.PopMatrix(); var finalTex = new Texture2D(rendTex.width, rendTex.height, TextureFormat.ARGB32, false); RenderTexture.active = rendTex; finalTex.ReadPixels(new Rect(0, 0, finalTex.width, finalTex.height), 0, 0); finalTex.Apply(); return finalTex;}
顺便这里 给大家写个图片的保存方法:
public static void SaveTexture(Texture2D tex, string toPath){ using (var fs = File.OpenWrite(toPath)) { var bytes = tex.EncodeToPNG(); fs.Write(bytes, 0, bytes.Length); }}
再补充个图片自动压缩功能:
public static bool CompressTexture(params string[] texturePath){ var shell = BabySystem.babyFrameWorkAbsolutePath + "TextureTools/"; if (BabySystem.activeBuildTarget == BuildTarget.iOS) shell = shell + "mac/pngquant"; else shell = shell + "win/pngquant.exe"; var strCmdText = "--ext .png " + "--force -- " + string.Join(" ",texturePath); System.Diagnostics.Process process = new System.Diagnostics.Process(); System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; startInfo.FileName = shell; startInfo.Arguments = strCmdText; process.StartInfo = startInfo; process.Start(); process.WaitForExit(); int ExitCode = process.ExitCode; if (ExitCode != 0) { Debug.LogError("Run CMD CompressTexture Failed : "+ExitCode); return false; } return true;}
最后 附上这个压缩工具的下载地址:
pngquant下载
阅读全文
0 0
- Unity使用C#不失真修改图片的分辨率
- C#修改图片分辨率
- 不失真的图片展示
- C#生成缩略图不失真的方法
- 图片压缩不失真
- 不失真缩放图片
- Java生成图片时使用BufferedImage的构造方法,让图片不失真
- 我的Android进阶之旅------>Android使用9Patch图片作为不失真背景
- 我的Android进阶之旅------>Android使用9Patch图片作为不失真背景
- C#代码修改图片分辨率大小
- 使用9-Patch图片实现不失真按钮背景
- 【iOS开发】修改图片的大小分辨率 使用mac
- 图片不失真比例缩放
- 图片极致压缩,不失真
- android图片压缩不失真
- Unity 设置图片和屏幕的分辨率
- vc++ mfc 里保存缩放的bmp图片 不失真
- PS中放大图片不失真的方法
- java工程师初进公司的准备工作
- 滑块
- 海思--Hi3516aMpp开发环境配置
- 变量和表达式
- QTcpSocket类和QTcpServer类
- Unity使用C#不失真修改图片的分辨率
- 线性表之基于链表的实现
- 扉页
- Oracle分组统计查询-分组查询
- C/C++ 数组传参原理
- 编译通过,可运行时找不到类,提示 classLink not found 之类的错误
- 机器学习实战代码详解(11)使用Apriori算法进行关联分析
- 前端基础小案例:纯css制作旋转披萨
- 随便写写