Hi, I’m loving the 2D animation package, thank you for making it!
I was wondering if you could help me out with an issue I’m having. I’m doing a project that will ultimately be on WebGL. I have a character that animates with bones and skin and that all works great. What I want to do is give the user the option to upload a png to create a custom skin for the character.
What I’m currently doing is that I have the Texture2D loaded into memory(?) and I am creating a sprite out of it with Sprite.Create() and trying to swap it into the character via SpriteLibarary.AddOverride. I get this error when I try to do this. (Note: I am testing in Unity Editor, and I’m using package version 5.0.9 because I can’t upgrade to 2021)
IndexOutOfRangeException: Index 1089302692 is out of range of ‘2’ Length.
Unity.Collections.NativeArray1[T].FailOutOfRangeError (System.Int32 index) (at <af218701fe324032b521ddd91f13662b>:0) Unity.Collections.NativeArray
1[T].CheckElementReadAccess (System.Int32 index) (at :0)
Unity.Collections.NativeArray1[T].get_Item (System.Int32 index) (at <af218701fe324032b521ddd91f13662b>:0) UnityEngine.U2D.Animation.SpriteSkinUtility.Deform (Unity.Mathematics.float4x4 rootInv, Unity.Collections.NativeSlice
1[T] vertices, Unity.Collections.NativeSlice1[T] boneWeights, Unity.Collections.NativeArray
1[T] boneTransforms, Unity.Collections.NativeSlice1[T] bindPoses, Unity.Collections.NativeSlice
1[T] deformed) (at Library/PackageCache/com.unity.2d.animation@5.0.9/Runtime/SpriteSkinUtility.cs:294)
UnityEngine.U2D.Animation.SpriteSkinUtility.Deform (UnityEngine.Sprite sprite, UnityEngine.Matrix4x4 rootInv, Unity.Collections.NativeSlice1[T] vertices, Unity.Collections.NativeSlice
1[T] tangents, Unity.Collections.NativeSlice1[T] boneWeights, Unity.Collections.NativeArray
1[T] boneTransforms, Unity.Collections.NativeSlice1[T] bindPoses, Unity.Collections.NativeArray
1[T] deformableVertices) (at Library/PackageCache/com.unity.2d.animation@5.0.9/Runtime/SpriteSkinUtility.cs:265)
UnityEngine.U2D.Animation.SpriteSkinUtility.Deform (UnityEngine.Sprite sprite, UnityEngine.Matrix4x4 invRoot, UnityEngine.Transform[ ] boneTransformsArray, Unity.Collections.NativeArray`1[T] deformVertexData) (at Library/PackageCache/com.unity.2d.animation@5.0.9/Runtime/SpriteSkinUtility.cs:358)
UnityEngine.U2D.Animation.SpriteSkin.LateUpdate () (at Library/PackageCache/com.unity.2d.animation@5.0.9/Runtime/SpriteSkin.cs:282)
This is the code I am using below:
public void CustomSkin()
{
customSprite = Sprite.Create(texture2D, new Rect(450, 50, 1150, 1600), new Vector2(0.5f, 0.5f), 100.0f);
string referenceLabel = targetResolver.GetLabel();
Sprite referenceSprite = spriteLibrary.GetSprite(targetCategory, referenceLabel);
SpriteBone[ ] bones = referenceSprite.GetBones();
NativeArray poses = referenceSprite.GetBindPoses();
customSprite.SetBones(bones);
customSprite.SetBindPoses(poses);
const string customLabel = “customTorso”;
spriteLibrary.AddOverride(customSprite, targetCategory, customLabel);
targetResolver.SetCategoryAndLabel(targetCategory, customLabel);
targetResolver.gameObject.GetComponent().color = Color.white;
}
The odd thing is that it all works if I create the sprite a different way: customSprite = Resources.Load(texturePath). By loading the texture as sprite from the Resources folder I get no issues. The problem with this workflow is that if I want my user to upload via WebGL, I can’t save that png to resources and then load it from resources (at least my understanding is that I can’t save anything into Resources folder during runtime).
If you have any suggestions or can let me know what I am doing wrong, I would really appreciate it. Thank you!