Setting custom icon for file extensions using ScriptedImporter

The ScriptedImporter class is a welcome change. However, I’d like to have assets with a particular extension to have an associated icon - instead of the default Unity3D one. Is that possible?

It would be nice to have that wrapped into the ScriptedImporter attribute similar to this …

// Custom importer for *.foo files
[ScriptedImporter(1, "foo", "Assets/Path/To/Foo/Icon.png")]
public class FooImporter : ScriptedImporter
{
    // Importer details ...
}
1 Like

I think you can do this through the asset’s custom editor by overriding Editor.RenderStaticPreview.

Hmmm … I have a ScriptedImporterEditor inherited class set up for such a thing but RenderStaticPreview is not invoked on it, even if I have overridden HasPreviewGUI to return true. Looking the web there seems to be some difficultly getting this to work. Does anyone know of a working example?

Quick update: If you make your “Main Asset” in your scripted import a type that is previewable (like, say, a texture) then that will be used as an icon. That’s not quite what I’d like but it is something that can work if your imported object (that you want treated as a prefab) falls into the category of supporting a preview.

You can click the icon in the inspector, and change what texture to use there. I think that sets the new icon on every asset of the same type. I might be wrong, though.

Do you mean this?

            var texture = Texture2D.blackTexture;
            ctx.AddObjectToAsset(localPath, rootObject);
            ctx.AddObjectToAsset("icon", texture);
            ctx.SetMainObject(texture);

Didn’t work for me (Unity 2019.2.1.f1)

Some time ago, Unity provided an overload to AddObjectToAsset that lets you pass in an icon.

So try something like this …

var icon = Texture2D.blackTexture; // Choose your icon here
ctx.AddObjectToAsset(localPath, rootObject, icon); // Pass your icon here
ctx.SetMainObject(rootObject); // your root object's icon is used in the project hierarchy

That worked! thank you very much

necroing this, sorry but i didn’t see the 1st question answered…

I use gimp with .xcf files.
They are unknown to unity and i don’t plan to make an XCF importer. I’d just like to have an (gimp) icon for the .xcf extension in the project window.

Did anyone achieve this ?

Answering to myself:

I managed to do an interresting thing:

[ScriptedImporter(1, "xcf")]
public class GimpImporter : ScriptedImporter {
    public override void OnImportAsset(AssetImportContext ctx)
    {

        Texture2D icon = Resources.Load<Texture2D>("The_GIMP_icon");

       
        ctx.AddObjectToAsset("truc_gimp",icon, icon);
    }
}

I’d prefer having something like:
ctx.AddObjectToAsset(“truc_gimp”,null, icon);

but it’s forbidden :frowning:

Maybe something like:
ctx.AddObjectToAsset(“truc_gimp”,DefaultAsset, icon);

as defaultasset seems empty but not null.
The thing is that i’m unable to fin the defaut asset :-/
If anyone got an idea, it would be appreciated :wink:

Okay…

Finally back there with an acceptable solution :slight_smile:

/// <summary>
/// A custom scripted importer that handles importing ".lua" files as text assets.
/// </summary>
[ScriptedImporter(1, "xcf")]
public class GimpImporter : ScriptedImporter {
    public override void OnImportAsset(AssetImportContext ctx)
    {
        TextAsset desc = new TextAsset("Fichier .XCF à ouvrir avec Gimp");
        Texture2D icon = Resources.Load<Texture2D>("The_GIMP_icon");

        Object nada = new Object();
     
        ctx.AddObjectToAsset("Fichier Gimp",desc, icon);
    }
}

With the same method i will make more visual icons for unknown unity types :smile:

This is much more sexy ( and visual ) than those blank squares^^

Happy unitying !

Re-necroing this…

Is there a simple way to modify an asset ( native like .blend or unhandled like .hop ) icon ?
Even for folders, it would be great !

bump ?

Admittedly this is a bit of a plug but since you brought up the idea of an XCF importer… we’ve just released exactly that on the asset store:

https://assetstore.unity.com/packages/tools/sprite-management/importer-for-gimp-image-files-259830

1 Like