public void CreateCharacter()
{
SkeletonAnimation playerAnim;
SkeletonDataAsset playerData;
AtlasAsset atlasdata;
string name = "Knight";
atlasdata = ScriptableObject.CreateInstance<AtlasAsset> ();
playerData = ScriptableObject.CreateInstance<SkeletonDataAsset> ();
playerData.fromAnimation = new string[0];
playerData.toAnimation = new string[0];
playerData.duration = new float[0];
playerData.scale = 0.01f;
playerData.defaultMix = 0.15f;
atlasdata.atlasFile = (TextAsset)Resources.Load (name + ".atlas", typeof(TextAsset));
Material[] materials = new Material[1];
materials [0] = new Material (Shader.Find ("Spine/Skeleton"));
Texture aa = (Texture)Resources.Load (name, typeof(Texture2D));
materials [0].mainTexture = aa;
atlasdata.materials = materials;
playerData.atlasAsset = atlasdata;
playerData.skeletonJSON = (TextAsset)Resources.Load (name, typeof(TextAsset));
GameObject player = new GameObject();
player.transform.localPosition = Vector3.zero;
player.transform.localScale = new Vector3 (1f, 1f, 1f);
playerAnim = (SkeletonAnimation)player.AddComponent ("SkeletonAnimation");
playerAnim.skeletonDataAsset = playerData;
playerAnim.calculateNormals = true;
playerAnim.loop = true;
}
I've tweaked this a bit for you.
Most notably:
These must all be initialized (this is a flaw in the runtime, @Nate, I'll submit a pull request soon to fix) before the Skeleton JSON file is parsed or the SkeletonDataAsset.GetSkeleton function will take a poop.
playerData.fromAnimation = new string[0];
playerData.toAnimation = new string[0];
playerData.duration = new float[0];
atlasdata.atlasFile = (TextAsset)Resources.Load (name + ".atlas");
playerData.skeletonJSON = (TextAsset)Resources.Load (name + ".json");
This is where Unity gets obnoxious. the Atlas file is actually a .txt file with .atlas on the end so that line will work.
The Skeleton / Spine file is a .json file so it cannot be accessed by including the extension .json using Resources.Load.
Additionally, because of the same-name issue, Resources.Load(name) (without the json extension...) will return either the Texture or the JSON...
So you must specify the type of asset you wish to return using the overload of the Resources.Load function.
playerData.skeletonJSON = (TextAsset)Resources.Load (name, typeof(TextAsset));
Finally, the SkeletonAnimation.AnimationName property cannot be used until after SkeletonAnimation has initialized its AnimationState, which is best accomplished by waiting a frame for it to call its own Reset function and get your stuff setup.
playerAnim.AnimationName = "running";
Don't do this until after a frame has passed after creating your game object.