Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Resolved Is there a way to change a sprite but keep its bones?

Discussion in 'Animation' started by DaveKap, May 5, 2021 at 1:15 AM.

  1. DaveKap

    DaveKap

    Joined:
    Mar 6, 2013
    Posts:
    48
    I've read through this page: https://docs.unity3d.com/Packages/com.unity.2d.animation@6.0/manual/SpriteSwapIntro.html
    And it appears the only viable answer is to have the bones already copied and pasted to an already made sprite in order to do what I want.
    However, what I'm trying to do is create new sprites on the fly at runtime, via calls like this
    Code (CSharp):
    1. byte[] pngBytes = File.ReadAllBytes(Directory.GetCurrentDirectory() + "\\1.png");
    2. tex.LoadImage(pngBytes);
    3. Sprite sprite = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, tex.height), new Vector2(0.5f, 0.5f), 100.0f);
    4. rightArm.GetComponent<SpriteRenderer>().sprite = sprite;
    Which currently works great but completely obliterates the bones/animation mesh previously being used. I actually want to use the previous bones and animation mesh (which are simplified by the starting sprite being a solid square) because I don't care about accuracy of the sprites deformation, I just care that it deforms at all.

    So, is there a way to keep the bones intact on the game object, despite the sprite being changed?

    One more thing to add, I figured trying to manipulate the Sprite Skin component live would make this work, since that's where the bone transform information is stored and what gets overwritten when the sprite is swapped, however it appears this component doesn't allow adding bones while the game is running, despite removing them when I make the sprite swap. If there was some way to access this component, I'd try that, but I can't find a way. In script, all the bone information is read-only.

    Edit: Worth mentioning that swapping the sprite causes the Sprite Skin component to mention that the new sprite has no "bind poses" and looking this up, I cannot find anyone who has managed to generate bind poses at runtime.
     
    Last edited: May 5, 2021 at 1:31 AM
  2. DaveKap

    DaveKap

    Joined:
    Mar 6, 2013
    Posts:
    48
unityunity