unity3d 貼圖優化,借用ps腳本pot批處理

項目轉戰微端後,遇到一個問題:貼圖如果不是pot(power of two),無法壓縮成最優格式dxt,最明顯的區別是,內存佔用過大。差距,舉例,如圖:

npot


pot

是不是很誇張?然後看了下texture 的import settings,發現pot其實是要求圖片的寬、高必須爲4的倍數,不是2.這裏就頭疼了。美術的ui,基本上都是導出後裁切透明像素,

根本不care pot。一張、兩張麻煩美術修改,倒也還好。做了工具,掃描項目中不合法的圖片,發現有幾十張,這就要瘋了。知道ps可以批處理後,就思考着能否用ps來解決這個問題。研究了下ps的腳本,倒也蠻簡單的,用的js,就是查起資料來賊麻煩。索性最後搞定了。自己寫的jsx腳本如下:

var document=app.activeDocument;
//bug,default is cm
var oldW=document.width.as("px");
var oldH=document.height.as("px");
var potBase=4;
  var w_left = oldW % potBase;
                    var h_right =oldH % potBase;
                     var x = oldW / potBase;
                      var y = oldH/ potBase;
       //fixed bug:javascript 沒有整型
x=parseInt(x);
y=parseInt(y);             
y = h_right != 0 ? y + 1 : y;     
x = w_left != 0 ? x + 1 : x;
x *= potBase;
y *= potBase;

var anchor=AnchorPosition.MIDDLECENTER;
preferences.rulerUnits=Units.PIXELS;
document.resizeCanvas (x, y, anchor);
document.save();
var options=SaveOptions.SAVECHANGES;
document.close (options);

這裏大概描述了思路,實測是ok的。但是也是有弊病的,沒法用在批處理。(反正我找了一圈沒找着),只能打開一張圖片後,選擇文件-腳本-瀏覽,選擇該腳本處理。最後,

祭出大殺器,直接改文件-腳本-圖像處理起,這樣批量導出圖片的時候處理。代碼如下:

if ( /*this.params["psd"]*/typename == "psd" ) {
			if ( ! this.runningFromBridge && this.params["keepstructure"] && ! this.params["saveinsame"] ) {
				var subFolderText = filePathNoName;
				subFolderText = subFolderText.replace( this.params["source"], inFolderLocation);
				subFolderText += "/";
			} else {
			var subFolderText = inFolderLocation;
			}
			Folder( subFolderText ).create();
			var historyState = app.activeDocument.activeHistoryState;
            //fixed
			var _width = app.activeDocument.width.as("px");
			var _height = app.activeDocument.height.as("px");
			var height = (Math.round(_height/4)*4); 
			var width = (Math.round(_width/4)*4); 
			if ( this.params["runaction"] ) {
				doAction( this.params["action"], this.params["actionset"] );
			}
			var uniqueFileName = CreateUniqueFileName( subFolderText, fileNameNoPath, ".psd" );
			if ( ! IsFolderWritable( subFolderText ) ) {
				alert( strCannotWriteToFolder + File( subFolderText ).fsName );
			} else {
			    app.activeDocument.resizeCanvas(width,height)
			    SaveAsPSD( uniqueFileName, this.params["icc"] );
			}
			app.activeDocument.activeHistoryState = historyState;
		}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章