- Home /
How can I create a custom AssetPreview for a prefab
The core of the question is, how can I customize the Texture that is returned by the AssetPreview.GetAssetPreview method?
A detailed explanation of my use case follows:
I have some prefabs that contain a components that creates a mesh from sprites info and some data. (its a tool similar to spine, spriter, etc)
Right now I am making a custom level editor, and in the palette tool I am showing prefabs with the AssetPreview, so using AssetPreview.GetAssetPreview (item.gameObject) I am able to show a nice icon for the prefabs.
This works perfectly for prefabs that have just regular sprites in them, like, trees, rocks, houses, grass decals, etc, but when I want to add a prefab that is a character using our custom mesh based component the preview is not generated. Even in the project view when using the resize slider, the sprite based prefabs show nice icons, but the character prefabs show just the default prefab icon.
I tried using the CustomPreview attribute and ObjectPreview class to generate a preview using OnPreviewGUI and OnInteractivePreviewGUI but even though they work fine in the preview view of the inspector, they fail to work on the project view, or the AssetPreview.GetAssetPreview method call.
Thanks for any information
I added a sample project to show the issue and the current solutions I am trying https://github.com/rgarat/AssetPreviewTest/
Answer by Adam-Mechtley · Dec 05, 2016 at 10:45 AM
Hi @rubeng! So this is not documented very well, but AssetPreview.GetAssetPreview () is actually looking for your definition of Editor.RenderStaticPreview () on the custom editor class defined for the target type in question.
Thanks for your help, I tried your solution but I couldn't make it work, the method never gets called, at least how I tried it. I added a test project https://github.com/rgarat/AssetPreviewTest/ so you can see if what I am doing is right, or I am making some obvious mistake.
One possibility: The code path here prefers to take a cached result, and will exit early if the target object is not dirty. I didn't dig into it in too much depth but I think the problem is it never generates an initial cached result the first time you create an object.
TLDR: For your object with AnotherCustomObjectComponent added to it, try assigning something to one of its serialized fields (e.g., sprite) and see if it then generates the preview texture. If so, it would be terrific if you could also file a bug report with the example scripts and steps to reproduce :)
I added an integer field to the CustomObjectComponent, and to AnotherCustomObjectComponent, changed the values, updated the prefabs, but it was still not being generated.
Also the palette window has buttons to set the AssetPreview.SetPreviewTextureCacheSize to 1, and to 1000 so you can also try to clear the cache that way (not sure if that really clears it or not)
Your answer
Follow this Question
Related Questions
layout serializable objects in unity 1 Answer
Change angle of camera in prefab preview 1 Answer
Drag from custom editor ObjectField 0 Answers
Cannot close Preview Scene 0 Answers