bonahona/BonaDataEditor

How show image?

Closed this issue · 2 comments

First, this tool is amazing, congratulations.
I'd like to ask how to add an image like in your 'ability' screenshot (the second one), since there is no mention to it in the documentation

The editor uses Unity's inbuilt preview utility for the Icon image. If you used the editor to show a Prefab it would show the prefabs preview(normally a textured mesh). Normally Scriptable objects uses a plain boring Unity icon which is what I assume you are seeing. In order to select the image yourself you need to create a custom editor for the type you want to display and override its RenderStaticPreview() method.

This is an incomplete example of how the AbilityBase class has an editor to show a custom preview.
It assumed the AbilityBase has a field namned IconIMage of the type Sprite.

public override Texture2D RenderStaticPreview(string assetPath, Object[] subAssets, int width, int height)
 {
    try {
        var ability = target.To<AbilityBase>();
        if (ability.IconImage == null) {
            return base.RenderStaticPreview(assetPath, subAssets, width, height);
        }
        var result = new Texture2D(width, height);
        EditorUtility.CopySerialized(AssetPreview.GetAssetPreview(ability.IconImage), result);
        return result;
    } catch (System.Exception) {
        return base.RenderStaticPreview(assetPath, subAssets, width, height);
    }
}

I tried this and it worked, thank you.