• NGUI图集切割代码


    原地址:http://blog.csdn.net/u011440375/article/details/9707491

    因为最近工作用NGUI比较多,修改图集时还没原图,有时候需要把图集重新切割开来,用代码会比较方便,一下贴出主要代码
    
    首先读取NGUI图集的信息
    
    [csharp] view plaincopy
    UIAtlas mAtlas  ;  
             GameObject[] SelectedAsset=Selection.gameObjects;  
             if (SelectedAsset.Length == 1)  
             {  
                 mAtlas = SelectedAsset[0].GetComponent<UIAtlas>();  
      
      
                 if (mAtlas != null)  
                 {  
                     string atlasFile = string.Format("{0}.txt", mAtlas.name);  
                     string atlasPng = string.Format("{0}.png", mAtlas.name);  
                     char[] ch = { '.' };  
                     string path = AssetDatabase.GetAssetPath(SelectedAsset[0]);  
                     FileInfo aFileinfo = new FileInfo(path);  
                     string th = aFileinfo.DirectoryName;  
                     string[] strs = path.Split(ch);  
      
                     if (File.Exists(atlasFile))  
                     {  
                         File.Delete(atlasFile);  
                     }  
                     StreamWriter sw = new StreamWriter(strs[0] + ".txt");  
                     StringBuilder sb = new StringBuilder();  
                     foreach (UIAtlas.Sprite sprite in mAtlas.spriteList)  
                     {  
                         sb.AppendLine(string.Format("name:{0}, coordinate:{1}", sprite.name, sprite.outer));  
                     }  
      
                     sw.Write(sb.ToString());  
                     sw.Close();  
      
                     string[] arg = new string[2];  
                     arg[0] = atlasPng;  
                     arg[1] = th;  
                     //print(arg[0]);  
                     //print(arg[1]);  
                     string path_2 = AssetDatabase.GetAssetPath(Resources.Load("CutAltas/output"));  
                     FileInfo aFileinfo_2 = new FileInfo(path_2);  
                     string th_2 = aFileinfo_2.DirectoryName;  
                     print(th_2);  
                     string s = th_2 + "\Start.exe";  
    这是主要的代码,接下来用vs编写一个程序切割图集,主要代码如下
    [csharp] view plaincopy
    namespace WindowsFormsApplication1  
    {  
        public partial class Form1 : Form  
        {  
            string[] p;  
            string path = string.Empty;  
            public Form1(string[] s)  
            {  
                InitializeComponent();  
                p = s;  
                ImageName.Text = p[0];  
                path = p[1];  
            }  
      
            private void button1_Click(object sender, EventArgs e)  
            {  
                CutImage(path);  
                Application.Exit();  
                 
            }  
              
            void CutImage(string p)   
            {  
                string path=@"c:Documents and SettingsAdministrator桌面";  
                string savePath=string.Empty;  
                string imagePath = p + "\"+ImageName.Text;  
                string textPath = string.Empty;  
                Image img = Image.FromFile(imagePath);  
                  
                char[] c = { '.' };  
                string st= ImageName.Text;  
                string[] sts = st.Split(c);  
                savePath = path + sts[0];  
                if (!Directory.Exists(savePath))  
                {  
                    Directory.CreateDirectory(savePath);  
                }  
                textPath = p + "\"+sts[0]+".txt";  
                StreamReader sr = new StreamReader(textPath);  
      
                while (!sr.EndOfStream)  
                {  
                    string newLine = sr.ReadLine();  
                 //   Regex reg = new Regex(@"name:(?P<name>.+), coordinate:(x:(?P<x>d+).00, y:(?P<y>d+).00, (?P<width>d+).00, height:(?P<height>d+).00)");  
                 //   Regex regName = new Regex(@"name:.*");  
                    string patternName = @"name:w*";  
                    string patternX = @"coordinate:(x:w*.dd";  
                    string patternY = @"y:d*.dd";  
                    string patternWidth = @"d*.dd";  
                    string patternHeight = @"height:d*.dd";  
                    string name = savePath+"\";  
                    float x=0,y=0,width=0,height=0;  
                    char[] maoHao = { ':' };  
                      
                    MatchCollection imageName = Regex.Matches(newLine, patternName, RegexOptions.ExplicitCapture);  
      
                    foreach (Match nextMatch in imageName)  
                    {  
                        int index = nextMatch.Index;  
                        string result = nextMatch.ToString();  
                         
                        string[] re = result.Split(maoHao);  
                        name += re[re.Length - 1];  
                        name += ".png";  
                      //  MessageBox.Show(re[1]);  
                    }  
                    MatchCollection positionX = Regex.Matches(newLine, patternX, RegexOptions.ExplicitCapture);  
                    foreach (Match nextMatch in positionX)  
                    {  
                        int index = nextMatch.Index;  
                        string result = nextMatch.ToString();  
      
                        string[] re = result.Split(maoHao);  
                        x = float.Parse(re[re.Length - 1]);  
                    //    MessageBox.Show(x.ToString());  
                    }  
                    MatchCollection positionY = Regex.Matches(newLine, patternY, RegexOptions.ExplicitCapture);  
                    foreach (Match nextMatch in positionY)  
                    {  
                        int index = nextMatch.Index;  
                        string result = nextMatch.ToString();  
      
                        string[] re = result.Split(maoHao);  
                        y = float.Parse(re[re.Length-1]);  
                    //    MessageBox.Show(y.ToString());  
                    }  
                    MatchCollection widths = Regex.Matches(newLine, patternWidth, RegexOptions.ExplicitCapture);  
                    foreach (Match nextMatch in widths)  
                    {  
                        int index = nextMatch.Index;  
                        string result = nextMatch.ToString();  
      
                        string[] re = result.Split(maoHao);  
                        width = float.Parse(re[re.Length - 1]);  
                    //    MessageBox.Show(width.ToString());  
                    }  
                    MatchCollection heights = Regex.Matches(newLine, patternHeight, RegexOptions.ExplicitCapture);  
                    foreach (Match nextMatch in heights)  
                    {  
                        int index = nextMatch.Index;  
                        string result = nextMatch.ToString();  
      
                        string[] re = result.Split(maoHao);  
                        height = float.Parse(re[re.Length - 1]);  
                     //   MessageBox.Show(height.ToString());  
                     //   MessageBox.Show("Name: " + name + "x :" + x.ToString() + "y :" + y.ToString() + "width :" + width.ToString() + "height :" + height.ToString());  
                    }  
                //    MessageBox.Show("Name: "+name+"x :"+x.ToString()+"y :"+y.ToString()+"width :"+width.ToString()+"height :"+height.ToString());  
                    Bitmap bmp = new Bitmap(img);  
                    Rectangle rec = new Rectangle((int)x, (int)y, (int)width, (int)height);  
                    bmp = bmp.Clone(rec, bmp.PixelFormat);  
                    bmp.Save(name, System.Drawing.Imaging.ImageFormat.Png);  
                }  
               
                MessageBox.Show("切割完成,返回桌面查看!");  
            //    Bitmap bmp = new Bitmap(img, 100, 100);  
              //  bmp.Save(path + "newImage.jpg");  
            }  
        }  
    }  
    最后unity要调用vs生成的程序,并且传进参数
    static bool StartProcess(string filename, string[] args)
        {
            //print("wwww");
            try
            {
                string s="";
                foreach(string arg in args) 
                {
                    s += string.Format(""{0}" ", arg);
                }
                s = s.Trim();
                print(s);
                Process myprocess = new Process();
                ProcessStartInfo startInfo = new ProcessStartInfo(filename,s);
                myprocess.StartInfo = startInfo;
                myprocess.StartInfo.UseShellExecute = false;
                myprocess.Start();
                return true;
            }
            catch (Exception ex)
            {
                UnityEngine.Debug.Log("出错原因:" + ex.Message);
            }
            return false; 
        
        }
  • 相关阅读:
    Delphi String的散漫记录,真是知识无数,陷阱无数(转)
    GB2312 编码
    Windows系统字体与文件对照表
    Windows字符集的统一与转换
    Delphi 的绘图功能[10]
    各种字符集和编码详解
    NTC热敏电阻参数
    Spring Cloud 学习笔记(一)——入门、特征、配置
    上坡路定点停车与坡道起步
    网闸结构和工作原理
  • 原文地址:https://www.cnblogs.com/123ing/p/3722359.html
Copyright © 2020-2023  润新知