Skip to content

Instantly share code, notes, and snippets.

@pharan pharan/RuntimeAssets.cs

Last active Mar 30, 2019
Embed
What would you like to do?
Sample code for creating runtime Spine ScriptableObjects.
using UnityEngine;
using System.Collections.Generic;
namespace Spine.Unity {
public static class RuntimeAssets {
public static AtlasAsset CreateAtlasAsset (TextAsset atlasText, Material[] materials, bool initialize) {
AtlasAsset atlasAsset = ScriptableObject.CreateInstance<AtlasAsset>();
atlasAsset.Reset();
atlasAsset.atlasFile = atlasText;
atlasAsset.materials = materials;
if (initialize)
atlasAsset.GetAtlas();
return atlasAsset;
}
// Only providing the textures is slower because it has to search for atlas page matches.
public static AtlasAsset CreateAtlasAsset (TextAsset atlasText, Texture2D[] textures, Shader shader, bool initialize) {
if (shader == null)
shader = Shader.Find("Spine/Skeleton");
// Get atlas page names.
string atlasString = atlasText.text;
atlasString = atlasString.Replace("\r", "");
string[] atlasLines = atlasString.Split('\n');
var pages = new List<string>();
for (int i = 0; i < atlasLines.Length - 1; i++) {
if (atlasLines[i].Trim().Length == 0)
pages.Add(atlasLines[i + 1].Trim().Replace(".png", ""));
}
// Populate Materials[] by matching texture names with page names.
var materials = new Material[pages.Count];
for (int i = 0, n = pages.Count; i < n; i++) {
Material mat = null;
// Search for a match.
string pageName = pages[i];
for (int j = 0, m = textures.Length; j < m; j++) {
if (string.Equals(pageName, textures[j].name, System.StringComparison.OrdinalIgnoreCase)) {
// Match found.
mat = new Material(shader);
mat.mainTexture = textures[j];
break;
}
}
if (mat != null)
materials[i] = mat;
else
throw new System.ArgumentException("Could not find matching atlas page in the texture array.");
}
// Create AtlasAsset normally
return CreateAtlasAsset(atlasText, materials, initialize);
}
public static SkeletonDataAsset CreateSkeletonDataAsset (TextAsset skeletonDataFile, AtlasAsset atlasAsset, bool initialize, float scale = 0.01f) {
return CreateSkeletonDataAsset(skeletonDataFile, new [] {atlasAsset}, initialize, scale);
}
public static SkeletonDataAsset CreateSkeletonDataAsset (TextAsset skeletonDataFile, AtlasAsset[] atlasAssets, bool initialize, float scale = 0.01f) {
SkeletonDataAsset skeletonDataAsset = ScriptableObject.CreateInstance<SkeletonDataAsset>();
skeletonDataAsset.Reset();
skeletonDataAsset.skeletonJSON = skeletonDataFile;
skeletonDataAsset.atlasAssets = atlasAssets;
skeletonDataAsset.scale = scale;
// pre-patch workaround
skeletonDataAsset.fromAnimation = new string[0];
skeletonDataAsset.toAnimation = new string[0];
if (initialize)
skeletonDataAsset.GetSkeletonData(true);
return skeletonDataAsset;
}
}
}
using UnityEngine;
using System.Collections;
using Spine.Unity;
public class RuntimeSpawn : MonoBehaviour {
public float spawnAfterSeconds = 2f;
public TextAsset skeletonDataFile;
public TextAsset atlasText;
public Shader shader;
public Texture2D[] textures;
IEnumerator Start () {
yield return new WaitForSeconds(spawnAfterSeconds);
// Create runtime assets.
var atlasAsset = RuntimeAssets.CreateAtlasAsset(atlasText, textures, shader, true);
var skeletonDataAsset = RuntimeAssets.CreateSkeletonDataAsset(skeletonDataFile, atlasAsset, true);
// Use them to instantiate a SkeletonAnimation.
SkeletonAnimation skeletonAnimation = SkeletonAnimation.NewSkeletonAnimationGameObject(skeletonDataAsset);
// Enjoy as is.
skeletonAnimation.Initialize(false);
skeletonAnimation.state.SetAnimation(0, "walk", true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.