Unity 裁剪或者拷貝圖片


方法一:

	/// <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;
	}

  

完整代碼:

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;
	}
}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM