• Runtimes
  • [Unity Runtime] Import spine (json+png+atlas) at runtime?

  • Изменено
Related Discussions
...

Hi all,

Having some fun with Spine and the Unity runtimes...

In order to make my download file for mobile smaller, I am putting most of my assets as PNGs and JPGs into the /StreamingAssets folder and create textures and sprites at runtime. E.g. like this:

WWW www = new WWW(textureFile);
yield return www;

Sprite sprite = new Sprite();
sprite = Sprite.Create(www.texture, new Rect(0, 0, 1280, 800), new Vector2(0, 0), 8.0f);
renderer.sprite = sprite;

This allows me to store my (many) textures as JPGs and optimized PNGs. When I store them as textures in Unity, they end up stored much larger.

Now, I want to do the same for my spine skeletons: Instead of creating the spine assets / prefab in the Editor at design time, I would like to create the spine assets and gameobjects at runtime.

Has anyone done this ? Is there code anywhere that I could base my importer on? I just peeked at "SpineEditorUtilities", and see a lot of code and Editor Scripts.

More info why I want to do this:

The main motivation for this approach is to be able to control exactly how texture data is stored and reducing download file size (.e.g. APK file size on Android). (Btw, I'm aware that loading the textures from /StreamingAssets takes longer and that once loaded in memory the textures will use more space than the compressed jpg/png. I'm ok with that. The main goal is to fit the game into the download file.)

All the answers you seek are in the SpineEditorUtilities.cs

Look... Haaarder. </rafiki>

Seriously though I'll try to whip up an example tomorrow.


That was actually harder than I thought it was going to be.

This package has a modified SkeletonDataAsset and AtlasAsset to allow them to process raw text sources rather than TextAsset type so you can pull in arbitrary data. Also included a bare bones Web Loader for getting files from the web and using them (same method Android's StreamingAssets through WWW zip extraction).

http://www.xdtech.net/spine/SpineUnity_ ... itypackage

Feel free and test it against the assets stored at http://www.xdtech.net/spine/data/

4 года спустя

Hello Mitch,

Is it possible for you to update this package for the latest unity and spine versions?

Thanks

Hello! You necroed a thread from 2014.

I'll see if there's a simple way to do this with the current runtimes.
SkeletonDataAsset and AtlasAsset may already have some methods for this since some were added to better accommodate threaded loading too.


Looks like you can SkeletonJsons are doable but not Atlases at the moment. There are some missing methods.

But the utility method roughly looks like this:

public static SkeletonDataAsset LoadWithStrings (string jsonFromSomewhere, string atlasStringFromSomewhere, Material[] materials, float jsonScale = 0.01f) {
   // Check for errors with parameters here. None of them should be null.

   var atlasAsset = ScriptableObject.CreateInstance<AtlasAsset>(); // This type changed to SpineAtlasAsset in 3.7
   atlasAsset.materials = materials;
   Spine.Atlas atlas;
   try {
      atlas = new Atlas(new StringReader(atlasStringFromSomewhere), "", new MaterialsTextureLoader(atlasAsset));
      atlas.FlipV(); // flips V component of UV coordinates to obey Unity scheme.
   } catch (System.Exception ex) {
      Debug.LogError("Error reading atlas.");
      throw ex;
   }
   //atlasAsset.InitializeWithData(atlas); // this is a missing method.
   
Spine.AtlasAttachmentLoader attachmentLoader = new AtlasAttachmentLoader(new Atlas[] { atlas }); Spine.SkeletonJson json = new SkeletonJson(attachmentLoader) { Scale = jsonScale }; StringReader input = new StringReader(jsonFromSomewhere); SkeletonData skeletonData; try { skeletonData = json.ReadSkeletonData(input); } catch (System.Exception ex) { Debug.LogError("Error reading skeleton json."); throw ex; } Spine.Unity.SkeletonDataAsset skeletonDataAsset = ScriptableObject.CreateInstance<SkeletonDataAsset>(); skeletonDataAsset.InitializeWithData(skeletonData); return skeletonDataAsset; }

This support will probably be added in 3.7 though we don't know what the exact API would look like yet.