天天看點

巧用TexturePacker指令行,與ant內建

TexturePacker做的非常出色,打包紋理,制作spritesheet的首選利器,它的完美還展現在視圖與指令的分離,提供了完善的指令行工具,功能完全覆寫了界面所提供的功能,而且效率更快。

相信做技術的還是最喜歡使用指令行功能,因為其可以配合一些腳本完全避免掉瑣碎的手工操作,能批量操作,方面素材重構及整理,比如每次有資源更新,需要收到打開.tps檔案,然後在publish,檔案少了無所謂,如果有一大推檔案需要調整,那估計就顯得重複而枯燥了。

關于其指令行的官方文檔,已講得非常清晰:http://www.codeandweb.com/texturepacker/documentation

下面給出指令行的兩種使用方式:

1、方式一:TexturePacker  **.tps

此種方式适用于你已經使用TexturePacker  GUI制作了一個tps檔案,使用此行指令直接釋出結果。他所做的事相當于:點開TexturePacker, open xxx.tps檔案, 再點選publish,這樣對比一看是不是非常省事。

         如果你有一大推這種tps檔案,那使用一個腳本,周遊出所有的tps檔案,執行此行指令即可一鍵更新所有資源。如下行指令:

find . -name "*.tps" | xargs TexturePacker

2、方式二:如果不想手動制作tps,也是可行的,下面給出一個內建到ant中的使用,大家一看便知:

<?xml version="1.0" encoding="UTF-8"?>
<project name="Asset Packager" default="main">
	<!--Description: 素材打包工具 -->
	<!--Author: [email protected] -->
	<!--Date: 2013年12月18日 下午3:54:52 -->
	
	<property name="ant_dir" location="antlib"/>
    <path id="classpath">
		<fileset dir="${ant_dir}" includes="**/*.jar"/>
	</path>
	<taskdef resource="net/sf/antcontrib/antlib.xml" classpathref="classpath" target="_blank" rel="external nofollow" />
	
	<property name="resRoot" value="../assets/res"/>
	<property name="plistRoot" value="${resRoot}/plists"/>
	<property name="packageRoot" value="${resRoot}/package"/>
	
	<macrodef name="sheet">
	   <attribute name="folderName"/>
	   <attribute name="outFolder" default=""/>
	   <attribute name="multipack" default="false"/>
	   <sequential>
			<delete dir="temp"/>
			<copy todir="temp/res/@{folderName}">
			    <fileset dir="${packageRoot}/@{folderName}">
			        <include name="**/*.png"/>
			        <include name="**/*.jpg"/>
			    </fileset>
			</copy>
			<if>
			     <equals arg1="@{multipack}" arg2="false"/>
			     <then>
					<exec executable="TexturePacker">
						<arg line="--format cocos2d --data temp/@{folderName}.plist 
						    	   --sheet temp/@{folderName}.png
								   --opt RGBA8888 --dither-fs-alpha
								   --max-width 2048 --max-height 2048 --size-constraints POT
								   --border-padding 0 --shape-padding 0
								   --algorithm MaxRects --maxrects-heuristics Best
								   --disable-rotation --trim-mode Trim temp"/>
					</exec>
					<delete dir="temp/res/@{folderName}"/>
					<copy todir="${plistRoot}/@{outFolder}">
					    <fileset dir="temp" includes="*.*"></fileset>
					</copy>
					<delete dir="temp"/>
			     </then>
			     <else>
			         <exec executable="TexturePacker">
						<arg line="--format cocos2d --data temp/@{folderName}{n}.plist 
						    	   --sheet temp/@{folderName}{n}.png
								   --opt RGBA8888 --dither-fs-alpha
								   --max-width 2048 --max-height 2048 --size-constraints POT
								   --border-padding 0 --shape-padding 0
								   --algorithm MaxRects --maxrects-heuristics Best
								   --disable-rotation --trim-mode Trim --multipack temp"/>
					</exec>
					<delete dir="temp/res/@{folderName}"/>
					<copy todir="${plistRoot}/@{outFolder}/@{folderName}">
					    <fileset dir="temp" includes="*.*"></fileset>
					</copy>
					<delete dir="temp"/>
			     </else>
			</if>
	   </sequential>
	</macrodef>
	
	<target name="main">
	    <sheet folderName="buildingIcon"/>
	    <sheet folderName="scienceIcon"/>
	    <sheet folderName="soldierIcon"/>
	    <sheet folderName="componets"/>
	    <sheet folderName="ui" multipack="true"/>
	    <sheet folderName="head"/>
	    <sheet folderName="battleUI" multipack="true"/>
	</target>
</project>
           

ant中定義了一個宏 sheet,folderName為需要打包的目錄名(打包會周遊該目錄以及其子目錄下得所有png,jpg),outFolder為導處路徑,multipack等于ture時,表示該目錄的素材可以打到不止一張紋理上,适用于圖檔資源過多,而一張紋理又放不下的情況,導出的紋理名字會有序号标示。