Unity Spine插件使用

Unity Spine插件使用

Spine是针对游戏开发的2D骨骼动画。Unity本身并不支持播放spine文件需要安装 Unity Spine插件才能支持。

Spine插件下载安装

Spine插件可以再Spine官方文档中找到.

注意:需要确保spine-unity组件包与美术使用的Spine版本能对应上。如果版本无法对应上可能无法正常使用。
版本文件可以在Spine插件目录下的version.txt找到。 文件内容大概如下:

This Spine-Unity runtime works with data exported from Spine Editor
version: 3.8.xx Package version:
spine-unity-3.8-2020-10-28.unitypackage

Spine资源导入

Spine资源在导出的时候可以选择 Json 格式或者二进制格式。在导出的时候需要进行一些设置,否则需要在SkeletonDataAsset中手动拖拽Skeleton JSONAtlas Assets.

  • Json导出
    使用Json导出默认是会产生 .png .json .atlas 这三个文件。 由于Spine插件无法正常识别到.atlas 文件所以我们需要在导出时设置其后缀名为 .atlas.txt(手动改也是可以的)。

  • 二进制导出
    二进制导出和上面的类似,会生成 .png .skel .atlas 这三个文件。为了方便Spine插件识别我们需要将其变更为 .png .skel.bytes .atlas.txt

按照上面设置完成后就可以直接从文件夹中拖放到 Unity 的 Project 窗口。

注意:以二进制格式而不是JSON格式导出,会使文件体积更小,加载更快.

Spine资源导入源码

Spine插件中的 SpineEditorUtilities 这个类继承自 AssetPostprocessor. 这样会在我们进行资源导入的时候触发对应函数。 我们可以看到spine插件是在所有资源被导入后调用。

public partial class SpineEditorUtilities : AssetPostprocessor {
    
    

	// ...
	// Auto-import post process entry point for all assets
	static void OnPostprocessAllAssets (string[] imported, string[] deleted, string[] moved, string[] movedFromAssetPaths) {
    
    
			
		// ...
		AssetUtility.HandleOnPostprocessAllAssets(imported, texturesWithoutMetaFileCopy);
		
		// ...
	}
}

然后在 AssetUtility 这个类中对文件的后缀名进行了处理。 可以从源码看到Spine插件会根据后缀名来识别到是否要进行加载. 我们也可以修改代码来匹配我们需要的后缀名(虽然不太推荐)。

public static class AssetUtility {
    
    
	// ...
	public static void ImportSpineContent (string[] imported, List<string> texturesWithoutMetaFile,
	bool reimport = false) {
    
    
	
	// ...

	foreach (string str in imported) {
    
    
		string extension = Path.GetExtension(str).ToLower();
		switch (extension) {
    
    
			case ".atlas":
				if (SpineEditorUtilities.Preferences.atlasTxtImportWarning) {
    
    
					Debug.LogWarningFormat("`{0}` : If this file is a Spine atlas, please change its extension to `.atlas.txt`. This is to allow Unity to recognize it and avoid filename collisions. You can also set this file extension when exporting from the Spine editor.", str);
				}
				break;
			case ".txt":
				if (str.EndsWith(".atlas.txt", System.StringComparison.Ordinal))
					atlasPaths.Add(str);
				break;
			case ".png":
			case ".jpg":
				imagePaths.Add(str);
				break;
			case ".json":
				var jsonAsset = AssetDatabase.LoadAssetAtPath<TextAsset>(str);
				if (jsonAsset != null && IsSpineData(jsonAsset, out compatibilityProblemInfo))
					skeletonPaths.Add(new PathAndProblemInfo(str, compatibilityProblemInfo));
				break;
			case ".bytes":
				if (str.ToLower().EndsWith(".skel.bytes", System.StringComparison.Ordinal)) {
    
    
					if (IsSpineData(AssetDatabase.LoadAssetAtPath<TextAsset>(str), out compatibilityProblemInfo))
						skeletonPaths.Add(new PathAndProblemInfo(str, compatibilityProblemInfo));
				}
				break;
		}
	}
	
	// ...
}

猜你喜欢

转载自blog.csdn.net/qq_36433883/article/details/125379140