- Home /
How to generate a Texture2D of the thumbnail of a prefab?
Hi, I'm quite new to Unity3D and I use C#. I'm making a inventory system. I want to know how can I use the thumbnail of a prefab and convert it into a Texture2D and attach it to each item in the inventory. I want to know if that is possible with scripts or plugins, please notify me. I dont mind if its a at run time script or not. Thank for reading.
i don't personally know of a way to do this on the fly, or if it is possible... i always created an icon in photo editing software.
Answer by roojerry · Jul 01, 2014 at 04:05 PM
Within the editor, you could write a script using AssetPreview.GetAssetPreview to get the preview Texture2D's from your assets, then encode those textures into PNGs and save them out to be used as assets in your game.
Other than that, I don't believe there is any quick or efficient way of doing this at runtime.
I have been trying to figure this out for the past 2 days, I was trying to get AssetPreview then Create.Asset and couldn't really figure out why I kept co$$anonymous$$g up with a blank item, unless I had it create a raw image. You sir have saved my day!
I have the same problem here. I try to create a list of Texture2D with AssetPreview.GetAssetPreview, but some work and some give NULL in return. Seems that the previews are not ready at the moment when I try to use them.
I tried this approach. I was able to get the texture2d using GetAssetPreview as well, but the problem is that after that I am not able to save it as a png file. I am getting the following error.
UnityException: Texture 'Prefab$$anonymous$$odel Icon' is not readable, the texture memory can not be accessed from scripts. You can make the texture readable in the Texture Import Settings. UnityEngine.Texture2D.EncodeToJPG () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/TextureBindings.gen.cs:371) AssetPackageCreator.export () (at Assets/Editor/AssetPackageCreator.cs:60)
After this I tried to make it readable as suggested in this SO answer How to make asset readable . This failed as well.
Is there any workaround for saving the thumbnail or preview to disk ?
@nixac I don´t know if you create a new png file but if you want to read/write into an existing one you will have to do the following. 1. Go under your image settings in the unity inspector 2. Change the texture type to advanced 3. Check "Read/Write Enabled" to true 4. Now you should be able to read/write into the file
I hope I could help :)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
GetAssetPreview Transparent background with higher resolution 0 Answers
Problem when loading texture from folder 0 Answers
Checking for null against Texture2D 0 Answers