- Home /
Uploaded sprite can't set bones and poses
I'm trying to get a sprite by uploading a texture using this script :
IEnumerator UpdatePNG(string path){
UnityWebRequest pngWR = new UnityWebRequest("file:///" + path);
DownloadHandlerTexture texDl = new DownloadHandlerTexture(true);
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);
pngSwapper.InjectCustom(s);
}
}
After uploading, the method calls InjectCustom();
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);
}
If the sprite used by InjectCustom() is already in Unity, it'll work and attach itself to the bones but if I use the uploaded sprite it returns this error
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)
Answer by Karl_Mamuyac · Mar 30 at 02:31 PM
Solved it! I uploaded the png as a file instead and then loaded it through Resource.Load() after calling AssetDatabase.Refresh()
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);
}
}
Your answer
Follow this Question
Related Questions
Sending image data through SyncVar doesn't work (Mirror Networking) 0 Answers
Texture2D.PackTextures() - maximumAtlasSize 1 Answer
Draw tilemap with Texture2D.SetPixels: Performance and Memory? 0 Answers
Polygon Collider 2D Not Precise Enough 0 Answers
Loading texures from client-owned PNG files in Mirror 0 Answers