Uploaded sprite isn't able to attach to 2D Bone

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)

It seems unlikely this has anything to do with sourcing the sprite off the net.

Can you inject a regular old random sprite with the above code?

1 Like

Using the InjectCustom() method, it works with any old random sprite. The issue only comes up when I try to use a sprite extracted from a Texture2D using UnityWebRequest

I found a thread that you replied to that I’ll try doing. It’s on making textures read/write enabled. Help on setting Read / Write Enabled flag on Texture
I’ll let you know if it works

Nothing changes even after passing the texture2d to the method

It seems like Sprites made through Sprite.Create() are causing the problem but I’m not sure how

Solved it!

I uploaded the PNG as a file instead of a texture and saved it to the Resources folder

IEnumerator UploadPNG(string path){
        UnityWebRequest www = UnityWebRequest.Get(path);
        yield return www.SendWebRequest();

        if(www.result == UnityWebRequest.Result.ProtocolError){
            Debug.Log(www.error);
        }
        else{
            byte[] bytes = www.downloadHandler.data;
            string fileName = Path.GetFileName(path);
            string fileName2 = Path.GetFileNameWithoutExtension(path);
            string filePath = Path.Combine(Application.dataPath, "Resources/PNGs/" + fileName);
            string filePath2 = Path.Combine("PNGs/" + fileName2);
            File.WriteAllBytes(filePath, bytes);
            Debug.Log("File saved to: " + filePath);

            AssetDatabase.Refresh();
            latestUpload = filePath;
            pngSwapper.LoadUploadedSprite(filePath2);
        }
    }

Then I proceed by loading it using Resource.Load after using AssetDatabase.Refresh