原地址: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 oomlist.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(" "); 30 // Debug.Log("xpos=" + xe.GetAttribute("XPos")); 31 // Debug.Log(" "); 32 // Debug.Log("ypos=" + xe.GetAttribute("YPos")); 33 // Debug.Log(" "); 34 // Debug.Log("width=" + xe.GetAttribute("Width")); 35 // Debug.Log(" "); 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 oomlist.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(" "); 30 // Debug.Log("xpos=" + xe.GetAttribute("XPos")); 31 // Debug.Log(" "); 32 // Debug.Log("ypos=" + xe.GetAttribute("YPos")); 33 // Debug.Log(" "); 34 // Debug.Log("width=" + xe.GetAttribute("Width")); 35 // Debug.Log(" "); 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在图片左下角。