unity 内存中切割图片
一般的说我们切割图片是将一张图片资源切割成更小的图片资源,也就是说在资源上就进行了切割,比如ugui上的切割方法。
如果我们有一些情况比如做拼图,可能让玩家自己选择自己的生活照作为拼图的原图。
那么我们需要进行在内存中进行切割
Texture2D ScaleTextureCutOut(Texture2D originalTexture, float startX,float startY, float originalWidth, float originalHeight)
{
originalWidth = Mathf.Clamp(originalWidth, , Mathf.Max(originalTexture.width - startX,));
originalHeight = Mathf.Clamp(originalHeight, , Mathf.Max(originalTexture.height - startY,));
Texture2D newTexture = new Texture2D(Mathf.CeilToInt(originalWidth), Mathf.CeilToInt(originalHeight));
int maxX = originalTexture.width - ;
int maxY = originalTexture.height - ;
for (int y = ; y < newTexture.height; y++)
{
for (int x = ; x < newTexture.width; x++)
{
float targetX = x + startX;
float targetY = y + startY;
int x1 = Mathf.Min(maxX, Mathf.FloorToInt(targetX));
int y1 = Mathf.Min(maxY, Mathf.FloorToInt(targetY));
int x2 = Mathf.Min(maxX, x1 + );
int y2 = Mathf.Min(maxY, y1 + ); float u = targetX - x1;
float v = targetY - y1;
float w1 = ( - u) * ( - v);
float w2 = u * ( - v);
float w3 = ( - u) * v;
float w4 = u * v;
Color color1 = originalTexture.GetPixel(x1, y1);
Color color2 = originalTexture.GetPixel(x2, y1);
Color color3 = originalTexture.GetPixel(x1, y2);
Color color4 = originalTexture.GetPixel(x2, y2);
Color color = new Color(Mathf.Clamp01(color1.r * w1 + color2.r * w2 + color3.r * w3 + color4.r * w4),
Mathf.Clamp01(color1.g * w1 + color2.g * w2 + color3.g * w3 + color4.g * w4),
Mathf.Clamp01(color1.b * w1 + color2.b * w2 + color3.b * w3 + color4.b * w4),
Mathf.Clamp01(color1.a * w1 + color2.a * w2 + color3.a * w3 + color4.a * w4)
);
newTexture.SetPixel(x, y, color);
}
}
newTexture.anisoLevel = ;
newTexture.Apply();
return newTexture;
}
这个代码摘自网络上的,然后添加了起始位置,逻辑是将每一个对应点的色素值取出来,放置在内存中的图片点上
今天在做应用的时候发现,图片切割的起始位置是在左下角,于是,在做拼图或者其他游戏,需要的时候要注意进行Y值的换算
还有另外的一点就是所应用的图片如果在unity中的情况,需要勾选允许读取和写入
否则在进行读颜色值的时候会出现错误。
做拼图遇到还有的一个问题是,让玩家选取本地任意地点文件,参考 http://blog.csdn.net/awnuxcvbn/article/details/21277481
后面有发现项目中用到图集,但是原图丢失了,然后在整理图集的时候不是特别方便,把A图集的一项删除之后无法放到B中。于是就需要拆除原图
[MenuItem("Tools/Resume Sprite From Atlas")]
public static void ResumeSpriteFromAtlas()
{
Object[] go = Selection.objects;
for (int i = 0; i < go.Length; i++)
{
if (go[i].GetType() == typeof(GameObject))
{
GameObject resObject = go[i] as GameObject;
UIAtlas resAtlas = resObject.GetComponent<UIAtlas>();
if (resAtlas != null)
{
DecompressAtlas(resAtlas);
}
}
}
}
public static void DecompressAtlas(UIAtlas resAtlas)
{
string mainPath = "D://OutSprites/" + resAtlas.name;
if ( !System.IO.Directory.Exists(mainPath) )
{
System.IO.Directory.CreateDirectory(mainPath);
}
Material mainMaterial = resAtlas.spriteMaterial;
Texture2D mainTexture = (Texture2D)mainMaterial.mainTexture;
for(int i = 0; i < resAtlas.spriteList.Count; i++)
{
UISpriteData spData = resAtlas.spriteList[i];
Texture2D newTexture = new Texture2D(spData.width, spData.height);
Color[] needCopy = mainTexture.GetPixels(spData.x, mainTexture.height - (spData.y + spData.height), spData.width, spData.height);
newTexture.SetPixels(0, 0, newTexture.width, newTexture.height, needCopy);
newTexture.Apply();
byte[] pngBytes = newTexture.EncodeToPNG();
string filePath = mainPath + "/" + spData.name + ".png";
Stream st = System.IO.File.Open(filePath, FileMode.OpenOrCreate);
st.Write(pngBytes, 0, pngBytes.Length);
st.Flush();
st.Close();
UnityEngine.Debug.Log(filePath);
}
}
在untiy 编辑器下可以将选中的图集拆出原图,这样就又可以随便组合图集了
unity 内存中切割图片的更多相关文章
- Android内存中的图片
图片在内存中的大小 Android.graphics.Bitmap类里有一个内部类Bitmap.Config类,在Bitmap类里createBitmap(intwidth, int height, ...
- win7(旗舰版)下,OleLoadPicture 加载内存中的图片(MagickGetImageBlob),返回值 < 0
昨天去三哥家,想把拍好的照片缩小一下,我用很久前写的一个软件进行缩小,然后进行一次效果预览,这个时候弹出: Call OleLoadPicture Fail - loadPictureFromMW 奇 ...
- 知道内存中一个图片的指针IntPtr大小,转换成图片显示
//nSize 为总长度//pImageData 为总数据//nImageSize //一个图片的长度 byte[] _bytes = new byte[nImageSize];// //IntPtr ...
- Android 使用ContentProvider扫描手机中的图片,仿微信显示本地图片效果
版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请注明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/1873 ...
- iOS之在内存中绘图
与直接在UIView控件上绘图不同,在内存中绘图时,需要开发者自己准备绘图环境,Quartz 2D提供了一个非常便捷的函数:UIGraphicsBeginImageContext(CGSize siz ...
- Android高效内存:让图片占用尽可能少的内存
Android高效内存:让图片占用尽可能少的内存 一.让你的图片最小化 1.1 大图小图内存使用情况对比 大图:440 * 336 小图:220 * 168 小图的高宽都是大图的1/2--> ...
- 在 Excel 中设置图片
package com.smbea.demo.excel; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStr ...
- iOS图片加载到内存中占用内存情况
我的测试结果: 图片占用内存 图片尺寸 .png文件大小 1MB 512*512 316KB 4MB 10 ...
- 两种方法实现用CSS切割图片只取图片中一部分
切割图片这里不是真正的切割,只是用CSS取图片中的一部分而已,主要有两种方式,一是做为某一元素的背景图片,二是用img元素的属性.下面有个不错的示例,大家可以参考下 切割图片这里不是真正的切割,只是用 ...
随机推荐
- 转:AJAX中xhr对象详解
XJAX ,并不是一种新技术的诞生.它实际上代表的是几项技术按一定的方式组合在一在同共的协作中发挥各自的作用. 它包括: 使用XHTML和CSS标准化呈现: 使用DOM实现动态显示和交互: 使用XML ...
- php 错误处理函数
eval() 把子符串当做php 代码执行 // 回调函数function a($b, $c) { echo $b; echo $c; } call_user_func_array('a', ar ...
- php+js实现分页
使用onclick传递参数时,参数为空分页无效.因此无刷新页面时可利用js重新获取input的值同样通过get地址传递到分页显示的php页面.page参数接收和传递方式必须一致为post或get. j ...
- PHP面试题之驼峰字符串转换成下划线样式例子
自己在看到这个问题的时候,想到的是用ASCII码来处理,没往万能的正则上去想.好吧,下面来看看答案: 答案1: 代码如下 复制代码 $str = 'OpenAPI'; $length = mb_str ...
- 实现Launcher默认壁纸、选择壁纸定制化功能
需求功能说明: 该定制需求为在系统中增加一个新的分区如myimage,用以实现存放定制资源.例如在myimage下新建wallpaper文件夹用于存放定制的墙纸图片资源,当Launcher加载 ...
- 黑马程序员——【Java高新技术】——类加载器
---------- android培训.java培训.期待与您交流! ---------- 一.概述 (一)类加载器(class loader) 用来动态加载Java类的工具,它本身也是Java类. ...
- 哈希(Hash)与加密(Encrypt)相关内容
1.哈希(Hash)与加密(Encrypt)的区别 哈希(Hash)是将目标文本转换成具有相同长度的.不可逆的杂凑字符串(或叫做消息摘要),而加密(Encrypt)是将目标文本转换成具有不同长度的.可 ...
- u-boot平台的建立,驱动的添加,索引的创建,命令机制的实现.
一:U-boot移植前建立自己的平台: 关注的相关文件:1.u-boot- 2010.03/board/samsung/ //这个目录下需要创建自己的板级目录fsc100 cp –a smdkc100 ...
- IOS atomic与nonatomic,assign,copy与retain的定义和区别
IOS atomic与nonatomic,assign,copy与retain的定义和区别 atomic和nonatomic用来决定编译器生成的getter和setter是否为原子操作. ...
- OD18
介绍一个工具exescope 可以修改一些exe程序里的东西 通过这个工具 我们找到了我们要除掉的NAG窗口的具体位置 那我们可以通过OD进行跟踪 来到程序头下段 ...