Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

Discussion in 'Scripting' started by Karl_Mamuyac, Mar 27, 2022.

  1. Karl_Mamuyac

    Karl_Mamuyac

    Joined:
    Aug 25, 2020
    Posts:
    6
    I'm uploading a sprite using UnityWebRequest with this:

    Code (CSharp):
    1. IEnumerator UpdatePNG(string path){
    2.         UnityWebRequest pngWR = new UnityWebRequest("file:///" + path);
    3.         DownloadHandlerTexture texDl = new DownloadHandlerTexture();
    4.         pngWR.downloadHandler = texDl;
    5.         yield return pngWR.SendWebRequest();
    6.         if (pngWR.result == UnityWebRequest.Result.Success) {
    7.             Texture2D t = texDl.texture;
    8.             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));
    9.  
    10.             pngSwapper.InjectCustom(s);
    11.         }
    12.     }
    and then using that sprite in a SpriteLibraryAsset like so:

    Code (CSharp):
    1. public void InjectCustom (Sprite customSprite){
    2.         // Duplicate bones and poses
    3.         string referenceLabel = targetResolver.GetLabel();
    4.         Sprite referenceHead =
    5.         spriteLibrary.GetSprite(targetCategory, referenceLabel);
    6.         SpriteBone[] bones = referenceHead.GetBones();
    7.         NativeArray<Matrix4x4> poses = referenceHead.GetBindPoses();
    8.         customSprite.SetBones(bones);
    9.         customSprite.SetBindPoses(poses);
    10.  
    11.         // Inject new sprite
    12.         const string customLabel = "customHead";
    13.         spriteLibrary.AddOverride(customSprite, targetCategory, customLabel);
    14.         targetResolver.SetCategoryAndLabel(targetCategory, customLabel);
    15.        
    16.     }
    When I run the application and use an image I uploaded this comes up:


    Code (CSharp):
    1. IndexOutOfRangeException: Index -1079236362 is out of range of '1' Length.
    2. Unity.Collections.NativeArray`1[T].FailOutOfRangeError (System.Int32 index) (at <13e6546058e340ada820e34dce3b245e>:0)
    3. Unity.Collections.NativeArray`1[T].CheckElementReadAccess (System.Int32 index) (at <13e6546058e340ada820e34dce3b245e>:0)
    4. Unity.Collections.NativeArray`1[T].get_Item (System.Int32 index) (at <13e6546058e340ada820e34dce3b245e>:0)
    5. 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)
    6. 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)
    7. 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)
    8. UnityEngine.U2D.Animation.SpriteSkin.LateUpdate () (at Library/PackageCache/com.unity.2d.animation@7.0.4/Runtime/SpriteSkin.cs:311)
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    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?
     
    Karl_Mamuyac likes this.
  3. Karl_Mamuyac

    Karl_Mamuyac

    Joined:
    Aug 25, 2020
    Posts:
    6
    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
     
  4. Karl_Mamuyac

    Karl_Mamuyac

    Joined:
    Aug 25, 2020
    Posts:
    6
  5. Karl_Mamuyac

    Karl_Mamuyac

    Joined:
    Aug 25, 2020
    Posts:
    6
  6. Karl_Mamuyac

    Karl_Mamuyac

    Joined:
    Aug 25, 2020
    Posts:
    6
    It seems like Sprites made through Sprite.Create() are causing the problem but I'm not sure how
     
  7. Karl_Mamuyac

    Karl_Mamuyac

    Joined:
    Aug 25, 2020
    Posts:
    6
    Solved it!

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

    Code (CSharp):
    1. IEnumerator UploadPNG(string path){
    2.         UnityWebRequest www = UnityWebRequest.Get(path);
    3.         yield return www.SendWebRequest();
    4.  
    5.         if(www.result == UnityWebRequest.Result.ProtocolError){
    6.             Debug.Log(www.error);
    7.         }
    8.         else{
    9.             byte[] bytes = www.downloadHandler.data;
    10.             string fileName = Path.GetFileName(path);
    11.             string fileName2 = Path.GetFileNameWithoutExtension(path);
    12.             string filePath = Path.Combine(Application.dataPath, "Resources/PNGs/" + fileName);
    13.             string filePath2 = Path.Combine("PNGs/" + fileName2);
    14.             File.WriteAllBytes(filePath, bytes);
    15.             Debug.Log("File saved to: " + filePath);
    16.  
    17.             AssetDatabase.Refresh();
    18.             latestUpload = filePath;
    19.             pngSwapper.LoadUploadedSprite(filePath2);
    20.         }
    21.     }
    Then I proceed by loading it using Resource.Load after using AssetDatabase.Refresh