用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="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在圖片左下角。


免責聲明!

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



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