- Home /
Unity Editor Playmode Tint Change by Script
Hello,
would anyone happen to know if there is a way to programmatically change the Editor playmode tint, found in Preferences > Colors > Playmode Tint?
I'm looking for something like Editor.Preferences.PlaymodeTint = Color.green At least that is how I'm expecting it to look.
Thanks!
I don't need to set the tint, but I wander how to get current tint, is this reachable?
Answer by adamtuliper · Mar 25, 2016 at 09:42 AM
You can find this in the registry although I'm not sure what the randomness after the string name is. Ex HKEY_CURRENT_USER\SOFTWARE\Unity Technologies\Unity Editor 5.x
Playmode tint_h1778814853
The randomness is a hash value created from the key name "Playmode tint". They most likely added this to avoid name collisions with other keys. The registry is cast insensitive which could lead to problems
Answer by Bunny83 · Mar 25, 2016 at 11:54 AM
As @adamtuliper said the tint color is stored in the registry (at least on windows machines). Unity actually uses EditorPrefs.Get/SetString for this setting. However the inner workings is a bit more complicated as the value is cached in Unity. Of course that's all handled by internal classes which aren't exposed as API. So reflection to the rescue. I made a simple helper class that allows you to get and set the tint color:
using UnityEngine;
using UnityEditor;
using System.Linq;
using System.Reflection;
using System.Collections.Generic;
public static class SettingsHelper
{
static bool m_Initialized = false;
static FieldInfo m_PrefsField = null;
static FieldInfo m_PrefColorField = null;
static MethodInfo m_SetColorPref = null;
static SortedList<string, object> GetList()
{
return (SortedList<string, object>)m_PrefsField.GetValue(null);
}
static object GetPref(string aName)
{
return GetList()[aName];
}
static System.Type GetEditorType(string aName)
{
return typeof(Editor).Assembly.GetTypes().Where((a) => a.Name == aName).FirstOrDefault();
}
static SettingsHelper()
{
var settingsType = GetEditorType("Settings");
var prefColorType = GetEditorType("PrefColor");
if (settingsType == null || prefColorType == null)
throw new System.Exception("Something has changed in Unity and the SettingsHelper class is no longer supported");
m_PrefsField = settingsType.GetField("m_Prefs", BindingFlags.Static | BindingFlags.NonPublic);
m_PrefColorField = prefColorType.GetField("m_color", BindingFlags.Instance | BindingFlags.NonPublic);
m_SetColorPref = prefColorType.GetMethod("ToUniqueString", BindingFlags.Instance | BindingFlags.Public);
if (m_PrefsField == null || m_PrefColorField == null || m_SetColorPref == null)
throw new System.Exception("Something has changed in Unity and the SettingsHelper class is no longer supported");
m_Initialized = true;
}
public static Color playmodeTint
{
get
{
if (!m_Initialized)
return Color.black;
var p = GetPref("Playmode tint");
return (Color)m_PrefColorField.GetValue(p);
}
set
{
if (!m_Initialized)
return;
var p = GetPref("Playmode tint");
m_PrefColorField.SetValue(p, value);
string data = (string)m_SetColorPref.Invoke(p, null);
EditorPrefs.SetString("Playmode tint", data);
}
}
}
If you only want to read the current color you could directly use
EditorPrefs.GetString("Playmode tint","");
However the color is encoded / decoded "manually" with two methods in the "PrefColor" class. "ToUniqueString" converts the color inside the PrefColor into that string representation and "FromUniqueString" reads in the color from the string representation and sets the internal color field. You could parse the string manually, if you want but keep in mind it's an internal format and they can change it whenever they like.
What a legend! Thanks for sharing that.
For future travellers: change "m_color" to "m_Color" if the script doesn't work - it must have changed at some point since this was posted.
Answer by clacey_78 · Feb 29, 2020 at 04:35 PM
Updates to @Bunny83 's script for 2019 you will need to make the change @jmitcheson mentioned to change "m_color" to "m_Color". You will also need to change the name of the settings assembly it's looking for from "Settings" to "PrefSettings" [Tested with 2019.2.12f1]
var settingsType = GetEditorType("Settings");
to
var settingsType = GetEditorType("PrefSettings");
A simple way to cycle colours during playmode as a test:
[InitializeOnLoad]
public class EditorPlaymodeTintCycler
{
private static float cycleTimer = 0.0f;
static EditorPlaymodeTintCycler()
{
EditorApplication.update += Update;
}
static void Update()
{
if (!EditorApplication.isPlaying)
{
return;
}
cycleTimer += Time.deltaTime;
if (cycleTimer >= 1.0f)
{
cycleTimer = 0.0f;
float R = Random.Range(0.0f, 1.0f);
float G = Random.Range(0.0f, 1.0f);
float B = Random.Range(0.0f, 1.0f);
Color c = new Color(R, G, B, 1.0f);
SettingsHelper.PlaymodeTint = c;
}
}
}
Answer by Lukebox · Apr 27, 2021 at 12:07 PM
Still works in 2019.4 with the above fixes applied. Also works for other colors like scene background color, just change "Playmode tint" to "Scene/Background"
Your answer
Follow this Question
Related Questions
Material doesn't have a color property '_Color' 4 Answers
[Unity Editor] Emulate Touch Input - Still Asking 12/10 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Singleton and MonoBehaviour in Editor 1 Answer
EditorWindow : Change the color of words in a Label String 1 Answer