unity3d 贴图优化,借用ps脚本pot批处理

来源:互联网 发布:开心贷网络借贷 编辑:程序博客网 时间:2024/06/02 03:26

项目转战微端后,遇到一个问题:贴图如果不是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 cmvar 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;            //fixedvar _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;}