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.
  2. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Why the prefab background preview in project view is black?

Discussion in 'Universal Render Pipeline' started by Necka_, Apr 16, 2020.

  1. Necka_

    Necka_

    Joined:
    Jan 22, 2018
    Posts:
    486
    Hello,

    I thought I was going crazy and that an asset changed my editor theme somehow. But no, it's because of SRP

    Here is the issue:


    In this video you can see me switching from SRP to Built-In and see how the prefab preview background in project panel changes (ignore the pink material that's not the issue here)

    In built-in the background is Grey. In SRP it's black.

    Having a black background is really bad. The grey background in Built-in is way better to preview the object because of the contrast.

    In this video it's a simple cube so yes, it's ok you can see and you know the shape of a cube.
    But when dealing with many prefabs it's becoming really annoying:
    upload_2020-4-16_13-25-3.png

    So either let us pick the color of our choice in the editor preference or just keep the grey background as in Built-in.

    The background is the same in free and pro editor theme (dark theme)
     
    VitN likes this.
  2. Elvar_Orn

    Elvar_Orn

    Unity Technologies

    Joined:
    Dec 9, 2019
    Posts:
    145
    Heyhey,
    These are good points. I will look into this.
     
    Necka_ likes this.
  3. Hannibal_Leo

    Hannibal_Leo

    Joined:
    Nov 5, 2012
    Posts:
    493
    Any Update on this?
     
  4. Refeas

    Refeas

    Joined:
    Nov 8, 2016
    Posts:
    192
    +1 on this, it's like this in URP/HDRP ever since the SRPs came out. Wonder why nobody really fixed it yet since it's easily reproducible just by using SRP and is workflow breaking.
     
  5. martint_unity

    martint_unity

    Unity Technologies

    Joined:
    Jan 11, 2018
    Posts:
    15
    Hi,
    We have a solution for this and this will allow users in URP to change the color to whatever they want. The default color will not be black, instead it will be the gray-ish color that has always been there.
     
  6. Refeas

    Refeas

    Joined:
    Nov 8, 2016
    Posts:
    192
    That's great news, thank you! Will this be available in URP 7.x as well?
     
  7. martint_unity

    martint_unity

    Unity Technologies

    Joined:
    Jan 11, 2018
    Posts:
    15
    Sorry, this will not be available for 7.x.x :(
    It will go into latest and then we see how far we backport it.
     
  8. tomraegan

    tomraegan

    Joined:
    Mar 28, 2016
    Posts:
    131
    I'm in 2020.1.6 and I can see the "Background for Prefabs" option in Preferences.

    Doesn't seem to do anything, however. All my prefabs retain their stubbornly black background.
     
  9. travis_unity746

    travis_unity746

    Joined:
    Nov 12, 2018
    Posts:
    14
    Hi @martint_unity Has this solution been implemented yet - I'm on 2020.1.16f and can't find any options that change the prefab preview window background colour?
    Travis
     
  10. tomraegan

    tomraegan

    Joined:
    Mar 28, 2016
    Posts:
    131
    Hi travis. The option exists...it's under Preferences/Colors and is called Background for Prefabs (I'm assuming that's the correct option).

    The option exists for me in 2020.1.16f. It just doesn't have any impact when I alter the color. My prefab backgrounds simply stay black.
     
  11. travis_unity746

    travis_unity746

    Joined:
    Nov 12, 2018
    Posts:
    14
    From what I've found, that's the setting for the background of the scene when you edit the prefab, not the background colour of the preview window...
     
  12. tomraegan

    tomraegan

    Joined:
    Mar 28, 2016
    Posts:
    131
    Oh, right!

    Well, in that case, like Travis, I'm keen on this option. Black backgrounds when dealing with hundreds of prefabs makes life trickier.
     
  13. travis_unity746

    travis_unity746

    Joined:
    Nov 12, 2018
    Posts:
    14
    Being able to change the lighting and/or background colour of the preview window would make choosing prefabs much easier - maybe make it the same as the skybox, or ambient colour from the environmental lighting settings, or a user selectable solid colour would be very helpful.
     
  14. Bordeaux_Fox

    Bordeaux_Fox

    Joined:
    Nov 14, 2018
    Posts:
    589
    Why not using the same preview window as Material with sun light toggle?
     
  15. tomraegan

    tomraegan

    Joined:
    Mar 28, 2016
    Posts:
    131
    I'm curious about what you mean.

    My problem is in the project folder, where all of my prefabs have black backgrounds. Most of my prefabs are grey, so visibility is tricky. If you could explain further what you mean, perhaps I've missed something obvious? It would not be the first time.
     
  16. Bordeaux_Fox

    Bordeaux_Fox

    Joined:
    Nov 14, 2018
    Posts:
    589
    I just mean Unity should make the 3d preview for prefabs like the same it is for materials.
     
  17. tomraegan

    tomraegan

    Joined:
    Mar 28, 2016
    Posts:
    131
    Oh! Thanks for clarifying. I thought maybe I was missing some editor trick.
     
  18. martint_unity

    martint_unity

    Unity Technologies

    Joined:
    Jan 11, 2018
    Posts:
    15
    Hi @travis_unity746 and everyone else here.
    Sorry the fix hasn't landed yet. Looked like it had some issues with the first implementation and we are looking into them. I will reply here when the fix is in a ready state.
     
    tomraegan likes this.
  19. Mads-Nyholm

    Mads-Nyholm

    Unity Technologies

    Joined:
    Aug 19, 2013
    Posts:
    163
    Note that the color 'Background for Prefabs' in Preferences/Color for is the background color for the Scene view when in Prefab Mode. See https://docs.unity3d.com/Manual/EditingInPrefabMode.html.
     
    Last edited: Dec 14, 2020
  20. martint_unity

    martint_unity

    Unity Technologies

    Joined:
    Jan 11, 2018
    Posts:
    15
    We have a PR in flight and I will comment here again when it has landed.
    It will make it so that the background color is the same in URP/HDRP as in builtin. So no changes in color.
    The ability to chose your own color will be in another PR.
     
    attaway likes this.
  21. Hannibal_Leo

    Hannibal_Leo

    Joined:
    Nov 5, 2012
    Posts:
    493
    PR means Preview Release?
     
  22. BattleAngelAlita

    BattleAngelAlita

    Joined:
    Nov 20, 2016
    Posts:
    391
    Asset store developers - hacks unity to support their asset on widest unity versions as possible
    Unity internal team of dozens super professionals - "you can change background color only on latest alpha of unity, with latest alpha of render-pipe that will be released in few years"
     
  23. attaway

    attaway

    Joined:
    Nov 12, 2014
    Posts:
    45
    Super excited for this. I've been wondering for awhile now why the background is black. Really difficult to see some prefabs
     
  24. GXMark

    GXMark

    Joined:
    Oct 13, 2012
    Posts:
    467
    There is also in project settings a place to set a prefab scene asset for the prefab. This seems to work sometimes and not others really weird. Prefab editing getting a proper lighting setup on it is an important aspect worth fixing.
     
  25. martint_unity

    martint_unity

    Unity Technologies

    Joined:
    Jan 11, 2018
    Posts:
    15
    The background should now be back to normal gray. So in the latest Beta this should be fine.
     
  26. Picmo

    Picmo

    Joined:
    Feb 27, 2015
    Posts:
    10
    Hi Martint,

    In what release is this fixed?

    Just tested it in the latest 2020.3.1 LTS and the background for asset thumbnails is still black.

    Thanks
    Phil
     
  27. GXMark

    GXMark

    Joined:
    Oct 13, 2012
    Posts:
    467
    A nice feature would be to have someway for the thumbnail inspector to simple create a thumbnail to the project with script code. Option to set the background opaque or transparency and size before save.
     
  28. Hannibal_Leo

    Hannibal_Leo

    Joined:
    Nov 5, 2012
    Posts:
    493
    You mean, something like this?

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Collections.Generic;
    4. using System.IO;
    5. using UnityEditor.Presets;
    6.  
    7. public class GeneratePreviewTexture : Editor
    8.     {
    9.         [MenuItem("Tools/Items/Generate Preview Texture")]
    10.         static void MakePreviewTexture()
    11.         {
    12.             if (Selection.gameObjects == null) return;
    13.             if (Selection.gameObjects.Length <= 0) return;
    14.  
    15.             foreach (GameObject go in Selection.gameObjects)
    16.             {
    17.                 GetAndSavePreviewTex(go);
    18.             }
    19.         }
    20.  
    21.         public static Texture2D GetAndSavePreviewTex(GameObject go)
    22.         {
    23.             Texture2D tex = null;
    24.  
    25.             var stopwatch = System.Diagnostics.Stopwatch.StartNew();
    26.  
    27.             while (tex == null)
    28.             {
    29.                 tex = AssetPreview.GetAssetPreview(go);
    30.                 System.Threading.Thread.Sleep(80);
    31.  
    32.                 // 1s = 1000ms --> 60000ms = 1min
    33.                 if (stopwatch.ElapsedMilliseconds > 60000)
    34.                 {
    35.                     throw new System.Exception("Could not create " +
    36.                         "preview texture for :" + go.name);
    37.                 }
    38.             }
    39.  
    40.             PixelsBlackToAlpha(tex, tex);
    41.  
    42.             string path = AssetDatabase.GetAssetPath(go);
    43.             string[] pathParts = path.Split('/');
    44.             pathParts[pathParts.Length - 1] = go.name + "_Icon.png";
    45.             path = "";
    46.             foreach (string pathPart in pathParts)
    47.             {
    48.                 path += pathPart + "/";
    49.             }
    50.             path = path.Remove(path.Length - 1);
    51.  
    52.             // must use File.WriteAllBytes instead of AssetDatabase
    53.             byte[] data = tex.EncodeToPNG();
    54.             File.WriteAllBytes(path, data);
    55.  
    56.             AssetDatabase.Refresh();
    57.  
    58.             // you can assign your own preset and copy it's GUID here
    59.             // or delete this line and the one below "preset.ApplyTo()"
    60.             string presetGUID = "1adfb305173a8684fbe9d95c4f162d23";
    61.             string presetPath = AssetDatabase.GUIDToAssetPath(presetGUID);
    62.             Preset preset = AssetDatabase.LoadAssetAtPath<Preset>(presetPath);
    63.  
    64.             AssetImporter importer = AssetImporter.GetAtPath(path);
    65.             preset.ApplyTo(importer);
    66.  
    67.             AssetDatabase.ImportAsset(path);
    68.             AssetDatabase.Refresh();
    69.  
    70.             Texture2D result = AssetDatabase.LoadAssetAtPath<Texture2D>(path);
    71.  
    72.             return result;
    73.         }
    74.  
    75.  
    76.         [MenuItem("Tools/Items/Texture To Alpha")]
    77.         static void TextureBlackToAlpha()
    78.         {
    79.             if (Selection.objects == null) return;
    80.             if (Selection.objects.Length <= 0) return;
    81.  
    82.             foreach (Object obj in Selection.objects)
    83.             {
    84.                 if (!obj) continue;
    85.                 Texture2D oldTex = obj as Texture2D;
    86.                 if (!oldTex) continue;
    87.                 Texture2D newTex = new Texture2D(oldTex.width, oldTex.height,
    88.                     TextureFormat.RGBA32, false);
    89.  
    90.                 PixelsBlackToAlpha(oldTex, newTex);
    91.  
    92.                 string path = AssetDatabase.GetAssetPath(obj);
    93.  
    94.                 // must use File.WriteAllBytes instead of AssetDatabase
    95.                 byte[] data = newTex.EncodeToPNG();
    96.                 File.WriteAllBytes(path, data);
    97.             }
    98.         }
    99.  
    100.         static void PixelsBlackToAlpha(Texture2D oldTex, Texture2D newTex)
    101.         {
    102.             for (int x = 0; x < newTex.width; x++)
    103.             {
    104.                 for (int y = 0; y < newTex.height; y++)
    105.                 {
    106.                     Color col = oldTex.GetPixel(x, y);
    107.                     if (col.r + col.g + col.b < 0.1f)
    108.                     {
    109.                         col = Color.clear;
    110.                     }
    111.                     newTex.SetPixel(x, y, col);
    112.                 }
    113.             }
    114.             newTex.Apply();
    115.         }
    116.     }
    This is our extremly simple way to get UI icons from preview images - does not work with dark objects and I will have to change it, once the background is grey xD

    But yea, thats it, tho I recommend using a more accurate approach by loading up a "foto" scene, adjusting the object there (if needed) and then save a full resolution image with transparent background.
    The script above is just a simple, hacky way that works in our project.
     
    Last edited: Apr 2, 2021
  29. PiterQ70

    PiterQ70

    Joined:
    Mar 3, 2015
    Posts:
    81
    Modified code. Get first pixel of generated preview, check neightbours if have same pixel like first to avoid clear pixel in object. Of course this is also not perfect solution but better ;)

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4. using System.IO;
    5. using System.Linq;
    6. using UnityEditor.Presets;
    7.  
    8. public class GeneratePreviewTexture : Editor
    9.     {
    10. [MenuItem("Tools/Generate Preview Texture", priority = 1999)]
    11.         static void MakePreviewTexture()
    12.         {
    13.             if (Selection.gameObjects == null) return;
    14.             if (Selection.gameObjects.Length <= 0) return;
    15.  
    16.             foreach (GameObject go in Selection.gameObjects)
    17.             {
    18.                 GetAndSavePreviewTex(go);
    19.             }
    20.         }
    21.  
    22.         public static Texture2D GetAndSavePreviewTex(GameObject go)
    23.         {
    24.             Texture2D tex = null;
    25.  
    26.             var stopwatch = System.Diagnostics.Stopwatch.StartNew();
    27.  
    28.             while (tex == null)
    29.             {
    30.                 tex = AssetPreview.GetAssetPreview(go);
    31.                 System.Threading.Thread.Sleep(80);
    32.  
    33.                 // 1s = 1000ms --> 60000ms = 1min
    34.                 if (stopwatch.ElapsedMilliseconds > 60000)
    35.                 {
    36.                     throw new System.Exception("Could not create " +
    37.                         "preview texture for :" + go.name);
    38.                 }
    39.             }
    40.  
    41.             PixelsToAlpha(tex, tex);
    42.  
    43.             string path = AssetDatabase.GetAssetPath(go);
    44.             string[] pathParts = path.Split('/');
    45.             pathParts[pathParts.Length - 1] = go.name + "_Icon.png";
    46.             path = "";
    47.             foreach (string pathPart in pathParts)
    48.             {
    49.                 path += pathPart + "/";
    50.             }
    51.             path = path.Remove(path.Length - 1);
    52.  
    53.             // must use File.WriteAllBytes instead of AssetDatabase
    54.             byte[] data = tex.EncodeToPNG();
    55.             File.WriteAllBytes(path, data);
    56.  
    57.             AssetDatabase.Refresh();
    58.  
    59.             // you can assign your own preset and copy it's GUID here
    60.             // or delete this line and the one below "preset.ApplyTo()"
    61.             string presetGUID = "1adfb305173a8684fbe9d95c4f162d23";
    62.             string presetPath = AssetDatabase.GUIDToAssetPath(presetGUID);
    63.             Preset preset = AssetDatabase.LoadAssetAtPath<Preset>(presetPath);
    64.  
    65.             AssetImporter importer = AssetImporter.GetAtPath(path);
    66.             preset.ApplyTo(importer);
    67.  
    68.             AssetDatabase.ImportAsset(path);
    69.             AssetDatabase.Refresh();
    70.  
    71.             Texture2D result = AssetDatabase.LoadAssetAtPath<Texture2D>(path);
    72.  
    73.             return result;
    74.         }
    75.  
    76.  
    77.         [MenuItem("Tools/Texture To Alpha", priority = 1)]
    78.         static void TextureBlackToAlpha()
    79.         {
    80.             if (Selection.objects == null) return;
    81.             if (Selection.objects.Length <= 0) return;
    82.  
    83.             foreach (Object obj in Selection.objects)
    84.             {
    85.                 if (!obj) continue;
    86.                 Texture2D oldTex = obj as Texture2D;
    87.                 if (!oldTex) continue;
    88.                 Texture2D newTex = new Texture2D(oldTex.width, oldTex.height,
    89.                     TextureFormat.RGBA32, false);
    90.  
    91.                 PixelsToAlpha(oldTex, newTex);
    92.  
    93.                 string path = AssetDatabase.GetAssetPath(obj);
    94.  
    95.                 // must use File.WriteAllBytes instead of AssetDatabase
    96.                 byte[] data = newTex.EncodeToPNG();
    97.                 File.WriteAllBytes(path, data);
    98.             }
    99.         }
    100.  
    101.         /// <summary>
    102.         /// Get first pixel color and replace all to alpha.
    103.         /// </summary>
    104.         /// <param name="oldTex"></param>
    105.         /// <param name="newTex"></param>
    106.    
    107. static void PixelsToAlpha(Texture2D oldTex, Texture2D newTex)
    108.         {
    109.             Color colorToReplace = oldTex.GetPixel(0, 0);
    110.             int[,] pixelsToReplace = new int[oldTex.width, oldTex.height];      
    111.             for (int x = 0; x < newTex.width; x++)
    112.             {
    113.                 for (int y = 0; y < newTex.height; y++)
    114.                 {
    115.                     bool[] neightbourSameColor = new bool[4] { false, false, false, false };
    116.                     neightbourSameColor[0] = x - 1 > 0 ? oldTex.GetPixel(x - 1, y) == colorToReplace : false;
    117.                     neightbourSameColor[1] = x + 1 < oldTex.width ? oldTex.GetPixel(x + 1, y) == colorToReplace : false;
    118.                     neightbourSameColor[2] = y - 1 > 0 ? oldTex.GetPixel(x, y - 1) == colorToReplace : false;
    119.                     neightbourSameColor[3] = y + 1 < oldTex.height ? oldTex.GetPixel(x, y + 1) == colorToReplace : false;
    120.                     pixelsToReplace[x, y] = oldTex.GetPixel(x, y) == colorToReplace && neightbourSameColor.Contains(true) ? 1 : 0;
    121.                 }
    122.             }
    123.             for (int x = 0; x < newTex.width; x++)
    124.             {
    125.                 for (int y = 0; y < newTex.height; y++)
    126.                 {                            
    127.                     Color col = pixelsToReplace[x,y] == 1 ? Color.clear : oldTex.GetPixel(x, y);
    128.                     newTex.SetPixel(x, y, col);
    129.                 }
    130.             }
    131.             newTex.Apply();
    132.         }
    133. }
     
    Last edited: Apr 5, 2021
  30. Hannibal_Leo

    Hannibal_Leo

    Joined:
    Nov 5, 2012
    Posts:
    493
    Interesting - I'd reuse the array to avoid creating a lot of garbage, slowing down the computation speed, or just get rid of it, because you could write
    Code (CSharp):
    1. pixelsToReplace[x, y] = 0;
    2.  
    3. if (oldTex.GetPixel(x, y) != colorToReplace) continue;
    4.  
    5. if (x - 1 > 0 &&
    6.     oldTex.GetPixel(x - 1, y) != colorToReplace) continue;
    7.  
    8. if (x + 1 < oldTex.width &&
    9.     oldTex.GetPixel(x + 1, y) != colorToReplace) continue;
    10.  
    11. if (y - 1 > 0 &&
    12.     oldTex.GetPixel(x, y - 1) != colorToReplace) continue;
    13.  
    14. if (y + 1 < oldTex.height &&
    15.     oldTex.GetPixel(x, y + 1) != colorToReplace) continue;
    16.  
    17. pixelsToReplace[x, y] = 1;
    Which would also quit early in many cases.

    But I don't get what this even does. It checks if neighbours have the same color - but it's not a flood fill alogorithm, so if the pixel at (0,0) is black and you have a black spot on your image (in the middle of your model), it will erease the black spot just like my implementation does, it will just leave a small rim of black pixels around the object.

    Not here to insult you in any way btw. , I am just trying to understand why you wrote it this way.
    (and promoting performance optimisation - even for editor tools xD )
     
    Last edited: Apr 7, 2021
  31. Tortuap

    Tortuap

    Joined:
    Dec 4, 2013
    Posts:
    118
    This very dark lighting is driving me crazy. I'm using legacy shaders, and the prefab preview is so dark I can't even recognize what I'm looking for.
     
  32. BattleAngelAlita

    BattleAngelAlita

    Joined:
    Nov 20, 2016
    Posts:
    391
    Example of an custom preview window
    FQMScMWy5f.png
     
  33. PiterQ70

    PiterQ70

    Joined:
    Mar 3, 2015
    Posts:
    81
    Hi.
    I had problem bcz your code clear every pixel if is black or whatever was set. My check if we have more pixels around this one so this should be background, should be cleared and yes you have right its not necessary to check every neighbors of pixel if one of the neighbors have same color loop should skip check next neighbors
    I have other solution for better background clearance. I need check this.
     
  34. tomraegan

    tomraegan

    Joined:
    Mar 28, 2016
    Posts:
    131
    Yep. Just upgraded to 2020.3.1 LTS and all prefabs have a black background in Project view. I am using URP.

    Would really love a fix for this. I hope someone in the Unity team has a need to try to build with this black background in place...you've got to experience it yourself to understand just how difficult it is.
     
  35. Necka_

    Necka_

    Joined:
    Jan 22, 2018
    Posts:
    486
    Hi @martint_unity , could you please check why this is not live? Your message is from January. In 2020.3.11f1 the background for prefabs is still Black in HDRP.
    This is really an annoying UX issue. While Unity improved a tons of UX in the past versions, this has not been touched so far in releases

    An update and a timeline would be really great
     
  36. Pie-Guy

    Pie-Guy

    Joined:
    Aug 5, 2019
    Posts:
    1
    Started looking for a solution to this issue - landed here. The only thing worse than not having a way to change this is having a way to change this that doesn't work.
     
    caio_gamedesign likes this.
  37. caio_gamedesign

    caio_gamedesign

    Joined:
    Mar 4, 2015
    Posts:
    2
    Please, fix this?! :)
    Or teach us how to really fix it??? :):):)
     
  38. caio_gamedesign

    caio_gamedesign

    Joined:
    Mar 4, 2015
    Posts:
    2
    upload_2021-7-15_14-32-12.png
    This image shows exactly what the problem is.
    Both are brand new installation, with default 3d project.
    I just created a Cube and add to assets folder as a prefab.
    Until version 2019 the preview from the 3d object looks fine.
    In version 2020 and after, the preview looks darken.
     
    KB_sky likes this.
  39. ThatRaincoat

    ThatRaincoat

    Joined:
    Jul 12, 2017
    Posts:
    27
    Also am searching for a solution to this. The black background makes it difficult to find prefabs.

    Using Unity 2020.3
     
  40. multimediamark

    multimediamark

    Joined:
    Mar 17, 2017
    Posts:
    30
    Having black background for material preview drives me insane.
     
  41. martint_unity

    martint_unity

    Unity Technologies

    Joined:
    Jan 11, 2018
    Posts:
    15
    Hi,
    It has been fixed in latest version of Unity and URP Package. And we are looking into if we can backport it. Sorry for the inconvenience this dark background might have caused.
     
  42. yowu_tencent

    yowu_tencent

    Joined:
    Nov 2, 2021
    Posts:
    1
    Hello Martint, just checking if this backport action has been done yet? Prefab preview is pretty dark in terms of visiblity. Would be nice if it's fixed and we could get an patch. Thank you!
    btw, we're using 2020.3.