游戏中的资源管理-纹理

纹理资源

简述:

游戏中的图片资源,需要根据不同的情况将其规划成几张大图,有针对性的加载纹理,可以减少纹理的内存占用,减少绘制的批次(cocos中的自动批量绘制功能)。

资源打包

在资源编辑工具的文件夹下,使用ruby脚本调用TexturePacker 和 pngquanti工具提供的命令行(需要安装这两个软件并将运行目录添加到环境变量中) 将资源进行打包成大图与对图片进行压缩。
完成后将生成的资源移至游戏运行的资源目录下。
游戏中的资源管理-纹理_第1张图片
相关脚本:
state_animate_pack.rb

require 'fileutils'
require 'pathname'
require_relative '../../../../../ResTool/OperaScript/packer'

#在需要打包的资源的文件夹目录下 调用ruby
#目的是为了更新当前的工作路径 方便生成的资源放置到合适的目录
UIPacker.pack(true, true)

packer.rb

require 'fileutils'
require 'pathname'

class UIPacker

    def self.do_pack(path, format, data_file, sheet_file, prefix, pngquant_on, options)
        #打包path目录下的format格式的资源到 data_file(资源在大图中的信息 如坐标等) sheet_file(大图)
        system "TexturePacker --format cocos2d --texture-format #{format} --opt RGBA8888 --dither-none-nn "\
        "--size-constraints NPOT #{options} --data #{data_file} --sheet #{sheet_file} #{path}"

        #是否压缩
        system "pngquanti --speed 1 --ext .png --force -- #{sheet_file}" if pngquant_on

        #更新资源信息列表 自动更名为唯一命名资源(路径相关) 用于cocos加载
        #"\\1" 已替换的数量
        text = File.read(data_file)
        prefix.gsub!("\/", '_')
        png_name = prefix[prefix.rindex("ResTool_") + 8, prefix.length]
        puts png_name
        text.gsub!(/(.*)\.png<\/key>/, "#{png_name}_\\1.png")
        File.open(data_file, 'w') { |file| file.write(text) }
    end

    def self.pack(pngquant_on, is_state_animate, options = "")
        format='png'

        #获取执行ruby脚本的工作目录
        temp_path=Dir.getwd
        name=Pathname.new(temp_path).basename.to_s

        prefix=temp_path.sub(/.*\/ui_res/, 'res/xui')
        suffix="pvr2ccz" == format ? "pvr.ccz" : "png"

        puts name
        temp_data_file="#{temp_path}/#{name}.plist"
        temp_sheet_file="#{temp_path}/#{name}.#{suffix}"

        ##游戏可使用的资源 所放置的路径
        #编辑好的资源移至res下同名目录
        data_file=temp_data_file.sub("\/ResTool\/", '/')
        sheet_file=temp_sheet_file.sub("\/ResTool\/", '/')

        #如果是动画 同一对象会有不同的状态动画 此时需移至对象目录下(即上一级目录)
        data_file.sub!("\/#{name}\/", '/') if is_state_animate
        sheet_file.sub!("\/#{name}\/", '/') if is_state_animate

        puts data_file
        puts sheet_file

        File.unlink(temp_data_file) if File.exist?(temp_data_file)
        File.unlink(temp_sheet_file) if File.exist?(temp_sheet_file)

        #调用打包、压缩工具 命令行
        do_pack(temp_path, format, temp_data_file, temp_sheet_file, prefix, pngquant_on, options)

        #移动文件
        FileUtils.mv(temp_sheet_file, sheet_file, :force => true) if File.exist?(temp_sheet_file)
        FileUtils.mv(temp_data_file, data_file, :force => true) if File.exist?(temp_data_file)
    end
end

游戏运行时使用

如动画资源,帧动画在编辑后,生成了动作对应的大图与plist文件,将纹理缓存后即可根据纹理生成动画。
游戏中的资源管理-纹理_第2张图片
相关代码

function AnimationManger.GetAnimate(target, action)
    local spriteFrame = cc.SpriteFrameCache:getInstance()
    local animation = cc.Animation:create() 
    for i=1, 6 do  
        local str = string.format("%s_%s_%s_%s_%s.png", target:GetObjType(), target:GetResType(), target:GetResId(), action, i)
        local blinkFrame = spriteFrame:getSpriteFrame(str)  
        animation:addSpriteFrame(blinkFrame)  
    end  
    animation:setDelayPerUnit(0.1)--设置每帧的播放间隔  
    animation:setRestoreOriginalFrame(true)--设置播放完成后是否回归最初状态  
    return cc.Animate:create(animation)
end

小结:

资源编辑的目的之一是每一张载入的纹理有一个唯一的名字,我在plist文件中根据路径对资源重新命名,此时就可以拿到所需的小图。
不同的资源可能有不同的打包需求,需重新编辑目录下的ruby文件。
善用工具提供的命令行以及批量操作脚本,可以节省很多重复性操作所带来的时间。

你可能感兴趣的:(游戏相关-工具,ruby,游戏工具,资源管理)