Unity 裁剪或者拷贝图片
方法一:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /// <summary> /// 裁剪或者拷贝图片,图片的原点在左下角 /// </summary> /// <param name="原图"></param> /// <param name="x,y表示从原图的什么位置开始裁剪,w,h表示裁剪的宽高"></param> /// <param name="x,y表示拷贝到新的图片中的什么位置,w,h新的图片的宽高"></param> /// <returns></returns> Texture2D CopyOrCutTexture(Texture2D source, RectInt cutScope, RectInt targetScope) { Color[] colors = source.GetPixels(cutScope.x, cutScope.y, cutScope.width, cutScope.height); Texture2D target = new Texture2D(targetScope.width, targetScope.height, source.format, false ); target.SetPixels(targetScope.x, targetScope.y, targetScope.width, targetScope.height, colors); return target; } |
方法二:
1 2 3 4 5 6 7 8 9 10 11 12 13 | /// <summary> /// 裁剪或者拷贝图片,图片的原点在左下角 /// </summary> /// <param name="原图"></param> /// <param name="x,y表示从原图的什么位置开始裁剪,w,h表示裁剪的宽高"></param> /// <param name="x,y表示拷贝到新的图片中的什么位置,w,h新的图片的宽高"></param> /// <returns></returns> Texture2D CutOrCopyTexture(Texture2D source, RectInt cutScope, RectInt targetScope) { Texture2D target = new Texture2D(targetScope.width, targetScope.height, source.format, false ); Graphics.CopyTexture(source, 0, 0, cutScope.x, cutScope.y, cutScope.width, cutScope.height, target, 0, 0, targetScope.x, targetScope.y); return target; } |
完整代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class CopyTexture : MonoBehaviour { public Texture2D source; void Start () { //Texture2D target = CopyOrCutTexture(source, new RectInt(0, 0, 500, 500), new RectInt(0, 0, 500, 500)); //byte[] data = target.EncodeToPNG(); //System.IO.File.WriteAllBytes("E:\\temp.png", data); Texture2D target = CutOrCopyTexture(source, new RectInt(0, 0, 500, 500), new RectInt(0, 0, 500, 500)); byte [] data = target.EncodeToPNG(); System.IO.File.WriteAllBytes( "E:\\temp.png" , data); } /// <summary> /// 裁剪或者拷贝图片,图片的原点在左下角 /// </summary> /// <param name="原图"></param> /// <param name="x,y表示从原图的什么位置开始裁剪,w,h表示裁剪的宽高"></param> /// <param name="x,y表示拷贝到新的图片中的什么位置,w,h新的图片的宽高"></param> /// <returns></returns> Texture2D CopyOrCutTexture(Texture2D source, RectInt cutScope, RectInt targetScope) { Color[] colors = source.GetPixels(cutScope.x, cutScope.y, cutScope.width, cutScope.height); Texture2D target = new Texture2D(targetScope.width, targetScope.height, source.format, false ); target.SetPixels(targetScope.x, targetScope.y, targetScope.width, targetScope.height, colors); return target; } /// <summary> /// 裁剪或者拷贝图片,图片的原点在左下角 /// </summary> /// <param name="原图"></param> /// <param name="x,y表示从原图的什么位置开始裁剪,w,h表示裁剪的宽高"></param> /// <param name="x,y表示拷贝到新的图片中的什么位置,w,h新的图片的宽高"></param> /// <returns></returns> Texture2D CutOrCopyTexture(Texture2D source, RectInt cutScope, RectInt targetScope) { Texture2D target = new Texture2D(targetScope.width, targetScope.height, source.format, false ); Graphics.CopyTexture(source, 0, 0, cutScope.x, cutScope.y, cutScope.width, cutScope.height, target, 0, 0, targetScope.x, targetScope.y); return target; } } |
博客园Jason_c微信打赏码
如果本篇文档对你有帮助,打赏Jason_c根华子吧,他的私房钱被老婆没收了,呜呜!

【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步