用unity3d切割图片
原地址:http://www.cnblogs.com/leng-yuye/archive/2012/05/31/2528148.html
需求:把图片的像素中不为alpha的部分切出来保存成单个图片。
之前打算用Texture2D.GetPixel()遍历整张图片,判断每一个像素的alpha是否为0,接着对每一个像素判断是否为临界的像素点,最后用new一个Texture2D用Texture2D.SetPixel()赋值像素的RGBA。但是存在一种特殊的情况,那就是想要截取的图片中有alpha=0的时候,这个方法就蛋疼了。于是乎又另图思路,结合CEImagesetEditor这个开源的工具来完成这个工作,虽然这种方法不够"智能",但是截取的很准确。
用CEImagesetEditor工具定位要截取的位置和大小后能生成一个XML文件,这个文件包含要截取图片的名字、位置以及大小。XML文件形如:

<?xml version="1.0" encoding="UTF-8"?> <Imageset Name="test" Imagefile="test\roomlist.tga" >
<Image Name="name" XPos="190" YPos="59" Width="60" Height="25" />
<Image Name="photo" XPos="32" YPos="48" Width="127" Height="206" />
<Image Name="sumbit" XPos="55" YPos="264" Width="65" Height="38" />
</Imageset>

接着依次做的事情有:解析XML文件,新建Texture2D,根据XML的信息遍历制定的区域,获取指定区域的像素RGBA值,RGBA赋值给Texture2D,保存到当前工程的某一目录中。

1 using UnityEngine;
2 using System;
3 using System.Collections;
4 using System.Xml;
5 using System.IO;
6
7 public class ClipPic : MonoBehaviour {
8
9 Texture2D newTexture;
10 public Texture2D resTexture;
11 Color color;
12
13 string picName;
14 int picPos_x,picPos_y;
15 int picWidth,picHeight;
16
17
18 // Use this for initialization
19 void Start () {
20 XmlDocument xmlDoc = new XmlDocument();
21 string xml = Resources.Load("test").ToString();
22 xmlDoc.LoadXml(xml);
23 XmlNode xn = xmlDoc.SelectSingleNode("Imageset");
24 XmlNodeList xnl = xn.ChildNodes;
25 foreach (XmlNode xnf in xnl)
26 {
27 XmlElement xe = (XmlElement)xnf;
28 // Debug.Log("Name=" + xe.GetAttribute("Name"));
29 // Debug.Log("\t");
30 // Debug.Log("xpos=" + xe.GetAttribute("XPos"));
31 // Debug.Log("\t");
32 // Debug.Log("ypos=" + xe.GetAttribute("YPos"));
33 // Debug.Log("\t");
34 // Debug.Log("width=" + xe.GetAttribute("Width"));
35 // Debug.Log("\t");
36 // Debug.Log("height=" + xe.GetAttribute("Height"));
37 picName = xe.GetAttribute("Name");
38 picPos_x = Convert.ToInt32(xe.GetAttribute("XPos"));
39 picPos_y = Convert.ToInt32(xe.GetAttribute("YPos"));
40 picWidth = Convert.ToInt32(xe.GetAttribute("Width"));
41 picHeight = Convert.ToInt32(xe.GetAttribute("Height"));
42 newTexture = new Texture2D(picWidth,picHeight);
43 for(int m=picPos_y;m<picPos_y+picHeight;++m)
44 {
45 for(int n=picPos_x;n<picPos_x+picWidth;++n)
46 {
47 color = resTexture.GetPixel(n,resTexture.height-m);
48 newTexture.SetPixel(n-picPos_x,picHeight-(m-picPos_y),color);
49 }
50 }
51 newTexture.Apply();
52 byte[] b = newTexture.EncodeToPNG();
53 File.WriteAllBytes("Assets/Resources/out/"+picName+".png",b);
54 }
55
56 }
57
58 }

PS:图片坐标原点不同,CEImagesetEditor在图片左上角;Unity3d在图片左下角。
需求:把图片的像素中不为alpha的部分切出来保存成单个图片。 之前打算用Texture2D.GetPixel()遍历整张图片,判断每一个像素的alpha是否为0,接着对每一个像素判断是否为临界的像素点,最后用new一个Texture2D用Texture2D.SetPixel()赋值像素的RGBA。但是存在一种特殊的情况,那就是想要截取的图片中有alpha=0的时候,这个方法就蛋疼了。于是乎又另图思路,结合CEImagesetEditor这个开源的工具来完成这个工作,虽然这种方法不够"智能",但是截取的很准确。 用CEImagesetEditor工具定位要截取的位置和大小后能生成一个XML文件,这个文件包含要截取图片的名字、位置以及大小。XML文件形如: 复制代码
<?xml version="1.0" encoding="UTF-8"?> <Imageset Name="test" Imagefile="test\roomlist.tga" >
<Image Name="name" XPos="" YPos="" Width="" Height="" />
<Image Name="photo" XPos="" YPos="" Width="" Height="" />
<Image Name="sumbit" XPos="" YPos="" Width="" Height="" />
</Imageset>
复制代码
接着依次做的事情有:解析XML文件,新建Texture2D,根据XML的信息遍历制定的区域,获取指定区域的像素RGBA值,RGBA赋值给Texture2D,保存到当前工程的某一目录中。 复制代码
using UnityEngine;
using System;
using System.Collections;
using System.Xml;
using System.IO; public class ClipPic : MonoBehaviour { Texture2D newTexture;
public Texture2D resTexture;
Color color; string picName;
int picPos_x,picPos_y;
int picWidth,picHeight; // Use this for initialization
void Start () {
XmlDocument xmlDoc = new XmlDocument();
string xml = Resources.Load("test").ToString();
xmlDoc.LoadXml(xml);
XmlNode xn = xmlDoc.SelectSingleNode("Imageset");
XmlNodeList xnl = xn.ChildNodes;
foreach (XmlNode xnf in xnl)
{
XmlElement xe = (XmlElement)xnf;
// Debug.Log("Name=" + xe.GetAttribute("Name"));
// Debug.Log("\t");
// Debug.Log("xpos=" + xe.GetAttribute("XPos"));
// Debug.Log("\t");
// Debug.Log("ypos=" + xe.GetAttribute("YPos"));
// Debug.Log("\t");
// Debug.Log("width=" + xe.GetAttribute("Width"));
// Debug.Log("\t");
// Debug.Log("height=" + xe.GetAttribute("Height"));
picName = xe.GetAttribute("Name");
picPos_x = Convert.ToInt32(xe.GetAttribute("XPos"));
picPos_y = Convert.ToInt32(xe.GetAttribute("YPos"));
picWidth = Convert.ToInt32(xe.GetAttribute("Width"));
picHeight = Convert.ToInt32(xe.GetAttribute("Height"));
newTexture = new Texture2D(picWidth,picHeight);
for(int m=picPos_y;m<picPos_y+picHeight;++m)
{
for(int n=picPos_x;n<picPos_x+picWidth;++n)
{
color = resTexture.GetPixel(n,resTexture.height-m);
newTexture.SetPixel(n-picPos_x,picHeight-(m-picPos_y),color);
}
}
newTexture.Apply();
byte[] b = newTexture.EncodeToPNG();
File.WriteAllBytes("Assets/Resources/out/"+picName+".png",b);
} } }
复制代码
PS:图片坐标原点不同,CEImagesetEditor在图片左上角;Unity3d在图片左下角。
用unity3d切割图片的更多相关文章
- unity 内存中切割图片
一般的说我们切割图片是将一张图片资源切割成更小的图片资源,也就是说在资源上就进行了切割,比如ugui上的切割方法. 如果我们有一些情况比如做拼图,可能让玩家自己选择自己的生活照作为拼图的原图. 那么我 ...
- 两种方法实现用CSS切割图片只取图片中一部分
切割图片这里不是真正的切割,只是用CSS取图片中的一部分而已,主要有两种方式,一是做为某一元素的背景图片,二是用img元素的属性.下面有个不错的示例,大家可以参考下 切割图片这里不是真正的切割,只是用 ...
- 实现用CSS切割图片的方法
切割图片这里不是真正的切割,只是用CSS取图片中的一部分而已.这样做的好处就是减少了打开网页时请求图片的次数.主要有两种方式,一是做为某一元素的背景图片,二是用img元素的属性. 方法一: 用CSS中 ...
- Android调用相册拍照控件实现系统控件缩放切割图片
android 下如果做处理图片的软件 可以调用系统的控件 实现缩放切割图片 非常好的效果 今天写了一个demo分享给大家 package cn.m15.test; import java.io.By ...
- Android下利用Bitmap切割图片
在自己自定义的一个组件中由于需要用图片显示数字编号,而当前图片就只有一张,上面有0-9是个数字,于是不得不考虑将其中一个个的数字切割下来,需要显示什么数字,只需要组合一下就好了. 下面是程序的关键代码 ...
- C#实现自动切割图片
由于做一个TD游戏需要一些图片素材,可是现有的从网上下载的<保卫萝卜>的图片资源是多张图片合在一起的,并且没有什么规则,虽然有 个xml文件似乎用来描述此图片内子图片位置大小等信息,但由于 ...
- Android 调用相册 拍照 实现系统控件缩放 切割图片
android 下如果做处理图片的软件 可以调用系统的控件 实现缩放切割图片 非常好的效果 今天写了一个demo分享给大家. package cn.m15.test; import java.io.B ...
- iOS 切割图片
- (UIImage *)CutImageWithImage:(UIImage *)image withRect:(CGRect)rect { //使用CGImageCreateWithImageIn ...
- Android 中通过切割图片创建人物行走动画
以前一直使用序列图片来实现动画效果,造成空间的极大浪费,所以想要尝试下切割图片来实现动画. 如图所示,是由66rpg纸娃娃系统生成的角色行走图.本程序必须实现将人物的整体图片切割后存入4x4的数组来动 ...
随机推荐
- 怎样简单编写一个html网页
# 转载请留言联系 一个HTML的基本结构如下: <!DOCTYPE html> <html lang="en"> <head> <met ...
- win32 listctrl控件右键菜单的实现
HMENU Menu_list,Menu_all; POINT point; HINSTANCE hInstance;//下面代码放到BOOL WINAPI DialogProc下 case WM_C ...
- list 迭代器随机范围内移动
Increments an iterator by a specified number of positions. template<class InputIterator, class Di ...
- php使用gd库将文字转换成图片(转)
GD库是干什么用的呢!它是php处理图形的扩展库,GD库提供了一系列用来处理图片的API,使用GD库可以处理图片,或者生成图片. <?php header("Content-type: ...
- 带着问题学git
序 作为git新手,常见的git clone,push,commit命令已经足够完成一次代码的发布,但是如果不幸碰到问题往往会束手无策,利用网络问答解决了之后也不知其所以然.所以,做一次好奇宝宝吧! ...
- 【转】python argparse用法总结
转自:https://www.jianshu.com/p/fef2d215b91d 1. argparse介绍 是python的一个命令行解析包,非常编写可读性非常好的程序 2. 基本用法 prog. ...
- [thinkphp] ajaxReturn案例
javascript: <script> $('.ajax-post').click(function(){ var action_url=$('form').attr('action') ...
- Python3 list与循环练习(购物车)
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Author;Tsukasa product_list = [ ('Iphone',5800), (' ...
- Eureka Server设计(转载 石杉的架构笔记)
目录: 一.问题起源 二.Eureka Server设计精妙的注册表存储结构 三.Eureka Server端优秀的多级缓存机制 四.总结 一.问题起源 Spring Cloud架构体系中,Eurek ...
- pthread条件变量
pthread条件变量等待条件有两种方式:无条件等待pthread_cond_wait()和计时等待pthread_cond_timedwait(),其中计时等待方式如果在给定时刻前条件没有满足,则返 ...