Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Tile Preview in a Custom Inspector

Discussion in '2017.2 Beta' started by snorkysnark, Aug 25, 2017.

  1. snorkysnark


    Oct 31, 2016
    I was wondering, is there a way to render an asset preview for a custom tile?
    I suppose I could create a custom inspector, override RenderStaticPreview, take the tile's sprite's texture (which would be tricky as it's a Multiple sprite) and multiply every pixel by the tile's color, but I think there should be an easier way since Unity already does all that for default tiles.
    Also, why not make a preview render automatically for any custom tile that derives from Tile, without the need of a custom incpector (it has a Sprite, what else do you need?) ?
    This is especially annoying because in my project I have a custom inspector for the Tile class, so I don't have default previews at all, and it becomes a lot harder to navigate in the assets.
    aka3eka likes this.
  2. snorkysnark


    Oct 31, 2016
    Ok, here's what I did for now. But this really should be a default feature.
    Code (CSharp):
    1. public override Texture2D RenderStaticPreview(string assetPath, Object[] subAssets, int width, int height)
    2.     {
    3.         Tile tile = AssetDatabase.LoadAssetAtPath<Tile>(assetPath);
    4.         if (tile.sprite != null)
    5.         {
    6.             Texture2D spritePreview = AssetPreview.GetAssetPreview(tile.sprite); // Get sprite texture
    8.             Color[] pixels = spritePreview.GetPixels();
    9.             for (int i = 0; i < pixels.Length; i++)
    10.             {
    11.                 pixels[i] = pixels[i] * tile.color; // Tint
    12.             }
    13.             spritePreview.SetPixels(pixels);
    14.             spritePreview.Apply();
    16.             Texture2D preview = new Texture2D(width, height);
    17.             EditorUtility.CopySerialized(spritePreview, preview); // Returning the original texture causes an editor crash
    18.             return preview;
    19.         }
    20.         return null;
    21.     }
    eses, SDriver, Psyboyo and 7 others like this.