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;
		}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值