- Home /
How to set thumbnail of assets from a script?
How would I like to know how to set an asset's texture from an editor script. Basically, I made my own rendering engine in DOTS and want to preview the prefabs with my own image and not the default blue cube. I tried
public override Texture2D RenderStaticPreview(string assetPath, UnityEngine.Object[] subAssets, int width, int height)
{
Debug.Log("Rendered Statics");
ConvertRenderer renderer = target as ConvertRenderer;
if (renderer == null || renderer.sprite == null)
return null;
Texture2D cache = new Texture2D(width, height);
EditorUtility.CopySerialized(AssetPreview.GetAssetPreview(renderer.sprite), cache);
return cache;
}
Doesn't seem to do anything. If you need a reference for what I am trying to do, there is an asset called "AssetIcons" that does what I am talking about.
Thanks!
I got my DOTS sprite rendering engine working perfectly, this is just one of the last hiccups. Basically, I want to preview prefabs from the asset window and just need to replace the default cube texture.
Bump, I know a lot of others wanted something similar to this and this should help them out if it gets answered.
Answer by enerology · Aug 16, 2020 at 05:18 PM
[CustomEditor(typeof(GameObject))]
public class ChangeObjectThumbEditor : Editor
{
public Texture2D texture;
public void OnEnable()
{
texture = Resources.Load<Texture2D>("DesertHeart");
}
public override Texture2D RenderStaticPreview(string assetPath, UnityEngine.Object[] subAssets, int width, int height)
{
Debug.Log("Rendered Statics");
GameObject renderer = target as GameObject;
if (renderer == null || texture == null)
return null;
Texture2D cache = new Texture2D(width, height);
EditorUtility.CopySerialized(AssetPreview.GetAssetPreview(texture), cache);
return cache;
}
}
Here is the code that allows you to change the thumbnail. It's extremely primitive, but should give you an idea of how to proceed.
Answer by Pangamini · Aug 15, 2020 at 11:27 PM
RenderStaticPreview only works for ScriptableObjects, not for gameObjects with a component (there could be more possible thumbnails coming from different scripts that way)
You just game me an idea to use "[CustomEditor(typeof(Gameobject))]" and it worked! It's still behaving weird but the basic idea behind what I wanted works.
Thanks!
Cool, I didn't think it'd work :-P Also I think that static preview is only visible for bigger size thumbnails, not when you are listing the assets with the smallest gui possible - not sure, maybe it changed
It also works for small assets. $$anonymous$$y textures show up in the object fields which are only 32x32 pixels. I am amazed that it worked so well and didn't need more code to work properly.
Your answer
![](https://koobas.hobune.stream/wayback/20220613004847im_/https://answers.unity.com/themes/thub/images/avi.jpg)