• MOV导出序列帧并在Unity中播放


    前言

    收集到一批还不错的MG动画,想要在Unity中当特效播放出来,那首先就得把MOV变成序列帧,然后使用TexturePacker打成一个图集,最后再导入Unity中制作Animation Clip播放。

    项目

    将MOV变成序列帧

    Mov视频

    需要提前安装FFmpeg
    创建一个Cut.txt文本文件,将下面内容复制到文本文件中,把Cut.txt改成Cut.bat,就可以将代码中设置的mov视频导出成序列帧并存储在同级目录下。

    @echo off
    setlocal enabledelayedexpansion
    
    :: 设置输入视频文件和输出图片文件夹路径
    set input_video=Ele.mov
    set output_folder=output_images
    
    :: 创建输出文件夹
    mkdir %output_folder%
    
    :: 使用FFmpeg抓取视频帧并保存为透明图片
    ffmpeg -i %input_video% -vf "select=eq(n\,0)+eq(pict_type\,I)" -vsync vfr -q:v 2 %output_folder%\frame%%04d.png
    
    :: 完成后的消息
    echo 透明图片已经生成到 %output_folder% 文件夹中。
    
    :: 按任意键退出
    pause
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    运行后导出的序列帧

    使用TexturePacker打成一个图集

    这里我只找到了TexturePacker3.0.9的版本(pj方法就是用里面带的两个exe替换原始的两个exe)

    链接:https://pan.baidu.com/s/1C04rikUgbdlstwBJV_Ch7g?pwd=upvu
    提取码:upvu

    使用TexturePacker制作图集
    但是这里有一个问题,3.0.9版本只能导出json格式的图集表,Unity2021以上只能使用tpsheet格式的精灵表
    导出的Json格式精灵表
    所以需要下面的步骤

    将Json格式精灵表转换为tpsheet格式精灵表

    创建一个Python脚本,写入以下内容

    import json
    import os
    
    def convert_texture_packer_to_unity(json_file, output_file):
        with open(json_file, 'r') as f:
            data = json.load(f)
    
        frames = data['frames']
        meta = data['meta']
        texture_size = meta['size']
    
        with open(output_file, 'w') as f:
            f.write("#\n")
            f.write("# Sprite sheet data for Unity.\n")
            f.write("#\n")
            f.write(f":format=40300\n")
            f.write(f":texture={meta['image']}\n")
            f.write(f":size={texture_size['w']}x{texture_size['h']}\n")
            f.write(":pivotpoints=enabled\n")
            f.write(":borders=disabled\n")
            f.write(":alphahandling=ClearTransparentPixels\n")
            f.write("\n")
    
            for frame_name, frame_data in frames.items():
                frame = frame_data['frame']
                source_size = frame_data['sourceSize']
                sprite_source_size = frame_data['spriteSourceSize']
    
                x = frame['x']
                y = texture_size['h'] - frame['y'] - frame['h']
                w = frame['w']
                h = frame['h']
    
                pivot_x = sprite_source_size['x'] / source_size['w']
                pivot_y = 1 - (sprite_source_size['y'] / source_size['h'])
    
                f.write(f"{frame_name.replace('.png', '')};{x};{y};{w};{h}; {pivot_x};{pivot_y}; 0;0;0;0\n")
    
    if __name__ == "__main__":
        json_file = "link.txt"  # Replace with your JSON file name
        output_file = "link.tpsheet"  # Replace with your desired output file name
    
        convert_texture_packer_to_unity(json_file, output_file)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    正确填入json_fileoutput_file的名称j运行即可得到转换后的tpsheet精灵表
    转换前后精灵表对比

    导入Unity并播放

    需要提前导入TexturePacker的读取工具
    导入TexturePacker的读取工具
    再将之前转换的tpsheet和png格式图片放入工程中
    导入项目
    之后使用Animation Clip播放序列帧即可
    播放序列帧

    总结

    比较难的问题只有如何将Json格式精灵表转换为tpsheet格式精灵表

    可以到Github直接下载了
    https://github.com/SlowFeather/TransTxt2Tp

    鸣谢

    ChatGPT 4

  • 相关阅读:
    QClipboard
    免费泛域名SSL如何申请,和通配符有什么区别
    反射获取AQS中同步队列与等待队列的长度
    MySQL CREATE TABLE 简单设计模板交流
    界面组件DevExpress WinForms v23.1 - 增强的图表、甘特图功能
    KEIL仿真 logic analyzer
    linux环境下统计目录下所有文件的行数
    【计算机视觉 | 目标检测】arxiv 计算机视觉关于目标检测的学术速递(9 月 1 日论文合集)
    Canvas 从 0 到 1 -- 开发 2D 游戏《保卫家园》-- 【上篇】
    My Eighty-fifth Page - 买卖股票的最佳时机Ⅱ - By Nicolas
  • 原文地址:https://blog.csdn.net/a71468293a/article/details/132754700