Search Unity

[SOLVED] OnPostProcess Sprites 'Missing'

Discussion in 'Immediate Mode GUI (IMGUI)' started by ArborealGames, Jan 26, 2016.

  1. ArborealGames

    ArborealGames

    Joined:
    Aug 8, 2012
    Posts:
    6
    Hi all,


    I am using Unity 5.1.1f

    I have programmed an AssetPostprocessor using unityscript.
    In the 'OnPostprocessTexture()' override it calculates and assigns a spritesheet using SpriteMetaData arrays to the texture, which is all fine.

    In the 'OnPostprocessSprites( sprites:Sprite[] )' override it attempts to assign the sprites in the 'Sprite[]' argument to a prefab gameobject's public serialized properties.

    This is where it goes a bit squiff.

    When I 'Debug.Log' the sprites that I assign to the prefab they echo out 'aSpriteName (UnityEngine.Sprite)' as expected. This suggests to me that they exists and I have a reference to them.

    When I then attempt to assign this sprite to my prefab gameobject's serialized property, it just says 'Missing (Sprite)' in the respective field.

    I wonder if perhaps there's some finalisation method I need to call on the assetdatabase or the assetimporter that I may have missed, or if doing this in 'OnPostprocessSprites' is the wrong thing to do...

    Any ideas? Thank you for your time!


    EDIT

    OK, I have boiled it down as much as possible.
    If all I do is this in the PostProcess Sprites event:

    Code (JavaScript):
    1. public class SpriteImporter extends AssetPostprocessor{
    2.  
    3.     public function OnPostprocessSprites( texture: Texture2D , sprites: Sprite[] ){
    4.  
    5.         Debug.Log( sprites[0] );
    6.        (
    7.             AssetDatabase.LoadAssetAtPath(
    8.                AssetDatabase.GUIDToAssetPath(
    9.                   AssetDatabase.FindAssets( "t:Object", [ "Assets/PrefabFolder" ] )[ 0 ]
    10.                  ), typeof( Object )
    11.                ) as GameObject
    12.          ).GetComponent( CustomMonobehaviourClass ).sprite= sprites[0];
    13.  
    14.    }
    15.  
    16. }
    ...then the sprite is assigned but is 'Missing(Sprite)' in the inspector, and thereby null during play.
    However, the debug part spits out:

    'spriteName (UnityEngine.Sprite)'

    , and if I look at the sprites they exist as expected under the texture as having imported correctly.


    UPDATE

    I made an editor menu item and had that assign the sprites and it works, and using the same logic inside the OnPostProcessSprites event seems to still fail, so I am lead to believe that OnPostProcessSprites is not truly after the sprites have finished processing... I will try using the OnPostProcessAllAssets event instead and see what happens...


    FIXED

    Yeah, as expected, doing it in OnPostprocessAllAssets() instead of OnPostprocessSprite works wonderfully. Just a really poor choice of wording for 'OnPostprocessSprite' then I suppose, oh well, now we know... hope this helps others.
     
    Last edited: Jan 31, 2016
  2. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    Not 100% sure, it's hard to know without seeing the actual code; i think that prefabs behave a bit differently and should be handled in slightly different ways.

    BTW: you can refresh the AssetDatabase (AssetDatabase.Refresh), you can check if that helps by calling it after doing your processing although i don't think it will help in any way in this case..
     
  3. ArborealGames

    ArborealGames

    Joined:
    Aug 8, 2012
    Posts:
    6
    Yeah, AssetDatabase.Refresh() doesn't make any difference.
    I'll have to experiment! :D
    It's just the documentation on this stuff is a bit ropey at best...

    Have updated my initial post with the code and progress
     
    Last edited: Jan 30, 2016
  4. drchilitopicoso

    drchilitopicoso

    Joined:
    Apr 18, 2013
    Posts:
    1
    Hi fvfwvwv, I know this is many months later, but i wanted to thank you for testing this stuff, you really helped me. OnPostprocessSprite is not really POST per se, which it was super aggravating to me. It seems that the Texture and Sprites you get as parameters are not the final ones, but temp objects which settings would be copied to the already existing ones(if they do exist, otherwise it just saves those.)

    In my case i wanted to set some values automatically to tiles when changing the texture of the sprites, to make it fast and less error prone.

    So for those trying to do something like that, use OnPostprocessSprite to catch changes you care and set them for sure in OnPostprocessAllAssets. Use a flag so you don't do it always.

    Btw, if you want to get the REAL texture being modified(not the temp one) use AssetDatabase.LoadAssetAtPath<Texture2D>(assetPath)

    I swear dude, forums is the documentation at this point. Sometimes I don't even check the docs, i just straight up open ILSpy and follow the rabbit hole, seems more complete.
     
  5. ayushvora

    ayushvora

    Joined:
    Jul 1, 2016
    Posts:
    12
    DAMMIT! Almost 2 hours wasted on this o_O
    Someone from Unity, the doc for OnPostProcessSprites REAAALLLY needs to say that the sprite will be missing o_O
     
    assertor likes this.