Search Unity

Question Adding custom metadata to sprites, as an 'import workflow'

Discussion in 'General Graphics' started by Waarten, Feb 13, 2023.

  1. Waarten

    Waarten

    Joined:
    Aug 24, 2018
    Posts:
    38
    An artist may set a "Custom Pivot" for a sprite. But I'd like to have artists set additional data for sprites. Specific 'hotspots', width of the 'base' of the sprite, things like that.

    I realize that I can make some ScriptableObject or some sort of data file for this, then have someone bundle every piece of data for each sprite. But ideally, I'd like it to be "tightly coupled" to the sprite. In the same sense that the "Sprite" is tightly coupled with the "Texture" asset. One Sprite has (or is extended with) one set of my custom metadata. It's a 1:1 relationship and should ideally be required, even.

    Additionally, I want it to be part of the import flow. When an artists adds new pngs to the asset folder, it's easy to see that the next step is selecting a texture type and then set things like the pivot. Likewise, during this same step, I would like to have the artist provide this 'additional custom metadata'.

    My initial thoughts were that I would create something that would add stuff into the meta file (because that is where pivots are stored). But I'm not sure how I can do that, and how I can read back from it from code.

    I have no idea if such a thing is possible, and I hope someone can point me in the right direction.

    Thanks a lot!
     
    fmoo likes this.
  2. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    666
    I've only seen this done for models but might also work for sprites.

    You can write an asset preprocessor which automatically adds new components to the prefab. However, the UI is a bit buggy. You sometimes have to select the asset twice for the component to show up in the inspector.

    Maybe somebody else can give you a better explanation.
     
  3. Waarten

    Waarten

    Joined:
    Aug 24, 2018
    Posts:
    38
    Cool, thanks.

    I've also looked further into this. Like c0d3_m0nk3y mentioned, it seems there are a few ways to hook custom logic into the importing process.

    However, I'm still not sure _where_ to put additional data. As mentioned, I'd rather _not_ make a separate asset 'next to' the original one, because then I'd need them to reference each other and I get into the problem of keeping that data synchronized. So ideally, I'd save the data _within_ the same resource (the .meta file?).

    I've found the AssetImporter.UserData field, which may be there for this use case. But I'm not sure how to read it properly/efficiently at runtime.

    Any tech artists / tooling programmers with more ideas?
     
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,413
    if nothing else, could at into filename, headsprite_8_4.png
    and can parse those values inside processor.

    of course its annoying to update,
    unless you can get that filename x,y values automatically from your painting application..

    other options, export as photoshop layers,
    i remember seeing some photoshop importers that allow you to work with layers..
    so in theory, can check some specific layer, for single pixel that marks pivot position.
     
  5. Waarten

    Waarten

    Joined:
    Aug 24, 2018
    Posts:
    38
    Thanks mgear,

    To be clear, I'm at this point talking arbitrary data, so not 'just' a pivot. So perhaps a few floats determining a few scales, weights or widths or whatever, maybe a string of points for some geometric information. Maybe some sort of enum to specify something else.

    Your suggestions would still work indeed, but, like you mentioned also, perhaps more as a 'last measure'.
     
  6. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    666
    What I was trying to say, you can make a new MonoBehaviour, add all the data fields you need and then add it to the source prefab automatically with an asset preprocessor. Then the data fields of the new component should show up when importing the asset since "model" assets look like a prefab when importing.

    The disclaimer is, I don't know if this also works for sprites.
     
  7. c0d3_m0nk3y

    c0d3_m0nk3y

    Joined:
    Oct 21, 2021
    Posts:
    666
    Unfortunately, OnPostProcessSprites doesn't give you a game object, so not 100% sure this will work for sprites the same way.

    upload_2023-2-16_9-21-14.png

    PS: Just noticed that the inspector doesn't allow you to edit the component values, so maybe this is not the way to go.

    PPS: You can edit them on the imported prefab, so it kinda works but not great. The UserData field might be better suited for the job but haven't tried that.
     
    Last edited: Feb 16, 2023
  8. Waarten

    Waarten

    Joined:
    Aug 24, 2018
    Posts:
    38
    Hmm, right. I was confused because my workflow doesn't look anything like that. Like you're saying, models look like prefabs when importing, but there's no analog for this when using with sprites, like you also said.

    Thanks again, I'll post here when I've found a proper solution.
     
    fmoo likes this.