Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Muskelmann · Jun 27, 2013 at 06:18 PM · c#texturemissingpreviewgetassetpreview

Getting None or Missing Texture from AssetPreview.GetAssetPreview

Hi, i have a strange problem. The first few objects of my gameobject array which contains the asset previews from the specified folder return none or a missing texture. And every second time building the scene the textures from Element 10 to 20 are none or missing and the textures that were none before are assigned properly.

My code:

 using UnityEngine;
 using System.Collections;
 using UnityEditor;
 
 public class Items : MonoBehaviour {
     
     public GameObject[] item;
     
     public Texture2D[] itemIcon;
 
     void Start () {
         
         itemIcon = new Texture2D[item.Length];
         
         for(int i = 0; i < item.Length; i++)
             itemIcon[i] = AssetPreview.GetAssetPreview(item[i]);

     }
 }

alt text

alt text

I'm using Unity 4. Sorry for my english :P

unbenannt0.png (9.0 kB)
unbenannt.jpg (50.3 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hololabs · Nov 05, 2013 at 04:48 AM 0
Share

It looks like a bug in Unity. See duplicate here: http://answers.unity3d.com/questions/259608/editorutiltiygetassetpreviewobject-asset.html

4 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by undead-steve · Dec 02, 2013 at 11:40 PM

GetAssetPreview is really running asynchronously in the background, and if the preview you want is not available when you ask for it you will get a null instead of a texture. You can work around this by polling the preview multiple times:

    int counter = 0;
    Texture2D thumbnail = null;
    while (thumbnail == null && counter < 75)
    {
         thumbnail= AssetPreview.GetAssetPreview(o);
         counter ++;
         System.Threading.Thread.Sleep(15);
     }

However this does give a lag time of as much as 1 second for each preview. On my machine I rarely see more than 30 or 45 ms but disk access, processor speed, and other hidden stuff will all affect that. Use with caution.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Heikki-Hiltunen · Jul 14, 2017 at 03:04 PM 0
Share

I tried this code and it didn't solve the problem, just jammed unity pretty badly. For many objects GetAssetPreview never seems to return anything, no matter how long you wait. I think GetAssetPreview is simply buggy.

avatar image
1

Answer by DiebeImDunkeln · Feb 01, 2015 at 01:21 PM

After several hours of research I found a way to do it: Run the while loop until the texture is not null and then continue.

here a small example:

     Texture2D bTexture =  null;
     while(bTexture==null)
     {
                                 
         bTexture = AssetPreview.GetAssetPreview(go);
                                                 
         Thread.Sleep(80);
     }
                             
     if(bTexture != null)
     {
         bTexture.mipMapBias = -1.5f;
         bTexture.Apply();
         byte[] data = bTexture.EncodeToPNG();
         string pathAndName = path+go.name+".png";
         File.WriteAllBytes(pathAndName, data);
                                 
     }    
 
 
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dimmduh · Apr 04, 2016 at 08:09 AM 0
Share

Thank you, man! You saved my time, but I already lost 2 hours.

avatar image
0

Answer by rsodre · Jul 11, 2017 at 06:56 PM

I found that the texture from GetAssetPreview() is not always available. I'm copying it to a local texture once I get it.

             Texture2D tx = AssetPreview.GetAssetPreview(myPrefab);
             if (tx == null)
                 _tex = null;
             else
             {
                 _tex = new Texture2D(tx.width,tx.height);
                 _tex.SetPixels(tx.GetPixels());
                 _tex.Apply();
             }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Pazzaar · Dec 02, 2021 at 06:52 PM

For 2021, I think the best way to do this is probably set off a Coroutine to just query until an image is found. In my code I'm looking to set the thumbnail of some canvas objects but they aren't setting.

I did this and it seems to work.

 IEnumerator SetThumbnail()
 {
     Texture2D thumbnail = null;

     while (thumbnail == null)
     {
         thumbnail = AssetPreview.GetAssetPreview(myPrefab);
         yield return new WaitForSeconds(.5f);
     }

     var sprite = Sprite.Create(thumbnail, new Rect(0.0f, 0.0f, thumbnail.width, thumbnail.height), new Vector2(0.5f, 0.5f), 100.0f);
     SetImage(sprite);
 }


In my Start() function I just fire it off using:

  StartCoroutine(SetThumbnail());
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Texture does not appear when applied on the thumb and slider of vertical slider 0 Answers

C# WWW texture and webplayer 0 Answers

load all png files as resource but NOT from resource folder 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges