I’m uploading a sprite using UnityWebRequest with this:
IEnumerator UpdatePNG(string path){
UnityWebRequest pngWR = new UnityWebRequest("file:///" + path);
DownloadHandlerTexture texDl = new DownloadHandlerTexture();
pngWR.downloadHandler = texDl;
yield return pngWR.SendWebRequest();
if (pngWR.result == UnityWebRequest.Result.Success) {
Texture2D t = texDl.texture;
Sprite s = Sprite.Create(t, new Rect(0, 0, t.width, t.height), new Vector2(.5f, .5f), 100f, 100, SpriteMeshType.Tight, new Vector4(0, 0, 0, 0));
pngSwapper.InjectCustom(s);
}
}
and then using that sprite in a SpriteLibraryAsset like so:
public void InjectCustom (Sprite customSprite){
// Duplicate bones and poses
string referenceLabel = targetResolver.GetLabel();
Sprite referenceHead =
spriteLibrary.GetSprite(targetCategory, referenceLabel);
SpriteBone[] bones = referenceHead.GetBones();
NativeArray<Matrix4x4> poses = referenceHead.GetBindPoses();
customSprite.SetBones(bones);
customSprite.SetBindPoses(poses);
// Inject new sprite
const string customLabel = "customHead";
spriteLibrary.AddOverride(customSprite, targetCategory, customLabel);
targetResolver.SetCategoryAndLabel(targetCategory, customLabel);
}
When I run the application and use an image I uploaded this comes up:
IndexOutOfRangeException: Index -1079236362 is out of range of '1' Length.
Unity.Collections.NativeArray`1[T].FailOutOfRangeError (System.Int32 index) (at <13e6546058e340ada820e34dce3b245e>:0)
Unity.Collections.NativeArray`1[T].CheckElementReadAccess (System.Int32 index) (at <13e6546058e340ada820e34dce3b245e>:0)
Unity.Collections.NativeArray`1[T].get_Item (System.Int32 index) (at <13e6546058e340ada820e34dce3b245e>:0)
UnityEngine.U2D.Animation.SpriteSkinUtility.Deform (Unity.Mathematics.float4x4 rootInv, Unity.Collections.NativeSlice`1[T] vertices, Unity.Collections.NativeSlice`1[T] boneWeights, Unity.Collections.NativeArray`1[T] boneTransforms, Unity.Collections.NativeSlice`1[T] bindPoses, Unity.Collections.NativeSlice`1[T] deformed) (at Library/PackageCache/com.unity.2d.animation@7.0.4/Runtime/SpriteSkinUtility.cs:249)
UnityEngine.U2D.Animation.SpriteSkinUtility.Deform (UnityEngine.Sprite sprite, UnityEngine.Matrix4x4 rootInv, Unity.Collections.NativeSlice`1[T] vertices, Unity.Collections.NativeSlice`1[T] tangents, Unity.Collections.NativeSlice`1[T] boneWeights, Unity.Collections.NativeArray`1[T] boneTransforms, Unity.Collections.NativeSlice`1[T] bindPoses, Unity.Collections.NativeArray`1[T] deformableVertices) (at Library/PackageCache/com.unity.2d.animation@7.0.4/Runtime/SpriteSkinUtility.cs:220)
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@7.0.4/Runtime/SpriteSkinUtility.cs:313)
UnityEngine.U2D.Animation.SpriteSkin.LateUpdate () (at Library/PackageCache/com.unity.2d.animation@7.0.4/Runtime/SpriteSkin.cs:311)