Here you go. I used AtlasAsset instead of TextAsset - but process is the same after that.
using UnityEngine;
using System.Collections;
using System.IO;
using Spine;
[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
public class AtlasSprite : MonoBehaviour {
public AtlasAsset atlasAsset;
public string regionPath = "region";
public float pixelsPerUnit = 100;
Atlas atlas;
AtlasAttachmentLoader loader;
MeshFilter meshFilter;
Mesh mesh;
void Start(){
meshFilter = GetComponent<MeshFilter>();
//load atlas for referencing later
atlas = atlasAsset.GetAtlas();
//create instance of attachment loader
loader = new AtlasAttachmentLoader(atlas);
mesh = new Mesh();
meshFilter.mesh = mesh;
ShowRegion(regionPath);
}
void ShowRegion(string path){
//get atlas region attachment to get UV and size using Spine runtime
RegionAttachment a = loader.NewRegionAttachment(null, "Test", path);
if(a == null){
Debug.LogWarning("Couldnt find attachment: " + path);
return;
}
//find material assigned to atlas page
AtlasRegion region = atlas.FindRegion(path);
string matName = Path.GetFileNameWithoutExtension(region.page.name);
Material pageMaterial = null;
foreach(Material m in atlasAsset.materials){
if(m.mainTexture.name == matName){
pageMaterial = m;
break;
}
}
if(pageMaterial == null){
Debug.LogWarning("Couldn't find page: " + matName);
return;
}
renderer.material = pageMaterial;
//build mesh
Vector3 m00 = new Vector3( 0,0,0) / pixelsPerUnit;
Vector3 m10 = new Vector3( a.regionWidth,0,0) / pixelsPerUnit;
Vector3 m11 = new Vector3(a.regionWidth,a.regionHeight,0) / pixelsPerUnit;
Vector3 m01 = new Vector3(0, a.regionHeight,0) / pixelsPerUnit;
mesh.vertices = new Vector3[4]{ m00, m01, m11, m10 };
mesh.uv = new Vector2[4]{ new Vector2(a.uvs[0], a.uvs[1]), new Vector2(a.uvs[2], a.uvs[3]), new Vector2(a.uvs[4], a.uvs[5]), new Vector2(a.uvs[6], a.uvs[7]) };
mesh.triangles = new int[6]{0,1,2,2,3,0};
}
void OnGUI(){
regionPath = GUILayout.TextField(regionPath, GUILayout.MinWidth(300));
if(GUILayout.Button("Reload"))
ShowRegion(regionPath);
}
}