The Unity Spine plugin uses

The Unity Spine plugin uses

Spine is a 2D skeletal animation for game development. Unity itself does not support playing spine files and needs to install the Unity Spine plug-in to support it.

Spine plugin download and install

Spine plugins can be found in the official Spine documentation .

Note : You need to ensure that the spine-unity component package corresponds to the Spine version used by the artist. If the version does not match, it may not work properly.
Version files can be found in the Spine plugin directory version.txt. The content of the file is roughly as follows:

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 resource import

Spine resources can choose Json format or binary format when exporting. Some settings need to be made when exporting, otherwise you need to SkeletonDataAssetmanually drag Skeleton JSONand drop in Atlas Assets.

  • Json export Using Json export will generate these three files
    by default . .png .json .atlasSince the Spine plugin cannot recognize .atlasthe file normally, we need to set its suffix name when exporting .atlas.txt(manually changing it is also possible).

  • Binary export
    Binary export is similar to the above and will generate .png .skel .atlasthese three files. In order to facilitate the recognition of the Spine plugin, we need to change it to .png .skel.bytes .atlas.txt.

After completing the above settings, you can directly drag and drop from the folder to the Unity Projectwindow.

Note : Exporting in binary format instead of JSON will result in a smaller file size and faster loading.

Spine resource import source code

This class in the Spine plugin SpineEditorUtilitiesinherits from AssetPostprocessor . This will trigger the corresponding function when we import resources. We can see that the spine plugin is called after all resources are imported.

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);
		
		// ...
	}
}

Then AssetUtilitythe file extension is processed in this class. You can see from the source code that the Spine plugin will recognize whether to load according to the suffix. We can also modify the code to match the suffix we need (although it is not recommended).

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;
		}
	}
	
	// ...
}

Guess you like

Origin blog.csdn.net/qq_36433883/article/details/125379140