asdfasdfasdfsdfdasf

来源:互联网 发布:pbft算法 编辑:程序博客网 时间:2024/06/14 18:31

尊重他人的劳动,支持原创,转载请注明出处:http://dsqiu.iteye.com    

       由于NGUI 已经到3.0的版本,FastGUI 1.3.2只能支持NGUI2.6.3,由于NGUI 2.7版本以后很多脚本的命名和属性都改了,所以就不能用了,然后我就只有自己动手改造。

       主要改动和改进有:

               1)图集名的动态替换,以前为了公用其他图集的图片,每次在Unity中导入之后都有收到替换其他图集和图片,挺麻烦的,所以就干脆在PSD的图层组中加了命名规则来自动替换,比如 PSD 图层组 backgroun(common_bg1), 这样background的  这张图片就会自动替换成common的bg1图片,并且不导出background组内的图片。

              2)具有更多的可扩展行,简化了原来的逻辑,原来的逻辑很复杂,而且没必要,其实NGUI 的基本组件就是UIWidget的子类UILabel和UISprite,当然还有UITexture,其他都可以看做UIWidget的组合。也就是说定义的Button,Input等都是这些的集合,只要将定义的组件的成员变量进行手动赋值初始化一下就可以了。这样如果你要自己定义一个组件,只要在Unity中导入的时候,写好对应的初始化操作就可以了,然后在PSD的图层中指定一个命名规则就可以了。

              3)根据NGUI3.0的DrawCall规则,对UIWdiget进行了按图集排序,“机械”的减少DC。

              4)手动输出View类的需要的一些代码,比如找到某些控件等。

『PSD2NGUI

       FastGUI致命的缺点:导出的时间太久了,用过的人一定深有体会。然后发现还有另外一款插件——PSD2NGUI,看了下应该是完全脱离Adobe ExtenedScripte Toolkit的运行环境,由于手头的版本跟NGUI的版本没有对应,所以就没有具体试过,应该会很不错吧。』

                                                                                                                                                                                                                   增补于 2014,01,07 22:50

下面附上代码:

        使用Adobe ExtenedScript Toolkit  执行的脚本: 

Js代码 复制代码 收藏代码
  1. #target photoshop 
  2. app.bringToFront(); 
  3. var originalRulerUnits = preferences.rulerUnits; 
  4. preferences.rulerUnits = Units.PIXELS; 
  5. preferences.typeUnits = TypeUnits.PIXELS; 
  6. var cleanDocumentName = ""
  7. var targetImageFolders; 
  8. var actualPath  = ""
  9. var textInfos    = ""
  10. var baseDoc     = app.activeDocument; 
  11. var docWidth    = baseDoc.width.value; 
  12. var docHeight   = baseDoc.height.value; 
  13. var stackorder  = 0; 
  14. var parentObj = ""
  15. var layerData; 
  16.  
  17. var layerReadCount = 0; 
  18. var totalLayerCount = baseDoc.layers.length; 
  19.  
  20. var win, windowResource; 
  21. var createProgressWindow, progressWindow; 
  22. var exportType = "WIDGETS"
  23. // create a string to hold the data 
  24. var str ="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  25. // add header line1 
  26. str += "<psd width=\"" + docWidth + "\" height=\"" + docHeight + "\">\n"
  27.  
  28. function cLayer(doc, layer)  
  29.     this.layerWidth = layer.bounds[2].value - layer.bounds[0].value; 
  30.     this.layerHeight = layer.bounds[3].value - layer.bounds[1].value; 
  31.  
  32.     this.middleCenterX = this.layerWidth / 2 + layer.bounds[0].value; 
  33.     this.middleCenterY = this.layerHeight / 2 + layer.bounds[1].value; 
  34.  
  35.     this.center = this.middleCenterX +", " +this.middleCenterY; 
  36.  
  37.  
  38.     return this
  39. function getActiveLayerID() 
  40.    var d = getActiveLayerDescriptor(); 
  41.    return d.getInteger(cTID('LyrI')); 
  42.  
  43. function cTID(s) {return app.charIDToTypeID(s);} 
  44. function sTID(s) {return app.stringIDToTypeID(s);} 
  45. function getActiveLayerDescriptor() { 
  46.    var ref = new ActionReference(); 
  47.    ref.putEnumerated(cTID('Lyr '), cTID('Ordn'), cTID('Trgt')); 
  48.    return executeActionGet(ref); 
  49.  
  50.  
  51. function newDocFromLayer( curDoc, newDocName, layer ) 
  52.     var newDoc = app.documents.add( curDoc.width, curDoc.height, curDoc.resolution, newDocName , NewDocumentMode.RGB, DocumentFill.TRANSPARENT);  
  53.     newDoc.activeLayer.isBackgroundLayer = false
  54.     app.activeDocument = curDoc;  
  55.     layer.duplicate( newDoc );  
  56.     app.activeDocument = newDoc; 
  57.     return app.activeDocument; 
  58. function RemoveLayerSetsFromObject(pTargetObj, pRemoveInvisible) 
  59.     var arrToRemove = []; 
  60.     for(var i = 0; i < pTargetObj.layers[0].layers.length; i++) 
  61.     { 
  62.         if(pTargetObj.layers[0].layers[i].typename =="LayerSet"
  63.         { 
  64.             arrToRemove.push(pTargetObj.layers[0].layers[i]); 
  65.         } 
  66.         else 
  67.         { 
  68.             if(pRemoveInvisible) 
  69.             { 
  70.                 if(pTargetObj.layers[0].layers[i].visible ==false
  71.                 { 
  72.                     arrToRemove.push(pTargetObj.layers[0].layers[i]); 
  73.                 } 
  74.             } 
  75.         } 
  76.     } 
  77.     for(var i = 0; i < arrToRemove.length; i++) 
  78.     { 
  79.         arrToRemove[i].remove(); 
  80.     } 
  81. function CloseAndSaveAsPNG(pDoc) 
  82.     var pngSaveOptions = new PNGSaveOptions(); 
  83.     pDoc.trim(TrimType.TRANSPARENT); 
  84.     if(atlasName == ""
  85.         pDoc.saveAs(new File(targetImageFolders+pDoc.name+'.png'), pngSaveOptions,true, Extension.LOWERCASE); 
  86.     pDoc.close(SaveOptions.DONOTSAVECHANGES); 
  87.  
  88. function ExportSpriteData(pTargetObj, pPath) 
  89.     var tParentName = GetParentName (pPath); 
  90.     var tObjName = pTargetObj.name;     
  91.     var tLayerID = getActiveLayerID(); 
  92.     if(exportType == "WIDGETS")     
  93.     { 
  94.         atlasName = ""
  95.         var tmpString=pTargetObj.name; 
  96.         var a = tmpString.indexOf(")"); 
  97.         var b= tmpString.indexOf("("); 
  98.         if(a!==-1&&b!==-1) 
  99.         { 
  100.             tSourceName = tmpString.substring(b+1,a); 
  101.             var temp = tSourceName.split ("_"); 
  102.             atlasName = temp[0]; 
  103.             //str += "<layer type='SPRITE'> \n"+tSourceName+"  atalsName:"+temp[0]+"  "+atlasName; 
  104.             tSourceName = tSourceName.substring(atlasName.length+1); 
  105.             tObjName=tObjName.substring(0,b); 
  106.         } 
  107.         else if(tParentName==""
  108.         { 
  109.             tSourceName = tObjName; 
  110.         } 
  111.         else if(widgetType=="Sliced"||widgetType=="Tiled"||widgetType=="Filled"
  112.         { 
  113.             tSourceName = tParentName+"_"+tObjName.substring(4); 
  114.         }     
  115.          else  
  116.          { 
  117.             tSourceName = tParentName+"_"+tObjName; 
  118.         }  
  119.     } 
  120.     var newDoc = newDocFromLayer(baseDoc, tSourceName, pTargetObj); 
  121.     layerData = cLayer(newDoc, newDoc.layers[0]); 
  122.      
  123.     if(exportType == "WIDGETS"
  124.         RemoveLayerSetsFromObject(newDoc, true
  125.      
  126.     layerReadCount++; 
  127.     str += "<layer type='SPRITE'> \n"
  128.     if(widgetType=="Sliced"||widgetType=="Tiled"||widgetType=="Filled"
  129.         str += "        <name>"+tObjName.substring(4)+"</name>\n"
  130.     else  
  131.         str += "        <name>"+tObjName+"</name>\n"
  132.     str += "        <type>"+widgetType+"</name>\n"
  133.     str += "        <path>"+ pPath +"</path>\n"
  134.     str += "        <posX>"+ layerData.middleCenterX +"</posX>\n"
  135.     str += "        <posY>"+ layerData.middleCenterY +"</posY>\n"
  136.     str += "        <width>"+ layerData.layerWidth +"</width>\n"
  137.     str += "        <height>"+ layerData.layerHeight +"</height>\n"
  138.     str += "        <atlas>"+atlasName+"</atlas>\n"
  139.     str += "        <source>"+ tSourceName+"</source>\n"
  140.     str += "        <layerID>"+ tLayerID +"</layerID>\n" 
  141.     str += "</layer> \n"
  142.    // if(atlasName=="") 
  143.         CloseAndSaveAsPNG(newDoc); 
  144.  
  145. function ExportAnchor(pDoc, pTargetObj, pPath) 
  146.     var tLayerID = getActiveLayerID(); 
  147.     layerData = cLayer(pDoc, pTargetObj); 
  148.      
  149.     layerReadCount 
  150.      
  151.      str += "<layer type='"+widgetType+"'> \n"
  152.     str += "        <name>"+pTargetObj.name+"</name>\n"
  153.     str += "        <path>"+ pPath +"</path>\n"
  154.     str += "        <posX>"+ layerData.middleCenterX +"</posX>\n"
  155.     str += "        <posY>"+ layerData.middleCenterY +"</posY>\n"
  156.     str += "        <layerID>"+ tLayerID +"</layerID>\n"
  157.     str += "</layer> \n"
  158.  
  159.  
  160. function ExportTextLabel(pTargetObj, pPath) 
  161.     var tLayerID = getActiveLayerID(); 
  162.     $.writeln ("Layer ID: "+ tLayerID);     
  163.      
  164.     var newDoc  = newDocFromLayer(baseDoc, pTargetObj.name, pTargetObj); 
  165.     layerData   = cLayer(newDoc, newDoc.layers[0]); 
  166.     //var tPrefix = newDoc.layers[0].name.substring(0,4) ; 
  167.     var tLayer  = newDoc.layers[0].layers[0]; 
  168.  
  169.     if( tLayer.kind == LayerKind.TEXT ) 
  170.     { 
  171.         layerReadCount+=1; 
  172.         var tText   = tLayer.textItem; 
  173.         var tSize   = new Number( tText.size ); 
  174.         tSize       = Math.round (tSize); 
  175.         var tName   = newDoc.layers[0].name; 
  176.         tName       = tName.substring(4); 
  177.         //str += "<layer type='TEXT_LABEL'> \n"+ tText.font; 
  178.         //var tType = tPrefix == "txt_" ? "TEXT_LABEL" : "INPUT_TEXT"; 
  179.  
  180.         str += "<layer type='TEXT_LABEL'> \n"
  181.         str += "        <name>"+pTargetObj.name.substring(4)+"</name>\n"
  182.         str += "        <path>"+ pPath +"</path>\n"
  183.         str += "        <posX>"+ layerData.middleCenterX +"</posX>\n"
  184.         str += "        <posY>"+ layerData.middleCenterY +"</posY>\n"
  185.         str += "        <width>"+ layerData.layerWidth +"</width>\n"
  186.         str += "        <height>"+ layerData.layerHeight +"</height>\n"
  187.         str += "        <fontName>"+ tText.font +"</fontName>\n"
  188.         str += "        <fontSize>"+ tSize +"</fontSize>\n"
  189.         str += "        <fontColor>"+ tText.color.rgb.red +";"+ tText.color.rgb.green +";"+ tText.color.rgb.blue +"</fontColor>\n"
  190.         str += "        <contents>"+ tText.contents+"</contents>\n"
  191.         str += "        <layerID>"+ tLayerID +"</layerID>\n"
  192.         str += "</layer>\n"
  193.     } 
  194.     newDoc.close(SaveOptions.DONOTSAVECHANGES); 
  195.  
  196.  
  197.  
  198.  
  199.  
  200. function GetParentName(pPath) 
  201.     if(pPath.substring(0, pPath.length-1) =="/"
  202.         pPath = pPath.substring(0, pPath.length-1); 
  203.          
  204.     var strPathLeng = pPath.length; 
  205.     var lastIndex = pPath.lastIndexOf ("/"); 
  206.     if(lastIndex > -1) 
  207.     { 
  208.         pPath = pPath.substring ((lastIndex+1), strPathLeng); 
  209.     } 
  210.  
  211.     return pPath; 
  212.  
  213. function ExportSprite(pDoc, pTargetObj) 
  214.     var newDoc = newDocFromLayer(pDoc, pTargetObj.name, pTargetObj); 
  215.     layerData = cLayer(newDoc, newDoc.layers[0]); 
  216.      
  217.     CloseAndSaveAsPNG(newDoc); 
  218.  
  219. function ReadGroup(pTargetGroup, pPath) 
  220.     
  221.     if(exportType == "WIDGETS"
  222.     { 
  223.         totalLayerCount += pTargetGroup.layerSets.length; 
  224.         for(var i = (pTargetGroup.layerSets.length-1); i >= 0 ; i--) 
  225.         { 
  226.              var tmpObj = pTargetGroup.layerSets[i]; 
  227.             baseDoc.activeLayer = tmpObj; 
  228.              
  229.             progressWindow.bar.value = (layerReadCount/totalLayerCount)*100; 
  230.             //progressWindow.update();   
  231.             
  232.             if(tmpObj.artLayers.length > 0)  //基本组件 txt  sprite 
  233.             { 
  234.                 if(tmpObj.name.substring(0, 4) =="txt_"
  235.                 { 
  236.                     ExportTextLabel(tmpObj, pPath); 
  237.                 } 
  238.                 else if(tmpObj.name.substring(0, 4) =="slc_"
  239.                 { 
  240.                     widgetType="Sliced"
  241.                     ExportSpriteData(tmpObj, pPath); 
  242.                 } 
  243.                 else if(tmpObj.name.substring(0, 4) =="til_"
  244.                 { 
  245.                     widgetType="Tiled"
  246.                     ExportSpriteData(tmpObj, pPath); 
  247.                 } 
  248.                 else if(tmpObj.name.substring(0, 4) =="fil_"
  249.                 { 
  250.                     widgetType="Filled"
  251.                     ExportSpriteData(tmpObj, pPath); 
  252.                 } 
  253.                 else 
  254.                 { 
  255.                     widgetType="Simple"
  256.                     ExportSpriteData(tmpObj, pPath); 
  257.                 } 
  258.             } 
  259.             else 
  260.             { 
  261.                 if(tmpObj.name.substring(0, 4) =="btn_"
  262.                 { 
  263.                     widgetType="BUTTON"
  264.                 } 
  265.                 else if(tmpObj.name.substring(0, 4) =="ckb_"
  266.                 { 
  267.                     widgetType="CHECKBOX"
  268.                 } 
  269.                 else if(tmpObj.name.substring(0, 4) =="sld_"
  270.                 { 
  271.                     widgetType="SLIDER"
  272.                 } 
  273.                 else if(tmpObj.name.substring(0, 4) =="psb_"
  274.                 { 
  275.                     widgetType="PROGRESSBAR"
  276.                 } 
  277.                 else if(tmpObj.name.substring(0, 4) =="inp_"
  278.                 { 
  279.                      widgetType="INPUT"
  280.                 } 
  281.                 else 
  282.                 { 
  283.                     widgetType="ANCHOR"
  284.                      
  285.                 } 
  286.                 ExportAnchor(pTargetGroup, tmpObj, pPath); 
  287.             } 
  288.             if(tmpObj.layerSets.length > 0) 
  289.             { 
  290.                 var target=""
  291.                 if(pPath == ""
  292.                 { 
  293.                     target = tmpObj.name; 
  294.                  } 
  295.                 else 
  296.                 { 
  297.                     target= pPath+"/"+tmpObj.name; 
  298.                 } 
  299.                 ReadGroup(tmpObj, target); 
  300.             } 
  301.         } 
  302.          
  303.     } 
  304.     else if(exportType =="SPRITES"
  305.     { 
  306.         totalLayerCount += pTargetGroup.layerSets.length; 
  307.          for(var i = (pTargetGroup.layerSets.length-1); i >= 0 ; i--) 
  308.         { 
  309.              
  310.             progressWindow.bar.value = (layerReadCount/totalLayerCount)*100; 
  311.             //progressWindow.update();  
  312.              
  313.             var tmpObj = pTargetGroup.layerSets[i]; 
  314.             baseDoc.activeLayer = tmpObj; 
  315.              
  316.             var target = ""
  317.             if(pPath == ""
  318.             { 
  319.                 target = tmpObj.name; 
  320.              } 
  321.             else 
  322.             { 
  323.                 target= pPath+"/"+tmpObj.name; 
  324.             } 
  325.             //ExportAnchor(pTargetGroup, pTargetGroup.layerSets[i], pPath); 
  326.             ReadGroup(tmpObj, target); 
  327.         } 
  328.         totalLayerCount += pTargetGroup.artLayers.length; 
  329.         for(var i = (pTargetGroup.artLayers.length-1); i >= 0; i--)    
  330.         { 
  331.             var tmpObj = pTargetGroup.artLayers[i]; 
  332.             baseDoc.activeLayer = tmpObj; 
  333.             atlasName = ""
  334.             var tempPath = pPath.substring (0, pPath.lastIndexOf("/")) ;  //调整命名为: 父目录(组)名+ 当前目录(组)名 
  335.             var tGrandParentName = GetParentName (tempPath); 
  336.             var tParentName = GetParentName (pPath); 
  337.             var tObjName = tmpObj.name;     
  338.              
  339.             if(tGrandParentName==""
  340.             { 
  341.                     tSourceName = tParentName; 
  342.             } 
  343.             else  
  344.             { 
  345.                 tSourceName = tGrandParentName+"_"+tParentName; 
  346.             }  
  347.             var newDoc = newDocFromLayer(baseDoc, tSourceName, tmpObj); 
  348.             CloseAndSaveAsPNG(newDoc); 
  349.             layerReadCount++; 
  350.             //ExportSprite(baseDoc, pPath); 
  351.         } 
  352.  
  353.     } 
  354.  
  355. windowResource = "dialog {  \ 
  356.     orientation: 'column', \ 
  357.     alignChildren: ['fill', 'top'],  \ 
  358.     preferredSize:[140, 60], \ 
  359.     text: 'FastGUI Photoshop Exporter',  \ 
  360.     margins:15, \ 
  361.     \ 
  362.     bottomGroup: Group{ \ 
  363.         cancelButton: Button { text: 'Cancel', properties:{name:'cancel'}, size: [120,24], alignment:['right','center'] }, \ 
  364.         exportAllAsSprite: Button { text: 'Export Sprites', properties:{name:'exportsprites'}, size: [120,24], alignment:['right','center'] }, \ 
  365.         exportWidgets: Button { text: 'Export Widgets', properties:{name:'exportwidgets'}, size: [120,24], alignment:['right','center'] }, \ 
  366.     }\ 
  367. }" 
  368. win = new Window(windowResource); 
  369.  
  370. win.bottomGroup.cancelButton.onClick = function()  
  371.   return win.close(); 
  372. }; 
  373. win.bottomGroup.exportAllAsSprite.onClick = function()  
  374.     exportType = "SPRITES"
  375.     StartFolder(); 
  376. }; 
  377. win.bottomGroup.exportWidgets.onClick = function() { 
  378.     exportType = "WIDGETS"
  379.     StartFolder(); 
  380. }; 
  381.  
  382. function StartFolder() 
  383.     win.close(); 
  384.     var theFolder    = Folder.selectDialog ("Select the target folder:"); 
  385.  
  386.     if (theFolder)  
  387.     { 
  388.         progressWindow = createProgressWindow("Please wait...", undefined, 0, 100);  
  389.         progressWindow.show(); 
  390.         cleanDocumentName = app.activeDocument.name.match(/([^\.]+)/)[1]; 
  391.          
  392.         var TargetFolder = new Folder(theFolder+'/'+cleanDocumentName+'/'); 
  393.         TargetFolder.create();      
  394.          
  395.         var folder = new Folder(TargetFolder+"/Images"); 
  396.         folder.create(); 
  397.         targetImageFolders = TargetFolder+"/Images/"
  398.          
  399.         ReadGroup(baseDoc, ""); 
  400.         // Use this to export XML file to same directory where PSD file is located 
  401.         var mySourceFilePath = TargetFolder +"/"
  402.         // create a reference to a file for output 
  403.         var csvFile = new File(mySourceFilePath.toString().match(/([^\.]+)/)[1] +"FastGUIData.xml"); 
  404.         // open the file, write the data, then close the file 
  405.         csvFile.encoding = "utf-8"
  406.         csvFile.open('w'); 
  407.         csvFile.writeln(str + "</psd>"); 
  408.         csvFile.close(); 
  409.         preferences.rulerUnits = originalRulerUnits; 
  410.         // Confirm that operation has completed 
  411.         alert("Operation Complete!" +"\n" +"Layer coordinates were successfully exported to:" +"\n" +"\n" + mySourceFilePath.toString().match(/([^\.]+)/)[1] + app.activeDocument.name.match(/([^\.]+)/)[1] +".xml"); 
  412.     } 
  413. createProgressWindow = function(title, message, min, max) { 
  414.   var win; 
  415.   win = new Window('palette', title); 
  416.   win.bar = win.add('progressbar', undefined, min, max); 
  417.   win.bar.preferredSize = [300, 20]; 
  418.   win.stProgress = win.add("statictext"); 
  419.   win.stProgress.preferredSize.width = 200; 
  420.   return win; 
  421. }; 
  422.  
  423.  
  424. win.show(); 

        在Unity中使用的脚本(比原来上了很多,XML解析的三个脚本就不贴了)

FastGUIOutPut.cs:

C#代码 复制代码 收藏代码
  1. using UnityEngine; 
  2. using System.Collections; 
  3. using System.Collections.Generic; 
  4.  
  5.  
  6. public class FastGUIOutput : MonoBehaviour  
  7.     public Dictionary<int,int> references =new Dictionary<int,int>(); 

FastGUI.cs:

C#代码 复制代码 收藏代码
  1. using UnityEditor; 
  2. using UnityEngine; 
  3. using System.Collections.Generic; 
  4. using System.IO; 
  5. using System.Text; 
  6.  
  7. [ExecuteInEditMode] 
  8. public class FastGUI : EditorWindow 
  9.     //ͼ����depth���� 
  10.     private constint COMMON_DEPTH = 0; 
  11.     private constint FONT_DEPTH = 2000; 
  12.     private constint MIDDLE_DEPTH = 1000; 
  13.     public static Dictionary<string, Transform> pathTransDic =new Dictionary<string, Transform>(); 
  14.     [MenuItem("Monster Juice/Fast GUI")] 
  15.     static void ShowWindow ()  
  16.     { 
  17.         EditorWindow.GetWindow<FastGUI>(); 
  18.     } 
  19.     private string applicationDataPath  = Application.dataPath.Substring(0, Application.dataPath.IndexOf("Assets")); 
  20.     public staticstring assetFolderToBeParseed; 
  21.     private string applicationFolderToBeParseed; 
  22.     private string lastFolderChecked; 
  23.      
  24.     static publicint targetWidht; 
  25.     static publicint targetHeight; 
  26.      
  27.     public static Object objectToBeLoaded; 
  28.     public staticstring sourceFolder =""
  29.  
  30.     private bool haveXML; 
  31.     private bool haveRoot; 
  32.     private bool haveTargetPanel; 
  33.     private bool isOnSize; 
  34.      
  35.     private Color defaultColor; 
  36.      
  37.     private UIPanel targetRootPanel; 
  38.     private UIPanel lastCheckedtargetRootPanel; 
  39.     public static UIRoot actualRoot; 
  40.     private float itemActual = 1; 
  41.     private float totalItens = 1; 
  42.      
  43.     private UIAtlas targetAtlas; 
  44.  
  45.     private Dictionary<XMLNode, Transform> transformDic =new Dictionary<XMLNode, Transform>(); 
  46.      
  47.      
  48.     private XMLNodeList xmlObjects; 
  49.      
  50.      
  51.     private string debugerText;  
  52.      
  53.     private string currentObjectName =""
  54.      
  55.     public static FastGUIOutput actualFastGUIOutput =null
  56.     StreamReader reader; 
  57.      
  58.     void OnSelectionChange ()  
  59.     {  
  60.         Repaint(); 
  61.     } 
  62.     void OnEnable() 
  63.     { 
  64.         defaultColor = GUI.backgroundColor; 
  65.     } 
  66.     void OnGUI ()  
  67.     { 
  68.         GUILayout.Label("FastGUI", EditorStyles.boldLabel); 
  69.         FastGUIEditorTools.DrawSeparator(); 
  70.         GUILayout.BeginHorizontal(); 
  71.         GUILayout.Label("FastGUI Folder:"); 
  72.         objectToBeLoaded                = EditorGUILayout.ObjectField(objectToBeLoaded,typeof(Object),false); 
  73.         if(objectToBeLoaded != null
  74.         { 
  75.             if(AssetDatabase.GetAssetPath(objectToBeLoaded).IndexOf(".xml") > -1) 
  76.             { 
  77.                 objectToBeLoaded = null
  78.             } 
  79.         } 
  80.         assetFolderToBeParseed          = AssetDatabase.GetAssetPath(objectToBeLoaded); 
  81.         applicationFolderToBeParseed    = applicationDataPath+assetFolderToBeParseed; 
  82.         GUILayout.EndHorizontal(); 
  83.          
  84.         GUILayout.BeginVertical(); 
  85.         if(lastFolderChecked != applicationFolderToBeParseed) 
  86.         { 
  87.             lastFolderChecked   = applicationFolderToBeParseed; 
  88.             haveXML             = HasXML(); 
  89.         } 
  90.         GUILayout.EndVertical(); 
  91.          
  92.         GUILayout.BeginHorizontal(); 
  93.         GUILayout.Label("Parent Panel:"); 
  94.         targetRootPanel                 = EditorGUILayout.ObjectField(targetRootPanel,typeof(UIPanel),true)as UIPanel; 
  95.         GUILayout.EndHorizontal(); 
  96.         if(targetRootPanel!=null
  97.         { 
  98.             haveTargetPanel = true
  99.         } 
  100.         else 
  101.         { 
  102.             haveTargetPanel = false
  103.         } 
  104.         if(haveTargetPanel) 
  105.         { 
  106.             if(lastCheckedtargetRootPanel != targetRootPanel) 
  107.             { 
  108.                 lastCheckedtargetRootPanel = targetRootPanel; 
  109.                 actualRoot = GetUIRoot(targetRootPanel.gameObject); 
  110.                  
  111.                  
  112.                 if(actualRoot == null
  113.                 { 
  114.                     haveRoot = false
  115.                 } 
  116.                 else 
  117.                 { 
  118.                     haveRoot = true
  119.                 } 
  120.             } 
  121.         } 
  122.         GUILayout.BeginHorizontal(); 
  123.         GUILayout.Label("Target Atlas:"); 
  124.         targetAtlas                 = EditorGUILayout.ObjectField(targetAtlas,typeof(UIAtlas),false)as UIAtlas; 
  125.         GUILayout.EndHorizontal(); 
  126.          
  127.          
  128.         GUILayout.Space(10); 
  129.         if(actualRoot != null && objectToBeLoaded!=null
  130.         { 
  131.             if(actualRoot.manualHeight != targetHeight || actualRoot.minimumHeight < targetHeight) 
  132.             { 
  133.                 isOnSize = false
  134.             } 
  135.             else 
  136.             { 
  137.                 isOnSize = true
  138.             } 
  139.             if(!isOnSize) 
  140.             { 
  141.                 GUI.backgroundColor = new Color(171f/255, 26f/255, 37f/255,1); 
  142.                 if(GUILayout.Button("Update the UIRoot")) 
  143.                 { 
  144.                     UpdateUIRootSize(); 
  145.                 } 
  146.                 GUI.backgroundColor = defaultColor; 
  147.             } 
  148.         } 
  149.         if(haveXML && haveRoot && isOnSize && haveTargetPanel) 
  150.         { 
  151.             GUI.backgroundColor = new Color(17f/255, 146f/255, 156f/255,1); 
  152.             if(GUILayout.Button("FastGUI it!")) 
  153.             { 
  154.                 ParseeTargetFolder(); 
  155.             } 
  156.             GUI.backgroundColor = defaultColor; 
  157.              
  158.         } 
  159.         else 
  160.         { 
  161.             UpdateDebugger(); 
  162.         } 
  163.     } 
  164.     private void ResetFields() 
  165.     { 
  166.         objectToBeLoaded    = null
  167.         targetRootPanel     = null
  168.         targetAtlas         = null
  169.          
  170.     } 
  171.     private void ResetProperties() 
  172.     { 
  173.         reader                      = null
  174.         FastGUI.actualFastGUIOutput = null
  175.         NGUISettings.atlas          = null
  176.         NGUISettings.fontData       = null
  177.         NGUISettings.fontTexture    = null
  178.         targetAtlas                 = null
  179.         FastGUI.sourceFolder        = ""
  180.     } 
  181.     private void ParseeTargetFolder() 
  182.     { 
  183.         actualRoot = GetUIRoot(targetRootPanel.gameObject); 
  184.         pathTransDic = new Dictionary<string, Transform>(); 
  185.         pathTransDic.Add("", targetRootPanel.transform); 
  186.         if(targetAtlas != null
  187.             NGUISettings.atlas = targetAtlas; 
  188.          
  189.         if(xmlObjects == null
  190.             ReadXML(); 
  191.          
  192.         if(actualFastGUIOutput == null
  193.             CreateOutputPrefab(); 
  194.          
  195.         itemActual = 0; 
  196.         string      tType       = null
  197.         Transform   tLastAnchor = null
  198.         string      tLastAnchorPath =null;  
  199.              
  200.         foreach(XMLNode tNode in xmlObjects) 
  201.         { 
  202.             EditorUtility.DisplayProgressBar( 
  203.                 "FastGUI Progress"
  204.                 ("Object: "+currentObjectName+"("+itemActual+"/"+totalItens+")"), 
  205.                 itemActual/totalItens); 
  206.              
  207.             tType       = tNode["@type"].ToString(); 
  208.             itemActual  += 1.0f; 
  209.              
  210.             if(tNode["@type"].ToString() =="ANCHOR"
  211.             { 
  212.                 tLastAnchor     = FastGUIPostProcessing.CreateAnchor( tNode, targetRootPanel, tLastAnchor, tLastAnchorPath);; 
  213.             } 
  214.             else if(tNode["@type"].ToString() =="BUTTON"
  215.             { 
  216.                 tLastAnchor = FastGUIPostProcessing.CreateWidgetContainer<Button>(tNode, targetRootPanel, tLastAnchor, tLastAnchorPath); 
  217.             } 
  218.             else if(tNode["@type"].ToString() =="SPRITE"
  219.             { 
  220.                 tLastAnchor = FastGUIPostProcessing.CreateSprite(tNode, targetRootPanel, tLastAnchor, tLastAnchorPath); 
  221.             } 
  222.             else if( tType =="TEXT_LABEL"
  223.             { 
  224.                 tLastAnchor = FastGUIPostProcessing.CreateTextLabel(tNode, targetRootPanel, tLastAnchor, tLastAnchorPath); 
  225.  
  226.             } 
  227.             else if( tType =="INPUT"
  228.             { 
  229.                 tLastAnchor = FastGUIPostProcessing.CreateWidgetContainer<UIInput>(tNode, targetRootPanel, tLastAnchor, tLastAnchorPath); 
  230.  
  231.             } 
  232.             else if( tType =="CHECKBOX"
  233.             { 
  234.                 tLastAnchor = FastGUIPostProcessing.CreateWidgetContainer<UIToggle>(tNode, targetRootPanel, tLastAnchor, tLastAnchorPath); 
  235.  
  236.             } 
  237.             else if( tType =="SLIDER"
  238.             { 
  239.                 tLastAnchor = FastGUIPostProcessing.CreateWidgetContainer<UISlider>(tNode, targetRootPanel, tLastAnchor, tLastAnchorPath); 
  240.             } 
  241.             else if( tType =="PROGRESSBAR"
  242.             { 
  243.                 tLastAnchor = FastGUIPostProcessing.CreateWidgetContainer<UIScrollBar>(tNode, targetRootPanel, tLastAnchor, tLastAnchorPath); 
  244.             } 
  245.             tLastAnchorPath = tNode.GetNode("path>0")["_text"].ToString() +"/" + tNode.GetNode("name>0")["_text"].ToString(); 
  246.             transformDic.Add(tNode, tLastAnchor); 
  247.             currentObjectName = tNode.GetNode("name>0")["_text"].ToString(); 
  248.              
  249.              
  250.              
  251.         } 
  252.         ProcessLast(); 
  253.         EditorUtility.ClearProgressBar(); 
  254.         SetToPixelPerfect(); 
  255.         ResetProperties(); 
  256.         ResetFields(); 
  257.     } 
  258.      
  259.     private void UpdateUIRootSize() 
  260.     { 
  261.         if (actualRoot.transform != null
  262.         { 
  263.             float calcActiveHeight = targetHeight; 
  264.              
  265.             actualRoot.minimumHeight    = targetHeight; 
  266.             actualRoot.manualHeight     = targetHeight; 
  267.             actualRoot.manualHeight     = targetHeight; 
  268.              
  269.             actualRoot.scalingStyle     = UIRoot.Scaling.FixedSize; 
  270.              
  271.  
  272.             if (calcActiveHeight > 0f ) 
  273.             { 
  274.                 float size = 2f / calcActiveHeight; 
  275.                  
  276.                 Vector3 ls = actualRoot.transform.localScale; 
  277.      
  278.                 if (!(Mathf.Abs(ls.x - size) <=float.Epsilon) || 
  279.                     !(Mathf.Abs(ls.y - size) <= float.Epsilon) || 
  280.                     !(Mathf.Abs(ls.z - size) <= float.Epsilon)) 
  281.                 { 
  282.                     actualRoot.transform.localScale = new Vector3(size, size, size); 
  283.                 } 
  284.             } 
  285.         }        
  286.     } 
  287.     public void SetToPixelPerfect() 
  288.     { 
  289.         actualRoot.scalingStyle     = UIRoot.Scaling.FixedSizeOnMobiles; 
  290.     } 
  291.     private UIRoot GetUIRoot(GameObject tTarget) 
  292.     { 
  293.         if(tTarget.transform.parent!=null
  294.         { 
  295.             if(tTarget.transform.parent.gameObject.GetComponent<UIRoot>()) 
  296.             { 
  297.                 return tTarget.transform.parent.gameObject.GetComponent<UIRoot>(); 
  298.             } 
  299.             else 
  300.             { 
  301.                 return GetUIRoot(tTarget.transform.parent.gameObject); 
  302.             } 
  303.         } 
  304.         return null
  305.     } 
  306.     private void UpdateDebugger() 
  307.     { 
  308.         FastGUIEditorTools.DrawSeparator(); 
  309.  
  310.         GUILayout.Label("Output", EditorStyles.boldLabel); 
  311.          
  312.          
  313.         if(!haveXML) 
  314.             if(objectToBeLoaded!=null
  315.                 GUILayout.Label("FastGUIData.xml can't be found inside the folder: "+objectToBeLoaded.name+"\n", EditorStyles.wordWrappedMiniLabel); 
  316.          
  317.         //if(!haveImagesFolder) 
  318.             //if(objectToBeLoaded!=null) 
  319.                 //GUILayout.Label("Image Foulder can't be found inside the folder: "+objectToBeLoaded.name+"\n", EditorStyles.wordWrappedMiniLabel); 
  320.          
  321.         if(!haveRoot)        
  322.             if(targetRootPanel!=null
  323.                 GUILayout.Label("Can't find the UIRoot of panel: "+targetRootPanel, EditorStyles.wordWrappedMiniLabel); 
  324.          
  325.         if(!haveTargetPanel)         
  326.             GUILayout.Label("You must select one target Panel", EditorStyles.wordWrappedMiniLabel); 
  327.          
  328.          
  329.         if(objectToBeLoaded == null
  330.             GUILayout.Label("You must select one valid FastGUI export folder", EditorStyles.wordWrappedMiniLabel);   
  331.          
  332.          
  333.         if(targetAtlas == null
  334.             GUILayout.Label("You must select one Atlas", EditorStyles.wordWrappedMiniLabel);     
  335.          
  336.         FastGUIEditorTools.DrawSeparator(); 
  337.     } 
  338.      
  339.     private void ReadXML() 
  340.     {    
  341.         string fileData = ""
  342.         reader = new StreamReader(assetFolderToBeParseed+"/FastGUIData.xml",Encoding.UTF8); 
  343.         fileData = reader.ReadToEnd(); 
  344.         //Debug.Log(fileData); 
  345.         reader.Close(); 
  346.          
  347.         XMLParser parser    = new XMLParser(); 
  348.          
  349.         XMLNode node        = parser.Parse( fileData ); 
  350.  
  351.         xmlObjects          = node.GetNodeList("psd>0>layer"); 
  352.          
  353.         targetWidht = int.Parse(node.GetNode("psd>0")["@width"].ToString()); 
  354.         targetHeight = int.Parse(node.GetNode("psd>0")["@height"].ToString()); 
  355.          
  356.         totalItens = (float)xmlObjects.Count;    
  357.     } 
  358.     private bool HasXML() 
  359.     { 
  360.         string[] files = Directory.GetFiles(applicationFolderToBeParseed,"FastGUIData.xml", SearchOption.TopDirectoryOnly); 
  361.          
  362.         if(files.Length > 0) 
  363.         { 
  364.             ReadXML(); 
  365.             return true
  366.         } 
  367.         return false
  368.     } 
  369.     private void CreateOutputPrefab() 
  370.     { 
  371.         if(!Directory.Exists(applicationFolderToBeParseed+"/Output/")) 
  372.             Directory.CreateDirectory(applicationFolderToBeParseed+"/Output/"); 
  373.         AssetDatabase.Refresh(); 
  374.          
  375.         Object prefabOutput = PrefabUtility.CreateEmptyPrefab(FastGUI.assetFolderToBeParseed+"/Output/FastGUIOutput.prefab"); 
  376.          
  377.          
  378.         // Create a new game object for the atlas 
  379.         GameObject go = new GameObject("FastGUIOutput"); 
  380.         go.AddComponent<FastGUIOutput>(); 
  381.  
  382.         // Update the prefab 
  383.         PrefabUtility.ReplacePrefab(go, prefabOutput); 
  384.          
  385.         DestroyImmediate(go); 
  386.         AssetDatabase.SaveAssets(); 
  387.         AssetDatabase.Refresh(); 
  388.         Debug.Log(FastGUI.assetFolderToBeParseed + " "); 
  389.         actualFastGUIOutput = AssetDatabase.LoadAssetAtPath(FastGUI.assetFolderToBeParseed+"/Output/FastGUIOutput.prefab",typeof(FastGUIOutput))as FastGUIOutput; 
  390.         Debug.Log("...................op:" + actualFastGUIOutput.name); 
  391.         actualFastGUIOutput.references = new Dictionary<int,int>(); 
  392.     } 
  393.      
  394.     private void ProcessLast() 
  395.     { 
  396.          
  397.         string member = "", findMem =""
  398.         List<UIWidget> commonList = new List<UIWidget>(); 
  399.         List<UIWidget> middleList = new List<UIWidget>(); 
  400.         List<UIWidget> fontList = new List<UIWidget>(); 
  401.         foreach ( KeyValuePair<XMLNode,Transform> keyValuein transformDic) 
  402.         { 
  403.             XMLNode tNode = keyValue.Key; 
  404.             string tType = tNode["@type"].ToString(); 
  405.             string  tName       = tNode.GetNode ("name>0")["_text"].ToString(); 
  406.             string  tParentName = tNode.GetNode ("path>0")["_text"].ToString(); 
  407.             string tPath = ""
  408.             if (string.IsNullOrEmpty(tParentName)) 
  409.                 tPath = tName; 
  410.             else 
  411.                 tPath = tParentName + "/" + tName; 
  412.  
  413.             itemActual += 1.0f; 
  414.             Transform trans = keyValue.Value; 
  415.             //ָ����Ա���� 
  416.             if(tType == "SPRITE"
  417.             { 
  418.                 if(tNode.GetNode("atlas>0")["_text"].ToString() =="Common"
  419.                 { 
  420.                     Debug.Log(trans.gameObject.name); 
  421.                     commonList.Add(trans.gameObject.GetComponent<UISprite>()); 
  422.                 } 
  423.                 else  
  424.                     middleList.Add(trans.gameObject.GetComponent<UISprite>());} 
  425.             else if(tType =="TEXT_LABEL"
  426.             { 
  427.                 fontList.Add(trans.gameObject.GetComponent<UILabel>());} 
  428.             else if (tType =="BUTTON"
  429.             { 
  430.                 Button button = trans.GetComponent<Button>(); 
  431.                 NGUITools.AddWidgetCollider(trans.gameObject); 
  432.                 member += "    private Button " + tName +";\n"
  433.                 findMem += "        " + tName +" = FindInChild<Button>(\"" + tPath +"\");\n"
  434.                  
  435.             } 
  436.             else if (tType =="INPUT"
  437.             { 
  438.                 UIInput input = trans.GetComponent<UIInput>(); 
  439.                 if(trans.FindChild("label") !=null
  440.                 { 
  441.                     UILabel label = trans.FindChild("label").GetComponent<UILabel>(); 
  442.  
  443.                     input.label = label; 
  444.                 } 
  445.                 NGUITools.AddWidgetCollider(trans.gameObject); 
  446.                 member += "    private UIInput " + tName +";\n"
  447.                 findMem += "        " + tName +" = FindInChild<UIInput>(\"" + tPath +"\");\n"
  448.             } 
  449.             else if (tType =="CHECKBOX"
  450.             { 
  451.                 UIToggle toggle = trans.GetComponent<UIToggle>(); 
  452.                 if(trans.FindChild("checkmark")!=null
  453.                     toggle.activeSprite = trans.FindChild("checkmark").GetComponent<UISprite>(); 
  454.                 toggle.value = true
  455.                 NGUITools.AddWidgetCollider(trans.gameObject); 
  456.                 member += "    private UIToggle " + tName +";\n"
  457.                 findMem += "        " + tName +" = FindInChild<UIToggle>(\"" + tPath +"\");\n"
  458.             } 
  459.             else if (tType =="SLIDER"
  460.             { 
  461.                 UISlider slider = trans.GetComponent<UISlider>(); 
  462.                 if (trans.FindChild("foreground") !=null && trans.FindChild("background") !=null
  463.                 { 
  464.                     UISprite foreground = trans.FindChild("foreground").GetComponent<UISprite>(); 
  465.                     UISprite background = trans.FindChild("background").GetComponent<UISprite>(); 
  466.  
  467.                     Transform thumb = trans.FindChild("thumb"); 
  468.  
  469.                     background.pivot = UIWidget.Pivot.Left; 
  470.                     trans.position = background.transform.position; 
  471.                     Vector3 post = trans.position; 
  472.  
  473.                     foreground.pivot = UIWidget.Pivot.Left; 
  474.                     foreground.transform.position = post; 
  475.                     //background.transform.localPosition = Vector3.zero; 
  476.                     background.pivot = UIWidget.Pivot.Left; 
  477.                     background.transform.position = post; 
  478.                     if (thumb != null
  479.                         slider.thumb = thumb; 
  480.                     slider.foregroundWidget = foreground; 
  481.                 } 
  482.                 //slider.thumb = thumb; 
  483.                 NGUITools.AddWidgetCollider(trans.gameObject); 
  484.                 member += "    private UISlider " + tName +";\n"
  485.                 findMem += "        " + tName +" = FindInChild<UISlider>(\"" + tPath +"\");\n"
  486.             } 
  487.             else if (tType =="PROGRESSBAR"
  488.             { 
  489.                 UIScrollBar scrollBar = trans.GetComponent<UIScrollBar>(); 
  490.                 if (trans.FindChild("foreground") !=null && trans.FindChild("background") !=null
  491.                 { 
  492.                     UISprite foreground = trans.FindChild("foreground").GetComponent<UISprite>(); 
  493.                     UISprite background = trans.FindChild("background").GetComponent<UISprite>(); 
  494.  
  495.                     background.pivot = UIWidget.Pivot.Left; 
  496.                     trans.position = background.transform.position; 
  497.                     Vector3 post = trans.position; 
  498.  
  499.                     foreground.pivot = UIWidget.Pivot.Left; 
  500.                     foreground.transform.position = post; 
  501.                     background.pivot = UIWidget.Pivot.Left; 
  502.                     background.transform.position = post; 
  503.                     scrollBar.foregroundWidget = foreground; 
  504.                     scrollBar.backgroundWidget = background; 
  505.                 } 
  506.                 //scrollBar.transform.localPosition = lPost; 
  507.                 NGUITools.AddWidgetCollider(trans.gameObject); 
  508.                 member += "    private UIScollBar " + tName +";\n"
  509.                 findMem += "        " + tName +" = FindInChild<UIScollBar>(\"" + tPath +"\");\n"
  510.             } 
  511.              
  512.         } 
  513.         if (!Directory.Exists(applicationFolderToBeParseed +"/Output/")) 
  514.             Directory.CreateDirectory(applicationFolderToBeParseed + "/Output/"); 
  515.         //String fileName=objectToBeLoaded.find 
  516.         FileStream fs = new FileStream(applicationFolderToBeParseed +"/Output/" + objectToBeLoaded.name +".txt", FileMode.Create); 
  517.         StreamWriter sw = new StreamWriter(fs); 
  518.         sw.Write("using UnityEngine;\n\n"); 
  519.         sw.Write("class " + objectToBeLoaded.name +": BaseView\n{\n"); 
  520.         sw.Write(member); 
  521.         sw.Write("    protected override void Init()\n    {\n"); 
  522.         sw.Write(findMem); 
  523.         sw.Write("    }\n}"); 
  524.         sw.Flush(); 
  525.         sw.Close(); 
  526.         fs.Close(); 
  527.  
  528.         //Debug.Log(commonList.Count+"   ..............."); 
  529.         for (int i = 0, max = commonList.Count; i < max; i++) 
  530.         { 
  531.             UIWidget w = commonList[i]; 
  532.         } 
  533.         //����depth���� 
  534.         commonList.Sort(UIWidget.CompareFunc); 
  535.         middleList.Sort(UIWidget.CompareFunc); 
  536.         fontList.Sort(UIWidget.CompareFunc); 
  537.         for(int i=0,max=commonList.Count;i<max;i++) 
  538.         { 
  539.  
  540.             UIWidget w = commonList[i]; 
  541.             w.depth = COMMON_DEPTH + i; 
  542.  
  543.         } 
  544.         for(int i=0,max=middleList.Count;i<max;i++) 
  545.         { 
  546.             UIWidget w = middleList[i]; 
  547.             w.depth = MIDDLE_DEPTH + i; 
  548.         } 
  549.         for(int i=0,max=fontList.Count;i<max;i++) 
  550.         { 
  551.             UIWidget w = fontList[i]; 
  552.             w.depth = FONT_DEPTH + i; 
  553.         }    
  554.     } 
  555.      

FastGUIPostProcessing.cs:

C#代码 复制代码 收藏代码
  1. using UnityEngine; 
  2. using UnityEditor; 
  3. using System.Collections; 
  4. using System.Collections.Generic; 
  5. using System.IO; 
  6. using System.Xml; 
  7. using System.Text; 
  8.  
  9. public class FastGUIPostProcessing : Editor 
  10.  
  11.     public FastGUI monsterImporter; 
  12.  
  13.     public void SetImporterLink(FastGUI pLink) 
  14.     { 
  15.         monsterImporter = pLink; 
  16.     } 
  17.  
  18.     static public Vector3 AdjustPosition(float pX,float pY) 
  19.     { 
  20.         Vector3 tTargetPos = new Vector3(pX - (FastGUI.targetWidht / 2), FastGUI.targetHeight / 2 - pY, 0); 
  21.         tTargetPos.x = tTargetPos.x / (FastGUI.targetHeight / 2); 
  22.         tTargetPos.y = tTargetPos.y / (FastGUI.targetHeight / 2); 
  23.  
  24.  
  25.         tTargetPos.x += FastGUI.actualRoot.transform.position.x; 
  26.         tTargetPos.y += FastGUI.actualRoot.transform.position.y; 
  27.  
  28.  
  29.         return tTargetPos; 
  30.     } 
  31.  
  32.     public static Transform CreateAnchor(XMLNode pNode, UIPanel pTargetRootPannel, Transform pLastAnchor,string pLastAnchorPath) 
  33.     { 
  34.         string tName = pNode.GetNode("name>0")["_text"].ToString(); 
  35.         string tParentName = pNode.GetNode("path>0")["_text"].ToString(); 
  36.         float tPosX = float.Parse(pNode.GetNode("posX>0")["_text"].ToString()); 
  37.         float tPosY = float.Parse(pNode.GetNode("posY>0")["_text"].ToString()); 
  38.         int tLayerID = int.Parse(pNode.GetNode("layerID>0")["_text"].ToString()); 
  39.         Transform lastRoot = pTargetRootPannel.transform; 
  40.  
  41.         if (tParentName == pLastAnchorPath) 
  42.         { 
  43.             lastRoot = pLastAnchor; 
  44.         } 
  45.         else 
  46.         { 
  47.             if (pTargetRootPannel.transform.FindChild(tParentName)) 
  48.                 lastRoot = pTargetRootPannel.transform.FindChild(tParentName); 
  49.         } 
  50.  
  51.         GameObject tNewGo; 
  52.         if (FastGUI.actualFastGUIOutput.references.ContainsKey(tLayerID)) 
  53.         { 
  54.             tNewGo = EditorUtility.InstanceIDToObject(FastGUI.actualFastGUIOutput.references[tLayerID])as GameObject; 
  55.             if (tNewGo == null
  56.             { 
  57.                 FastGUI.actualFastGUIOutput.references.Remove(tLayerID); 
  58.                 return CreateAnchor(pNode, pTargetRootPannel, pLastAnchor, pLastAnchorPath); 
  59.             } 
  60.         } 
  61.         else 
  62.         { 
  63.             tNewGo = new GameObject(tName); 
  64.             FastGUI.actualFastGUIOutput.references.Add(tLayerID, tNewGo.GetInstanceID()); 
  65.         } 
  66.  
  67.         tNewGo.transform.parent = lastRoot; 
  68.         tNewGo.transform.position = AdjustPosition(tPosX, tPosY); 
  69.         tNewGo.transform.localScale = Vector3.one; 
  70.  
  71.         return tNewGo.transform; 
  72.     } 
  73.  
  74.     public static Transform CreateSprite(XMLNode pNode, UIPanel pTargetRootPannel, Transform pLastAnchor,string pLastAnchorPath) 
  75.     { 
  76.         string tName = pNode.GetNode("name>0")["_text"].ToString(); 
  77.         string tParentName = pNode.GetNode("path>0")["_text"].ToString(); 
  78.         float tPosX = float.Parse(pNode.GetNode("posX>0")["_text"].ToString()); 
  79.         float tPosY = float.Parse(pNode.GetNode("posY>0")["_text"].ToString()); 
  80.         string tSource = pNode.GetNode("source>0")["_text"].ToString(); 
  81.         string tType = pNode.GetNode("type>0")["_text"].ToString(); 
  82.         string tAtlas = pNode.GetNode("atlas>0")["_text"].ToString(); 
  83.         int width = int.Parse(pNode.GetNode("width>0")["_text"].ToString()); 
  84.         int height = int.Parse(pNode.GetNode("height>0")["_text"].ToString()); 
  85.          
  86.         //AssetDatabase.LoadAssetAtPath( 
  87.         int tLayerID = int.Parse(pNode.GetNode("layerID>0")["_text"].ToString()); 
  88.         
  89.         Transform lastRoot = pTargetRootPannel.transform; 
  90.         if (tParentName == pLastAnchorPath) 
  91.         { 
  92.             lastRoot = pLastAnchor; 
  93.         } 
  94.         else 
  95.         { 
  96.             if (pTargetRootPannel.transform.FindChild(tParentName)) 
  97.                 lastRoot = pTargetRootPannel.transform.FindChild(tParentName); 
  98.         } 
  99.  
  100.         UISprite tSpriteWidget = null
  101.         GameObject tNewGo = null
  102.         if (FastGUI.actualFastGUIOutput.references.ContainsKey(tLayerID)) 
  103.         { 
  104.             tNewGo = EditorUtility.InstanceIDToObject(FastGUI.actualFastGUIOutput.references[tLayerID])as GameObject; 
  105.             if (tNewGo == null
  106.             { 
  107.                 FastGUI.actualFastGUIOutput.references.Remove(tLayerID); 
  108.                 //CreateSprite(pNode, pTargetRootPannel, pLastAnchor, pLastAnchorPath); 
  109.                 tSpriteWidget = NGUITools.AddWidget<UISprite>(lastRoot.gameObject); 
  110.                 FastGUI.actualFastGUIOutput.references.Add(tLayerID, tSpriteWidget.gameObject.GetInstanceID()); 
  111.             } 
  112.             else 
  113.             { 
  114.                 tSpriteWidget = tNewGo.GetComponent<UISprite>(); 
  115.             } 
  116.         } 
  117.         else 
  118.         { 
  119.             tSpriteWidget = NGUITools.AddWidget<UISprite>(lastRoot.gameObject); 
  120.             FastGUI.actualFastGUIOutput.references.Add(tLayerID, tSpriteWidget.gameObject.GetInstanceID()); 
  121.         } 
  122.  
  123.         tSpriteWidget.name = tName ; 
  124.         tSpriteWidget.type = (UISprite.Type)System.Enum.Parse(typeof(UISprite.Type),tType); 
  125.         if (!tAtlas.Equals("")) 
  126.         { 
  127.             UIAtlas targetAtlas = null
  128.             string[] tPaths = Directory.GetFiles(Application.dataPath +"/", tAtlas +".prefab", SearchOption.AllDirectories); 
  129.  
  130.  
  131.             foreach (var tPath in tPaths) 
  132.             { 
  133.                 string projectRelativePath = tPath.Substring(tPath.ToString().IndexOf("Assets/")); 
  134.                 targetAtlas = AssetDatabase.LoadAssetAtPath(projectRelativePath,typeof(UIAtlas))as UIAtlas; 
  135.                 if (targetAtlas != null
  136.                     break
  137.  
  138.             } 
  139.             tSpriteWidget.atlas = targetAtlas; 
  140.         } 
  141.         else 
  142.             tSpriteWidget.atlas = NGUISettings.atlas; 
  143.         tSpriteWidget.spriteName = tSource; 
  144.          
  145.         tSpriteWidget.pivot = NGUISettings.pivot; 
  146.         tSpriteWidget.depth = NGUITools.CalculateNextDepth(pTargetRootPannel.gameObject); 
  147.         tSpriteWidget.transform.localScale = Vector3.one; 
  148.         tSpriteWidget.transform.position = AdjustPosition(tPosX, tPosY); 
  149.         tSpriteWidget.width = width; 
  150.         tSpriteWidget.height = height; 
  151.          
  152.         tSpriteWidget.MakePixelPerfect(); 
  153.          
  154.         return lastRoot; 
  155.     } 
  156.     public static Transform CreateTextLabel(XMLNode pNode, UIPanel pTargetRootPannel, Transform pLastAnchor,string pLastAnchorPath) 
  157.     { 
  158.         // Node parameters: 
  159.         string tFontName = pNode.GetNode("fontName>0")["_text"].ToString(); 
  160.         string tFontColor = pNode.GetNode("fontColor>0")["_text"].ToString(); 
  161.         int width = int.Parse(pNode.GetNode("width>0")["_text"].ToString()); 
  162.         int height = int.Parse(pNode.GetNode("height>0")["_text"].ToString()); 
  163.  
  164.         string tContent = pNode.GetNode("contents>0")["_text"].ToString(); 
  165.         Debug.Log(tContent + "......"); 
  166.         int tFontSize = int.Parse(pNode.GetNode("fontSize>0")["_text"].ToString()); 
  167.  
  168.         Font targetFont = null
  169.         string[] tPaths = Directory.GetFiles(Application.dataPath +"/", tFontName +".ttf", SearchOption.AllDirectories); 
  170.  
  171.         foreach (var tPath in tPaths) 
  172.         { 
  173.             string projectRelativePath = tPath.Substring(tPath.ToString().IndexOf("Assets/")); 
  174.             targetFont = AssetDatabase.LoadAssetAtPath(projectRelativePath,typeof(Font))as Font; 
  175.             if (targetFont != null
  176.                 break
  177.         } 
  178.  
  179.         UILabel tLabel; 
  180.         Transform tContainer = CreateAnchor(pNode, pTargetRootPannel, pLastAnchor, pLastAnchorPath); 
  181.  
  182.         // Create Label: 
  183.         tLabel = tContainer.gameObject.AddComponent<UILabel>(); 
  184.  
  185.         // Set Position: 
  186.         tLabel.transform.localPosition = tContainer.localPosition; 
  187.  
  188.         string[] tColor = tFontColor.Split(';'); 
  189.         tLabel.color = new Color(float.Parse(tColor[0]) / 255f,float.Parse(tColor[1]) / 255f,float.Parse(tColor[2]) / 255f); 
  190.         tLabel.trueTypeFont = targetFont; 
  191.         tLabel.fontSize = tFontSize; 
  192.         tLabel.width = width; 
  193.         tLabel.height = height; 
  194.         tLabel.text = tContent; 
  195.         //Debug.Log(".........."+tContent); 
  196.         tLabel.overflowMethod = UILabel.Overflow.ResizeFreely; 
  197.         tLabel.depth = NGUITools.CalculateNextDepth(pTargetRootPannel.gameObject); ; 
  198.         tLabel.pivot = UIWidget.Pivot.Center; 
  199.  
  200.         tLabel.MakePixelPerfect(); 
  201.         //tLabel.MakePositionPerfect(); 
  202.  
  203.         return tContainer; 
  204.     } 
  205.     static public UIFont GetFont(string pFontName) 
  206.     { 
  207.         string[] tPaths = Directory.GetFiles(Application.dataPath +"/", pFontName +".prefab", SearchOption.AllDirectories); 
  208.  
  209.         foreach (var tPath in tPaths) 
  210.         { 
  211.             string projectRelativePath = tPath.Substring(tPath.ToString().IndexOf("Assets/")); 
  212.             UIFont tGO = AssetDatabase.LoadAssetAtPath(projectRelativePath,typeof(UIFont))as UIFont; 
  213.  
  214.             return tGO; 
  215.         } 
  216.         return null
  217.     } 
  218.      
  219.     public static Transform CreateCheckBox(XMLNode pNode, UIPanel pTargetRootPannel,Transform pLastAnchor,string pLastAnchorPath ) 
  220.     {    
  221.         string  tName       = pNode.GetNode ("name>0")["_text"].ToString(); 
  222.         string  tParentName = pNode.GetNode ("path>0")["_text"].ToString(); 
  223.         float   tPosX       = float.Parse(pNode.GetNode ("posX>0")["_text"].ToString()); 
  224.         float   tPosY       = float.Parse(pNode.GetNode ("posY>0")["_text"].ToString()); 
  225.         int     tLayerID    = int.Parse(pNode.GetNode ("layerID>0")["_text"].ToString()); 
  226.          
  227.         Transform lastRoot = pTargetRootPannel.transform; 
  228.          
  229.         if(tParentName == pLastAnchorPath) 
  230.         { 
  231.             lastRoot = pLastAnchor; 
  232.         } 
  233.         else 
  234.         { 
  235.             if(pTargetRootPannel.transform.FindChild(tParentName)) 
  236.                 lastRoot = pTargetRootPannel.transform.FindChild(tParentName); 
  237.         } 
  238.          
  239.         UIToggle tCheckBox = null
  240.         GameObject tNewGo = null
  241.         if(FastGUI.actualFastGUIOutput.references.ContainsKey(tLayerID)) 
  242.         { 
  243.             tNewGo      = EditorUtility.InstanceIDToObject(FastGUI.actualFastGUIOutput.references[tLayerID])as GameObject; 
  244.             if(tNewGo == null
  245.             { 
  246.                 FastGUI.actualFastGUIOutput.references.Remove(tLayerID);     
  247.                 return CreateCheckBox(pNode, pTargetRootPannel, pLastAnchor, pLastAnchorPath); 
  248.             } 
  249.             else 
  250.             { 
  251.                 tCheckBox   = tNewGo.GetComponent<UIToggle>(); 
  252.                  
  253.                 foreach(Transform tTransin tNewGo.transform) 
  254.                 { 
  255.                     DestroyImmediate(tTrans.gameObject); 
  256.                 } 
  257.             } 
  258.         } 
  259.         else 
  260.         { 
  261.             tNewGo                      = new GameObject(tName); 
  262.             tNewGo.transform.parent     = lastRoot; 
  263.             tNewGo.transform.position   = AdjustPosition(tPosX, tPosY); 
  264.             tNewGo.transform.localScale = Vector3.one; 
  265.              
  266.             tCheckBox                   = tNewGo.AddComponent<UIToggle>(); 
  267.              
  268.             FastGUI.actualFastGUIOutput.references.Add(tLayerID, tNewGo.GetInstanceID()); 
  269.         }        
  270.         NGUITools.AddWidgetCollider(tCheckBox.gameObject); 
  271.          
  272.         return tCheckBox.transform; 
  273.     } 
  274.     public static Transform CreateSlider(XMLNode pNode, UIPanel pTargetRootPannel, Transform pLastAnchor,string pLastAnchorPath ) 
  275.     {    
  276.         string  tName       = pNode.GetNode ("name>0")["_text"].ToString(); 
  277.         string  tParentName = pNode.GetNode ("path>0")["_text"].ToString(); 
  278.         float   tPosX       = float.Parse(pNode.GetNode ("posX>0")["_text"].ToString()); 
  279.         float   tPosY       = float.Parse(pNode.GetNode ("posY>0")["_text"].ToString()); 
  280.         int     tLayerID    = int.Parse(pNode.GetNode ("layerID>0")["_text"].ToString()); 
  281.  
  282.         Transform lastRoot = pTargetRootPannel.transform; 
  283.          
  284.          
  285.         if(tParentName == pLastAnchorPath) 
  286.         { 
  287.             lastRoot = pLastAnchor; 
  288.         } 
  289.         else 
  290.         { 
  291.             if(pTargetRootPannel.transform.FindChild(tParentName)) 
  292.                 lastRoot = pTargetRootPannel.transform.FindChild(tParentName); 
  293.         } 
  294.          
  295.         UISlider tSlider = null
  296.         GameObject tNewGo = null
  297.         if(FastGUI.actualFastGUIOutput.references.ContainsKey(tLayerID)) 
  298.         { 
  299.             tNewGo          = EditorUtility.InstanceIDToObject(FastGUI.actualFastGUIOutput.references[tLayerID])as GameObject; 
  300.             if(tNewGo == null
  301.             { 
  302.                 FastGUI.actualFastGUIOutput.references.Remove(tLayerID);     
  303.                 return CreateCheckBox(pNode, pTargetRootPannel, pLastAnchor, pLastAnchorPath); 
  304.             } 
  305.             else 
  306.             { 
  307.                 tSlider         = tNewGo.GetComponent<UISlider>(); 
  308.                  
  309.                 foreach(Transform tTransin tNewGo.transform) 
  310.                 { 
  311.                     DestroyImmediate(tTrans.gameObject); 
  312.                 } 
  313.             } 
  314.         } 
  315.         else 
  316.         { 
  317.             tNewGo                      = new GameObject(tName); 
  318.             tNewGo.transform.parent     = lastRoot; 
  319.             tNewGo.transform.position   = AdjustPosition(tPosX, tPosY); 
  320.             tNewGo.transform.localScale = Vector3.one; 
  321.              
  322.             tSlider                     = tNewGo.AddComponent<UISlider>(); 
  323.              
  324.             FastGUI.actualFastGUIOutput.references.Add(tLayerID, tNewGo.GetInstanceID()); 
  325.         } 
  326.  
  327.         NGUITools.AddWidgetCollider(tSlider.gameObject); 
  328.          
  329.         return tSlider.transform; 
  330.     } 
  331.     public static Transform CreateProgressBar(XMLNode pNode, UIPanel pTargetRootPannel, Transform pLastAnchor,string pLastAnchorPath ) 
  332.     {    
  333.         string  tName       = pNode.GetNode ("name>0")["_text"].ToString(); 
  334.         string  tParentName = pNode.GetNode ("path>0")["_text"].ToString(); 
  335.         float   tPosX       = float.Parse(pNode.GetNode ("posX>0")["_text"].ToString()); 
  336.         float   tPosY       = float.Parse(pNode.GetNode ("posY>0")["_text"].ToString()); 
  337.         int     tLayerID    = int.Parse(pNode.GetNode ("layerID>0")["_text"].ToString()); 
  338.          
  339.         Transform lastRoot = pTargetRootPannel.transform; 
  340.          
  341.          
  342.         if(pTargetRootPannel.transform.FindChild(tParentName)) 
  343.             lastRoot = pTargetRootPannel.transform.FindChild(tParentName); 
  344.                  
  345.          
  346.         UIScrollBar tSlider = null
  347.         GameObject tNewGo = null
  348.         if(FastGUI.actualFastGUIOutput.references.ContainsKey(tLayerID)) 
  349.         { 
  350.             tNewGo = EditorUtility.InstanceIDToObject(FastGUI.actualFastGUIOutput.references[tLayerID])as GameObject; 
  351.             if(tNewGo == null
  352.             { 
  353.                 FastGUI.actualFastGUIOutput.references.Remove(tLayerID);     
  354.                 return CreateCheckBox(pNode, pTargetRootPannel, pLastAnchor, pLastAnchorPath); 
  355.             } 
  356.             else 
  357.             { 
  358.                 tSlider = tNewGo.GetComponent<UIScrollBar>(); 
  359.                  
  360.                 foreach(Transform tTransin tNewGo.transform) 
  361.                 { 
  362.                     DestroyImmediate(tTrans.gameObject); 
  363.                 } 
  364.             } 
  365.         } 
  366.         else 
  367.         { 
  368.             tNewGo                      = new GameObject(tName); 
  369.             tNewGo.transform.parent     = lastRoot; 
  370.             tNewGo.transform.position   = AdjustPosition(tPosX, tPosY); 
  371.             tNewGo.transform.localScale = Vector3.one; 
  372.  
  373.             tSlider = tNewGo.AddComponent<UIScrollBar>(); 
  374.             NGUITools.AddWidgetCollider(tSlider.gameObject); 
  375.             FastGUI.actualFastGUIOutput.references.Add(tLayerID, tNewGo.GetInstanceID()); 
  376.         } 
  377.          
  378.         return tSlider.transform; 
  379.     } 
  380.     public static Transform CreateButton(XMLNode pNode, UIPanel pTargetRootPannel, Transform pLastAnchor,string pLastAnchorPath ) 
  381.     {    
  382.         string  tName       = pNode.GetNode ("name>0")["_text"].ToString(); 
  383.         string  tParentName = pNode.GetNode ("path>0")["_text"].ToString(); 
  384.         float   tPosX       = float.Parse(pNode.GetNode ("posX>0")["_text"].ToString()); 
  385.         float   tPosY       = float.Parse(pNode.GetNode ("posY>0")["_text"].ToString()); 
  386.         int     tLayerID    = int.Parse(pNode.GetNode ("layerID>0")["_text"].ToString()); 
  387.          
  388.          
  389.         Transform lastRoot = pTargetRootPannel.transform; 
  390.          
  391.          
  392.         if(tParentName == pLastAnchorPath) 
  393.         { 
  394.             lastRoot = pLastAnchor; 
  395.         } 
  396.         else 
  397.         { 
  398.             if(pTargetRootPannel.transform.FindChild(tParentName)) 
  399.                 lastRoot = pTargetRootPannel.transform.FindChild(tParentName); 
  400.         } 
  401.          
  402.         Button tImageButton = null
  403.         GameObject tNewGo = null
  404.         if(FastGUI.actualFastGUIOutput.references.ContainsKey(tLayerID)) 
  405.         { 
  406.             tNewGo  = EditorUtility.InstanceIDToObject(FastGUI.actualFastGUIOutput.references[tLayerID])as GameObject; 
  407.             if(tNewGo == null
  408.             { 
  409.                 FastGUI.actualFastGUIOutput.references.Remove(tLayerID);     
  410.                 return CreateButton(pNode, pTargetRootPannel, pLastAnchor, pLastAnchorPath); 
  411.             } 
  412.             else 
  413.             { 
  414.                 tImageButton    = tNewGo.GetComponent<Button>(); 
  415.                 foreach(Transform tTransin tNewGo.transform) 
  416.                 { 
  417.                     DestroyImmediate(tTrans.gameObject); 
  418.                 } 
  419.             } 
  420.         } 
  421.         else 
  422.         { 
  423.             tNewGo                      = new GameObject(tName); 
  424.             tNewGo.transform.parent     = lastRoot; 
  425.             tNewGo.transform.position   = AdjustPosition(tPosX, tPosY); 
  426.             tNewGo.transform.localScale = Vector3.one; 
  427.              
  428.             tImageButton                = tNewGo.AddComponent<Button>(); 
  429.              
  430.             FastGUI.actualFastGUIOutput.references.Add(tLayerID, tNewGo.GetInstanceID()); 
  431.         } 
  432.          
  433.         NGUITools.AddWidgetCollider(tImageButton.gameObject); 
  434.          
  435.         return tImageButton.transform; 
  436.     } 
  437.     static public Transform CreateTextInput( XMLNode pNode, UIPanel pTargetRootPannel, Transform pLastAnchor,string pLastAnchorPath ) 
  438.     { 
  439.         string tName = pNode.GetNode("name>0")["_text"].ToString(); 
  440.         string tParentName = pNode.GetNode("path>0")["_text"].ToString(); 
  441.         float tPosX = float.Parse(pNode.GetNode("posX>0")["_text"].ToString()); 
  442.         float tPosY = float.Parse(pNode.GetNode("posY>0")["_text"].ToString()); 
  443.         int tLayerID = int.Parse(pNode.GetNode("layerID>0")["_text"].ToString()); 
  444.  
  445.  
  446.         Transform lastRoot = pTargetRootPannel.transform; 
  447.  
  448.  
  449.         if (tParentName == pLastAnchorPath) 
  450.         { 
  451.             lastRoot = pLastAnchor; 
  452.         } 
  453.         else 
  454.         { 
  455.             if (pTargetRootPannel.transform.FindChild(tParentName)) 
  456.                 lastRoot = pTargetRootPannel.transform.FindChild(tParentName); 
  457.         } 
  458.  
  459.         UIInput tImageButton = null
  460.         GameObject tNewGo = null
  461.         if (FastGUI.actualFastGUIOutput.references.ContainsKey(tLayerID)) 
  462.         { 
  463.             tNewGo = EditorUtility.InstanceIDToObject(FastGUI.actualFastGUIOutput.references[tLayerID])as GameObject; 
  464.             if (tNewGo == null
  465.             { 
  466.                 FastGUI.actualFastGUIOutput.references.Remove(tLayerID); 
  467.                 return CreateButton(pNode, pTargetRootPannel, pLastAnchor, pLastAnchorPath); 
  468.             } 
  469.             else 
  470.             { 
  471.                 tImageButton = tNewGo.GetComponent<UIInput>(); 
  472.                 foreach (Transform tTransin tNewGo.transform) 
  473.                 { 
  474.                     DestroyImmediate(tTrans.gameObject); 
  475.                 } 
  476.             } 
  477.         } 
  478.         else 
  479.         { 
  480.             tNewGo = new GameObject(tName); 
  481.             tNewGo.transform.parent = lastRoot; 
  482.             tNewGo.transform.position = AdjustPosition(tPosX, tPosY); 
  483.             tNewGo.transform.localScale = Vector3.one; 
  484.  
  485.             tImageButton = tNewGo.AddComponent<UIInput>(); 
  486.  
  487.             FastGUI.actualFastGUIOutput.references.Add(tLayerID, tNewGo.GetInstanceID()); 
  488.         } 
  489.  
  490.         NGUITools.AddWidgetCollider(tImageButton.gameObject); 
  491.  
  492.         return tImageButton.transform; 
  493.     } 
  494.      
  495.      

小结:

       最近工作有点忙,就没怎么更新博客,虽然满篇都是代码,D.S.Qiu觉得对这篇博客感兴趣的一定是了解用过FastGUI 的,代码上精简了很多,但是PSD导出的过程还是很慢,对JavaScript(没有类型的语言)还是挺无语的和没空去琢磨Adobe Script api,所以只能在原来的基础上造车,如果你有更好的从PSD到上次Unity的UI的方案或插件(其实FastGUI的最主要的作用就是定位),希望能够得到您的分享。

       如果您对D.S.Qiu有任何建议或意见可以在文章后面评论,或者发邮件(gd.s.qiu@gmail.com)交流,您的鼓励和支持是我前进的动力,希望能有更多更好的分享。

        转载请在文首注明出处:http://dsqiu.iteye.com/blog/1995248

更多精彩请关注D.S.Qiu的博客和微博(ID:静水逐风)

0 0
原创粉丝点击