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

运行后导出的序列帧

使用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=40300n")
        f.write(f":texture={meta['image']}n")
        f.write(f":size={texture_size['w']}x{texture_size['h']}n")
        f.write(":pivotpoints=enabledn")
        f.write(":borders=disabledn")
        f.write(":alphahandling=ClearTransparentPixelsn")
        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;0n")

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)

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

导入Unity并播放

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

总结

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

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

鸣谢

ChatGPT 4

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇

)">
下一篇>>