zoukankan      html  css  js  c++  java
  • 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;
    	}
    }
    

      

    博客园Jason_C技术交流群

    扫描二维码加入qq群:623307256,共同探讨工作中遇到的Unity相关的问题!

  • 相关阅读:
    Nginx初探
    很详细的Nginx配置说明
    linux环境下Apache+Tomcat集群配置
    Apache+Tomcat集群配置
    apache + tomcat 负载均衡分布式集群配置
    js 如何将dom转换为 图片(base64)
    饥荒猪镇物品 代码
    angular2 图片赋值的时候前面自动加 unsafe:xxx 导致图片信息不显示问题
    angular6、7 兼容ie9、10、11
    @angular/cli (angular脚手架) 如何降级
  • 原文地址:https://www.cnblogs.com/Jason-c/p/14303306.html
Copyright © 2011-2022 走看看