Hello, I'm trying to load and import spine assets at runtime:
var atlasRequest = UnityWebRequest.Get(atlasUri);
var skeletonRequest = UnityWebRequest.Get(skeletonUri);
var textureRequest = UnityWebRequestTexture.GetTexture(textureUri);
atlasRequest.SendWebRequest();
skeletonRequest.SendWebRequest();
textureRequest.SendWebRequest();
yield return new WaitUntil(() =>
atlasRequest.downloadHandler.isDone
&& skeletonRequest.downloadHandler.isDone
&& textureRequest.downloadHandler.isDone
);
atlasText = new TextAsset(atlasRequest.downloadHandler.text);
skeleText = new TextAsset(skeletonRequest.downloadHandler.text);
spineTexture = ((DownloadHandlerTexture)textureRequest.downloadHandler).texture;
spineAtlas = SpineAtlasAsset.CreateRuntimeInstance(atlasText, new Texture2D[] { spineTexture }, defaultMaterial.shader, true);
The web requests come through correctly and the files are loaded, but the last line (SpineAtlasAsset.CreateRuntimeInstance) throws an error:
ArgumentException: Could not find matching atlas page in the texture array.
Spine.Unity.SpineAtlasAsset.CreateRuntimeInstance (UnityEngine.TextAsset atlasText, UnityEngine.Texture2D[] textures, UnityEngine.Material materialPropertySource, System.Boolean initialize) (at Assets/3RDPARTY/Spine/Runtime/spine-unity/Asset Types/SpineAtlasAsset.cs:98)
Spine.Unity.SpineAtlasAsset.CreateRuntimeInstance (UnityEngine.TextAsset atlasText, UnityEngine.Texture2D[] textures, UnityEngine.Shader shader, System.Boolean initialize) (at Assets/3RDPARTY/Spine/Runtime/spine-unity/Asset Types/SpineAtlasAsset.cs:112)
I... don't understand, to be honest - I don't understand why the second parameter in CreateRuntimeInstance is an array of textures - all the assets I have been given only have one texture per character, since it's all packed into one atlas, and even the documentation seems to assume this is the normal way (spine-unity Runtime Documentation: Advanced Instantiation at Runtime), it says "or even directly from the three exported assets", but then glosses over the disparity in the code, in which, it suddenly provides an array of textures...
Therefore, in my code, as you can see, i'm providing as a parameter a one-item array containing that single texture, which gives me this error.
The spine files are from version 3.8.99, and the spine-unity library I was using at first was version 4.0.xx, which which I assumed was the problem, so I swapped the runtime for the 3.8 compatible one, but the error still persists...
Therefore I genuinely don't know what the problem is. The same spine files get imported into the editor at edit-time, without any issues.
Any idea about what the problem might be?
(I have attached the spine assets I'm trying this on)
Oh. Solved.
Your import code is matching the texture(s) based on name...
...in unity, loaded-at-runtime assets don't get their name set...
...so before calling the CreateRuntimeInstance, I need to set the name property of the texture manually (to the name of the file, without the extension)
spineTexture = ((DownloadHandlerTexture)textureRequest.downloadHandler).texture;
spineTexture.name = "2head"; // (for this specific case of mine
spineAtlas = SpineAtlasAsset.CreateRuntimeInstance(atlasText, new Texture2D[] { spineTexture }, defaultMaterial.shader, true);
You might want to add this note into the docs. I would suggest changing the error text to something more explanatory, but I assume that error can be triggered in multiple ways, so that wouldn't be ideal. However, a note in the docs would be nice, even though... to be honest, as soon as I stopped being lazy and read the code of that CreateRuntimeInstance function, it was pretty obvious what I need to do to fix it.
Sorry for the needless thread. The only reason I'm not deleting it is for it to be one relevant google result when someone tries to look it up next time.