作为个人的兴趣爱好,最新开始学习使用Unity尝试开发游戏。为了快速上手,选择了2D像素类型(pixel)作为入门学习首选。在选择和学习修改图片素材过程中发现了Aseprite这个非常好用的像素动画编辑软件,试用了一段时间后在steam上入了正版,这也是我第一次steam购买经历。虽然Aseprite作为开源软件可以从GITHUB上下载自行编译使用(/aseprite/aseprite/),但毕竟正版也不贵,顺便支持下软件作者!
说道正题,用Aseprite作出的帧序列动画,如果要手动导入到Unity中太麻烦了,所以就在网上找到了WeAthFoLD编写的Unity插件MetaSprite(/WeAthFoLD/MetaSprite)。不过MetaSprite应该是基于Unity及以前版本编写的,使用Unityf3版本加载后报错。查看Unity文档后,修改了ASEImporter.cs文件代码后可成功将Aseprite帧序列动画导入为Unity的Animation动画。
需要修改的方法:
public static void GenerateClipImageLayer(ImportContext ctx, string childPath, List<Sprite> frameSprites) {foreach (var tag in ctx.file.frameTags) {AnimationClip clip = ctx.generatedClips[tag];int time = 0;var keyFrames = new ObjectReferenceKeyframe[tag.to - tag.from + 2];for (int i = tag.from; i <= tag.to; ++i) {var aseFrame = ctx.file.frames[i];keyFrames[i - tag.from] = new ObjectReferenceKeyframe {time = time * 1e-3f,value = frameSprites[aseFrame.frameID]};var binding = new EditorCurveBinding {path = childPath,type = typeof(SpriteRenderer),propertyName = "m_Sprite"};AnimationUtility.SetObjectReferenceCurve(clip, binding, keyFrames); }}
var binding = new EditorCurveBinding {path = childPath,type = typeof(SpriteRenderer),propertyName = "m_Sprite"};将上述代码修改为:var binding = EditorCurveBinding.PPtrCurve("", typeof(SpriteRenderer), "m_Sprite");