Skip to content

Instantly share code, notes, and snippets.

@mukaschultze
Last active May 6, 2020 22:09
Show Gist options
  • Save mukaschultze/9381dda1a4905feea70a0a5adcb3065b to your computer and use it in GitHub Desktop.
Save mukaschultze/9381dda1a4905feea70a0a5adcb3065b to your computer and use it in GitHub Desktop.
Change Unity preview background to a nicer color!
using UnityEditor;
using UnityEngine;
namespace EnhancedHierarchy {
[InitializeOnLoad]
static class PreviewBackgroundColor {
private static readonly Color backgroundColor = new Color(1f, 0f, 1f); // Change this color
static PreviewBackgroundColor() {
Camera.onPreRender += (Camera cam) => {
if (cam && cam.name.Contains("Preview")) {
cam.backgroundColor = backgroundColor;
}
};
After.Frames(1, () => {
var texture = CreateTexture(backgroundColor);
GUIStyle preBackground = "PreBackground";
GUIStyle preBackgroundSolid = "PreBackgroundSolid";
preBackground.normal.background = texture;
preBackgroundSolid.normal.background = texture;
});
}
private static Texture2D CreateTexture(Color c) {
var texture = new Texture2D(16, 16);
for (var x = 0; x < texture.width; x++)
for (var y = 0; y < texture.height; y++)
texture.SetPixel(x, y, c);
texture.Apply();
texture.hideFlags = HideFlags.HideAndDontSave;
return texture;
}
}
}
@larssteenhoff
Copy link

This should change the preview under the inspector right?
I don't seem to get it to work, I added the script to the editor.
Should it show the preview change right away?

@mukaschultze
Copy link
Author

@larssteenhoff any errors in the console? I'd recommend putting the file inside the Enhanced Hierarchy folder, it uses the After utility from EH2.0.

And yes, it should change the inspector right away.

@larssteenhoff
Copy link

No errors in the console.
I added the script to the enhanced hierarchy / editor folder and it does not change the preview background for me

see the screenshots for my setup

Screenshot 2020-05-06 at 11 00 46
Screenshot 2020-05-06 at 11 04 54

@mukaschultze
Copy link
Author

@larssteenhoff, I updated it, it's now working somehow better. In Unity 2019 you need to slightly move the object to change the color, I honestly don't know why (Unity 2020 works fine).

iiiii

@larssteenhoff
Copy link

Thanks yes it works, and like you said only when interacting.

Perhaps do you know if it's possible to make the objects brighter in the preview? They are so dark like there is no light on them.

@mukaschultze
Copy link
Author

The lighting is hardcoded, there's no way of changing that. Maybe with shaders but that's an unknown world for me.

@larssteenhoff
Copy link

Ok thanks for letting me know, and for making this enhancement !
I will ask unity for a way to customise this part.

@larssteenhoff
Copy link

And if you ever find out a way to make the preview background work without interaction in 2019.3 please let me know :)

@larssteenhoff
Copy link

larssteenhoff commented May 6, 2020

I found a nice video that is somehow related to this
https://www.youtube.com/watch?v=TPGFnk8guL8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment