Search Unity

Question How do you use AssetPreview.GetAssetPreivew(...) with UIToolkit?

Discussion in 'UI Toolkit' started by MechaWolf99, Aug 31, 2020.

  1. MechaWolf99

    MechaWolf99

    Joined:
    Aug 22, 2017
    Posts:
    294
    I am trying to use AssetPreview.GetAssetPreivew(...) to set an Image, but it returns null. I assume this is because it is asynchronous, and so the Image element is never updated. But for the life of my I cannot figure out how to get it to work properly. Any ideas?
     
  2. JuliaP_Unity

    JuliaP_Unity

    Unity Technologies

    Joined:
    Mar 26, 2020
    Posts:
    698
  3. MechaWolf99

    MechaWolf99

    Joined:
    Aug 22, 2017
    Posts:
    294
    Hi, I tried using the scheduler, with Until, and AssetPreview.IsLoadingAsset. But that didn't seem to work.
    Code (CSharp):
    1.  schedule.Execute(() =>
    2.         {
    3.             _icon.image = AssetPreview.GetAssetPreview(targetObject);
    4.         }).Until(() => !AssetPreview.IsLoadingAssetPreview(InstanceID));
    I changed
    _icon.image = AssetPreview.GetAssetPreview(targetObject);
    to use
    _icon.image = AssetDatabase.GetCachedIcon(AssetDatabase.GetAssetPath(InstanceID));
    instead and that seemed to work. At least for now. I think that AssetPreview.GetAssetPreview makes a new request every time it is called, so IsLoadingAssetPreview is always true in my first attempt. I think if I need more than 30 icons at a time this may start to break. I'm not sure though.
    This also doesn't feel like a very clean way to go about it. (oh, and for context I am making an editor window sort of like the project browser, so a large amount of icons would not be unreasonable to have at once.)
     
  4. MechaWolf99

    MechaWolf99

    Joined:
    Aug 22, 2017
    Posts:
    294
    Okay, update. For some reason when opening Unity, if the window with the above bit of code is already open, it never stops executing until the window is closed.
     
  5. JuliaP_Unity

    JuliaP_Unity

    Unity Technologies

    Joined:
    Mar 26, 2020
    Posts:
    698
    That sounds like a bug, could you report it through Unity (Help > Report a bug)? Thanks!

    Also are you still having issues you need help with? From your previous message I had understood you had something that was working for you.
     
  6. MechaWolf99

    MechaWolf99

    Joined:
    Aug 22, 2017
    Posts:
    294
    Sorry for the late replay, thanks for getting back to me. I will see if I can reproduce it properly and submit a report.

    I got it working. I use GetAssPreview for objects like prefabs and materials, and GetMiniThumbnail for everything else and that seems to work fine! No need for Execute or anything.
     
    marcin_great likes this.
  7. DerDicke

    DerDicke

    Joined:
    Jun 30, 2015
    Posts:
    292
    I'm writing a tool to auto create icons for prefabs in an EditorWindow and having the same issues. Sometimes it works just perfectly, sometimes it crashes Unity, sometimes I get all kind of errors. How did you solve it?
     
  8. MechaWolf99

    MechaWolf99

    Joined:
    Aug 22, 2017
    Posts:
    294
    Sounds like you have other things going on or found a big because it should never be crashing Unity.
    If memory serves I ended up just using an IMGUIContainer, though this was party because I ended up doing a custom preview generation system as well.
     
  9. DerDicke

    DerDicke

    Joined:
    Jun 30, 2015
    Posts:
    292
    Thanks for answering. Did my own version. It works like a charm.