• RuntimesUnity
  • Can Spine Unity play spine without imported as SkeletonData?

I Want to play spine animation in unity, but the animation files: json, png, atlas , are downloaded from network.

So I wonder, can Spine Unity play without imported as SkeletonData, but with the pure json, png, atlas file ?

Related Discussions
...

Yes, you can play Spine animations in Unity using the downloaded JSON, PNG, and atlas files without importing them as SkeletonData assets. You can achieve this by loading the files at runtime and creating the necessary Spine objects programmatically.

Here is a general approach to do this:

  1. Download the files: Ensure your JSON, PNG, and atlas files are downloaded and accessible in your Unity project.

  2. Load the files at runtime: Use Unity's file I/O operations to read the JSON, PNG, and atlas files.

  3. Create Spine objects: Use the Spine runtime API to create the necessary objects from the loaded files.

Here is a simplified example of how you might do this:

using Spine;
using Spine.Unity;
using System.IO;
using UnityEngine;

public class SpineLoader : MonoBehaviour
{
    public string jsonPath;
    public string atlasPath;
    public string pngPath;

    private SkeletonAnimation skeletonAnimation;

    void Start()
    {
        // Load the JSON, atlas, and PNG files
        string json = File.ReadAllText(jsonPath);
        TextAsset atlasText = new TextAsset(File.ReadAllText(atlasPath));
        Texture2D texture = LoadTexture(pngPath);

        // Create the Atlas
        Atlas atlas = new Atlas(new StringReader(atlasText.text), "", new MaterialsTextureLoader(texture));

        // Create the SkeletonData
        SkeletonJson skeletonJson = new SkeletonJson(atlas);
        SkeletonData skeletonData = skeletonJson.ReadSkeletonData(new StringReader(json));

        // Create the SkeletonAnimation
        skeletonAnimation = SkeletonAnimation.NewSkeletonAnimationGameObject(skeletonData);
        skeletonAnimation.Initialize(true);
    }

    private Texture2D LoadTexture(string path)
    {
        byte[] fileData = File.ReadAllBytes(path);
        Texture2D texture = new Texture2D(2, 2);
        texture.LoadImage(fileData);
        return texture;
    }
}

In this example:

  • jsonPath, atlasPath, and pngPath are the paths to your downloaded files.
  • LoadTexture is a helper function to load the PNG file into a Texture2D.
  • The SkeletonAnimation object is created and initialized with the loaded data.

This approach allows you to dynamically load and play Spine animations in Unity without pre-importing them as SkeletonData assets.

For more detailed information, you can refer to the Spine Runtimes documentation.

Great work, thanks

@happyjiahan Note that there is also an example scene provided which shows how to do this:
Spine Examples/Other Examples/Instantiate from Script.
The GameObjects 3 RuntimeLoadFromExports and 4 Runtime BlendModes from Exports show how you can instantiate skeletons from just the exported files.

It's always worth having a look at the example scenes, as many common scenarios are demonstrated there already.

    21 ngày sau

    Harald Thanks very much!

    • Harald đã thích điều này.
    14 ngày sau

    hey guys, with your helps, I make the Spine Viewer Online
    https://spineviewer.online/

    The tool supports preview spine animations online, support different runtime versions: 3.7.x, 3.8.x, 4.0.x, 4.1.x, 4.2.x

    Thanks again for your helps.