Search Unity

Bug Why is autoRebind on SpriteSkin internal?

Discussion in '2D' started by tonytopper, May 25, 2023.

  1. tonytopper

    tonytopper

    Joined:
    Jun 25, 2018
    Posts:
    226
    This seems like a bug to me. Should be public so folks can modify this value programmatically.

    upload_2023-5-25_11-26-43.png

    The whole SpriteSkin, SpriteLibrary, and PSDImporter framework needs some love.
     
  2. MarekUnity

    MarekUnity

    Unity Technologies

    Joined:
    Jan 6, 2017
    Posts:
    204
    Thanks for bringing that to attention. May I understand what is your workflow? Are you dynamically instantiating objects and adding Sprite Skin component to them?
     
  3. tonytopper

    tonytopper

    Joined:
    Jun 25, 2018
    Posts:
    226
    I just have a use case where I have lots of pieces on each model that I need to have rebinding on. I don't want to manually check each piece to use rebinding each time we create a new model.

    I want to make an editor button (better yet if I could create an importer override) that just switches this on for all the right pieces on our models. I need a fast-import cycle to iterate model changes and scale the amount of clothing content.

    upload_2023-5-26_13-47-50.png

    I am trying to set up sprite swapping on some of these pieces to change a character's clothing.

    I need the alternative clothing to rebind when the sprite is swapped.

    P.S. As an aside, eventually I'll need a way to bake some of these pieces down into a single sprite, once the player customizes their character. Like combining head and ears into a single sprite for example.

    And then I'd like to use the auto weights generation on that sprite, but I imagine that's not currently available in runtime or even exposed as an API in the editor.
     
    EvOne and MarekUnity like this.
  4. MarekUnity

    MarekUnity

    Unity Technologies

    Joined:
    Jan 6, 2017
    Posts:
    204
    Thanks for the detailed description. I'm taking a note of that.

    In the meantime, I've written a small helper class that adds two public methods to the Sprite Skin:
    - SetAutoRebind
    - GetAutoRebind

    You can find it here.

    This shoud allow you to get and set the Auto Rebind property for your use case.

    Hope this helps for now!
     
    EvOne and tonytopper like this.
  5. tonytopper

    tonytopper

    Joined:
    Jun 25, 2018
    Posts:
    226
    Thanks, this is nice to hear. And it's very nice to have these extension methods.
     
    MarekUnity and EvOne like this.
  6. tonytopper

    tonytopper

    Joined:
    Jun 25, 2018
    Posts:
    226
    Unfortunately, the PSD asset importer overwrites this setting any time you update your PSB file. This is also a bug IMO.

    Any reason why this field is not public?
     
    EvOne likes this.
  7. tonytopper

    tonytopper

    Joined:
    Jun 25, 2018
    Posts:
    226
    To expand on the original question some more, why is setting rootBone not public? If I can edit something in the editor, I should be able to edit it with a script. Why is setting the boneTransforms not public?

    Creating rigging automation is harder than it should be IMO.

    We shouldn't have to rely on reflection hacks to do these things.
     
    EvOne likes this.
  8. tonytopper

    tonytopper

    Joined:
    Jun 25, 2018
    Posts:
    226
    I am running into an issue with the SetAutoRebind method.

    Here's the stack trace:

    UnassignedReferenceException: The variable m_SpriteRenderer of SpriteSkin has not been assigned.
    You probably need to assign the m_SpriteRenderer variable of the SpriteSkin script in the inspector.
    UnityEngine.U2D.Animation.SpriteSkin.get_sprite () (at ./Library/PackageCache/com.unity.2d.animation@9.0.3/Runtime/SpriteSkin.cs:109)
    UnityEngine.U2D.Animation.SpriteSkin.GetSpriteInstanceID () (at ./Library/PackageCache/com.unity.2d.animation@9.0.3/Runtime/SpriteSkin.cs:188)
    UnityEngine.U2D.Animation.SpriteSkin.CacheCurrentSprite (System.Boolean rebind) (at ./Library/PackageCache/com.unity.2d.animation@9.0.3/Runtime/SpriteSkin.cs:557)
    UnityEngine.U2D.Animation.SpriteSkin.set_autoRebind (System.Boolean value) (at ./Library/PackageCache/com.unity.2d.animation@9.0.3/Runtime/SpriteSkin.cs:120)
    System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <b89873cb176e44a995a4781c7487d410>:0)
    Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
    System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <b89873cb176e44a995a4781c7487d410>:0)
    System.Reflection.RuntimePropertyInfo.SetValue (System.Object obj, System.Object value, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] index, System.Globalization.CultureInfo culture) (at <b89873cb176e44a995a4781c7487d410>:0)
    System.Reflection.PropertyInfo.SetValue (System.Object obj, System.Object value, System.Object[] index) (at <b89873cb176e44a995a4781c7487d410>:0)
    System.Reflection.PropertyInfo.SetValue (System.Object obj, System.Object value) (at <b89873cb176e44a995a4781c7487d410>:0)
    SpriteSkinHelpers.SetAutoRebind (UnityEngine.U2D.Animation.SpriteSkin spriteSkin, System.Boolean autoRebind) (at Assets/Scripts/MadJoy/Character/SpriteSkinHelpers.cs:48)
    It looks like the animation package is missing a null check somewhere.

     
  9. MarekUnity

    MarekUnity

    Unity Technologies

    Joined:
    Jan 6, 2017
    Posts:
    204
  10. MarekUnity

    MarekUnity

    Unity Technologies

    Joined:
    Jan 6, 2017
    Posts:
    204
    They are not public because initially it was not designed to be generated from code. We are currently reevaluating which APIs can be safely made public to unblock use cases like yours.
    Ideally, in the future versions there will be a new high level API that's specifically designed for that purpose.
     
  11. tonytopper

    tonytopper

    Joined:
    Jun 25, 2018
    Posts:
    226
    The code in the latest version of SpriteSkin.cs needs some improvement in my opinion. It declares two structs, an enum, and the SpriteSkin class. Within the SpriteSkin class, there are two nested classes: Profiling and TransformData. This is too much in one file.

    It also sets m_SpriteRenderer in Awake which is proving to be painful. [ExecuteInEditMode] does not seem to be saving it from throwing a NullReferenceException during code executed in the editor. Wouldn't [ExecuteAlways] be better here anyway?

    This the the error I keep getting in SpriteSkin:

    NullReferenceException: Object reference not set to an instance of an object
    UnityEngine.U2D.Animation.SpriteSkin.get_sprite () (at ./Library/PackageCache/com.unity.2d.animation@10.1.0/Runtime/SpriteSkin.cs:187)
    UnityEngine.U2D.Animation.SpriteSkin.GetSpriteInstanceID () (at ./Library/PackageCache/com.unity.2d.animation@10.1.0/Runtime/SpriteSkin.cs:326)
    UnityEngine.U2D.Animation.SpriteSkin.CacheCurrentSprite (System.Boolean rebind) (at ./Library/PackageCache/com.unity.2d.animation@10.1.0/Runtime/SpriteSkin.cs:729)
    UnityEngine.U2D.Animation.SpriteSkin.set_autoRebind (System.Boolean value) (at ./Library/PackageCache/com.unity.2d.animation@10.1.0/Runtime/SpriteSkin.cs:202)
    MadJoy.MJ2D.Sprite.SpritePart`1[TLocation].CheckSpriteSkin () (at Assets/Scripts/MadJoy/Character/SpritePart.cs:202)

    Maybe a lazy loading pattern for the property for the SpriteRenderer property would be better.

    Looking at all this it wasn't a surprise my Prefabs that have SpriteSkin got jacked up when I moved to 2023. I'm going to have to redo a good bit of work.

    Thanks for listening to my feedback. Hope it helps.