战魂小筑

讨论群:309800774 知乎关注:http://zhihu.com/people/sunicdavy 开源项目:https://github.com/davyxu

   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  257 随笔 :: 0 文章 :: 506 评论 :: 0 Trackbacks

cocos2dx引擎使用plist文件, 一种特殊的xml格式作为其atlas纹理的描述文件. plist遵循苹果的xml中key-value的设计风格.对于OC来说是合适的, 但xml本身性能低下, 垃圾内容过多, 也让plist对于高性能游戏引擎不再适合. 因此, 研究TexturePacker的导出插件技术

TexturePacker的自定义插件目录位于其安装目录的bin\exporters\下, 但有一些插件属于内建支持, 例如cocos2dx的plist格式, 因此无法找到对应插件

本人参考shiva3d插件, 对应导出界面的DataFormat中的Shiva3D, 快速学会了如何导出

官方文档位于http://www.codeandweb.com/texturepacker/documentation/#customization

插件的基本格式及原理是:

bin\exporters\下的某一目录下存在的一个名为exporter.xml文件作为插件的描述,例如:

<exporter version="1.0">
    <!-- identifier of the exporter -->
    <name>shiva3d</name>
 
    <!-- display name of the exporter for the combo box -->
    <displayName>Shiva3D</displayName>
    
    <!-- description of the exporter -->
    <description>Exporter for Shiva3D.</description>
 
    <!-- exporter version -->
    <version>1.0</version>
    
    <!-- currently only one file allowed - more to come with update -->
    <files>
        <file>
            <!-- name of this file variable -->
            <name>xml</name>
 
            <!-- human readable name (for GUI) -->
            <displayName>XML</displayName>
 
            <!-- file extension for the file -->
            <fileExtension>xml</fileExtension>
 
            <!-- name of the template file -->
            <template>shiva.xml</template>
        </file>
    </files>
 
    <!-- target framework supports trimming -->
    <supportsTrimming>false</supportsTrimming>
 
    <!-- target framework supports rotated sprites -->
    <supportsRotation>true</supportsRotation>
 
    <!-- rotated sprites direction (cw/ccw) -->
    <rotationDirection>cw</rotationDirection>
 
    <!-- supports npot sizes -->
    <supportsNPOT>true</supportsNPOT>
 
    <!-- supports file name stripping (remove .png etc) -->
    <supportsTrimSpriteNames>yes</supportsTrimSpriteNames>
 
    <!-- supports texure subpath -->
    <supportsTextureSubPath>yes</supportsTextureSubPath>
 
</exporter>
 
 

 

在Template字段中, 描述同目录的导出文件格式模板. TexturePacker使用一种叫Grantlee的模板引擎,类似于Python使用的Django模板引擎, 文档参见:Grantlee Documentation. 简单的文本格式可以参考shiva.xml快速学会

这里我们使用protobuf的文本格式(极为类似json)导出plist, 下面是导出模板

 

{% for sprite in allSprites %}
Sprite {
    Name: "{{sprite.trimmedName}}"
    FrameX: {{sprite.frameRect.x}}
    FrameY: {{sprite.frameRect.y}}
    FrameWidth: {{sprite.frameRectWithoutRotation.width}}
    FrameHeight: {{sprite.frameRectWithoutRotation.height}}
    OffsetX: {{sprite.cornerOffset.x}}
    OffsetY: {{sprite.cornerOffset.y}}
    OriginalWidth: {{sprite.untrimmedSize.width}}
    OriginalHeight: {{sprite.untrimmedSize.height}}
    {% if sprite.rotated %}Rotated: true {% endif %}
}
{% endfor %}

导出的结果类似于:

 
Sprite {
    Name: "car01"
    FrameX: 100
    FrameY: 129
    FrameWidth: 76
    FrameHeight: 47
    OffsetX: 0
    OffsetY: 0
    OriginalWidth: 76
    OriginalHeight: 47
    Rotated: true 
}
 
Sprite {
    Name: "car02"
    FrameX: 100
    FrameY: 51
    FrameWidth: 76
    FrameHeight: 47
    OffsetX: 0
    OffsetY: 0
    OriginalWidth: 76
    OriginalHeight: 47
    Rotated: true 
}

...

导出插件还支持js扩展, 具体内容请继续参考官方文档, 但对于简单的文本格式, 这种方式已经足够了

对比plist后, 发现plist中的垃圾信息极为多, 而且作为spriteframe的name居然带有扩展名...  因此脱离plist,编写自己的导出插件才是王道!

posted on 2014-02-06 15:23 战魂小筑 阅读(5177) 评论(0)  编辑 收藏 引用 所属分类: 游戏开发技术C++/ 编程语言工具使用及设计

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理