Skip to content

Instantly share code, notes, and snippets.

@instance-id
Last active July 5, 2020 08:20
Show Gist options
  • Save instance-id/3046ad29bd8782a3b3398cd9ae48ca92 to your computer and use it in GitHub Desktop.
Save instance-id/3046ad29bd8782a3b3398cd9ae48ca92 to your computer and use it in GitHub Desktop.
Legit/Legal Unity 2019.3.0bx beta dark theme one click converter
// --- instance.id ------------------------------------------------------------
// Thanks to TheZombieKiller and Peter77 for creating this
// https://forum.unity.com/threads/editor-skinning-thread.711059/#post-4785434
// Tested on Unity 2019.3.0b1 - 95% Dark mode Conversion
// Example Screenshot - https://i.imgur.com/9q5VPQk.png
// (Note - Once I ran this, I had to hit play and then it took effect)
// ----------------------------------------------------------------------------
using System;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEngine.UIElements;
using UnityEditor;
using UnityEditorInternal;
public static class DarkTheme
{
[MenuItem("Theme/Init Dark")]
static void Init()
{
foreach (var sheet in Resources.FindObjectsOfTypeAll<StyleSheet>())
{
if (ContainsInsensitive(sheet.name, "Dark"))
continue;
if (!ContainsInsensitive(sheet.name, "Light"))
{
InvertColors(sheet);
continue;
}
var dark = null as StyleSheet;
var path = ReplaceInsensitive(AssetDatabase.GetAssetPath(sheet), "Light", "Dark");
var name = ReplaceInsensitive(sheet.name, "Light", "Dark");
if (path == "Library/unity editor resources")
dark = EditorGUIUtility.Load(name) as StyleSheet;
else
dark = AssetDatabase.LoadAssetAtPath<StyleSheet>(path);
if (!dark)
InvertColors(sheet);
else
{
string oldName = sheet.name;
EditorUtility.CopySerialized(dark, sheet);
sheet.name = oldName;
}
}
InternalEditorUtility.RequestScriptReload();
InternalEditorUtility.RepaintAllViews();
}
static void InvertColors(StyleSheet sheet)
{
var serialized = new SerializedObject(sheet); serialized.Update();
var colors = serialized.FindProperty("colors");
for (int i = 0; i < colors.arraySize; i++)
{
var property = colors.GetArrayElementAtIndex(i);
Color.RGBToHSV(property.colorValue, out var h, out var s, out var v);
property.colorValue = Color.HSVToRGB(h, s, 1 - v);
}
serialized.ApplyModifiedProperties();
}
static string ReplaceInsensitive(string str, string oldValue, string newValue)
{
return Regex.Replace(str, Regex.Escape(oldValue), newValue.Replace("$", "$$"), RegexOptions.IgnoreCase);
}
static bool ContainsInsensitive(string str, string find)
{
return str.IndexOf(find, StringComparison.OrdinalIgnoreCase) != -1;
}
}
@PatHightree
Copy link

PatHightree commented Oct 9, 2019

Thanks a lot for sharing this.
I was toying around with the package manager and hosting packages on github.
So as an exercese, I packaged your code (with your credentials) and put it up over here, https://github.com/PatHightree/DarkTheme
Couldn't fork a gist into a repo, so I put it in a fresh repo.
Hope you don't mind.

@instance-id
Copy link
Author

Sure thing, no worries. That just makes it easier for folks to use it, so it's all good.

@PatHightree
Copy link

Great, thanks!

@jrapoport
Copy link

Hey, this is great! Mod'ed it to automatically switch to dark theme on launch:
https://gist.github.com/jrapoport/be65c76e9f2cbabbd84f336d5bc1d902

@jrapoport
Copy link

p.s. I forked @PatHightree's package and fixed it up, but ultimately ran into errors loading it that way. I'm not super familiar with custom Unity packages and didn't have time to figure out the issue. I suspect it has something to do with when the package gets executed vs. a normal script and the state of the editor, but who knows.

@ShapeGroup
Copy link

Cool but it doesn't work very well for me.

At the first start the ui become completely black, on the second click instead they go to the dark theme.

@instance-id
Copy link
Author

Yeah, it seems that it has to go through a reload, which was why I mentioned in the comments "Note - Once I ran this, I had to hit play and then it took effect". You can try one of the repos that the other folks in the comments have made and see if that helps any.

@ShapeGroup
Copy link

No, it seems that the same thing happens to everyone.
I will try to work on it in the meantime I hope that someone will discover how to fix it because it is really useful.

@instance-id
Copy link
Author

Hopefully, soon it won't matter. When asked about the dark theme on twitter, Unity replied with this:
So hopefully that means what I think it means, lol.

@instance-id
Copy link
Author

And actually, this was just posted 6 hours ago, it looks like.

@ShapeGroup
Copy link

Ok, as I experiment with what we are waiting for and see what happens.
For the moment the important thing is not to remain blinded, it is already so hahaha

@TheBoje
Copy link

TheBoje commented Jan 30, 2020

Hello there,
I'm trying to understand how to setup this script. I can't manage to get this one working.
Thanks

@breitnw
Copy link

breitnw commented Feb 2, 2020

@TheBoje,
Once you've added the script to your Unity project, you should be able to activate the dark theme from the Unity menu bar. Go to the "Theme" dropdown and click "Init Dark." Hopefully this works for you!

@instance-id
Copy link
Author

Yes, in the root of your project explorer, if you don't have one, create a folder called "Editor", in there make a new script called DarkTheme.cs and then paste this code into it and have Unity refresh. Then the new menu item will appear in the menu bar.

@TheBoje
Copy link

TheBoje commented Feb 2, 2020

Thanks both @lestinkemonke @instance-id , however I figured out a couple hours later and forgot about this post. Btw thanks for saving my eyes, I justed used it during the Global Gam Jam I took part in.

@JammSpread
Copy link

JammSpread commented Mar 6, 2020

Hey you should replace InternalEditorUtility.RequestScriptReload(); with UnityEditor.EditorUtility! Since it is deprecated. :) Love this dark theme! 👍

@amazedsaint
Copy link

If this is not working for you, just change InternalEditorUtility.RepaintAllViews(); in the script to InternalEditorUtility.SwitchSkinAndRepaintAllViews(); - and you should be fine

@SaniveK
Copy link

SaniveK commented May 2, 2020

If this is not working for you, just change InternalEditorUtility.RepaintAllViews(); in the script to InternalEditorUtility.SwitchSkinAndRepaintAllViews(); - and you should be fine

Can't seem to get it working with Unity 2019.3.12f1
I adds the "Theme" menu option > Init Dark ... but it doesn't seem to do anything even after Play =(
Any tips?

@instance-id
Copy link
Author

@SaniveK
Copy link

SaniveK commented May 2, 2020

https://github.com/xeleh/enhancer

THANK YOU !!! (my eyes thank you as well)

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