Embracing the trend of dark mode, many users and developers alike find the dark theme more appealing and comfortable, especially during nighttime usage. If you're a Unity developer eager to incorporate dark mode into your project, you're likely wondering about the free resources available. This guide explores how to achieve Unity dark mode free of charge, making your games and apps user-friendly in any environment.

Before delving into the process, it's crucial to understand that while Unity provides theming tools, they don't offer a built-in dark mode toggle. However, you can create a workaround using free resources and some creative coding. Let's get started!

Understanding Unity's UI Toolkit
The new UI Toolkit introduced in Unity 2020 simplifies creating and managing user interfaces. It's crucial to grasp its fundamentals to implement dark mode effectively.

UI Toolkit uses UXML for the declarative part of the UI and C# for the code-behind functionality. UXML allows you to define your UI components and styles separately, making it easy to apply different themes like dark mode.
Introducing Stylesheets in Unity

Stylesheets in Unity provide a way to define your visual styles, such as colors, fonts, and spacing, in a central location. By using stylesheets, you can easily switch between light and dark themes.
To create a stylesheet, right-click in the Project window, go to Create > UI > Stylesheet. Name it according to your project's needs, and you're ready to start defining your UI's visual elements.
Creating a Dark Mode Stylesheet

For dark mode, you'll want to change colors to support the darker theme. You can create a new stylesheet specifically for dark mode or use an existing one with conditional rules based on a boolean variable indicating dark mode's activation.
Here's an example of a dark mode stylesheet snippet using Unity's color literals for quick visualization: ```xml ```
Implementing Dark Mode Toggle

To implement a dark mode toggle, you'll need to create a UI element that users can interact with to switch between themes. You can achieve this using Buttons, Toggle, or a dropdown, depending on your preferences.
Creating a Toggle System




















Using the UI Toolkit's Toggle component, you can create a simple toggle system for switching between light and dark modes. Assign the toggle's Is On value to a boolean variable that triggers the application of the stylesheet based on its value.
In your code-behind script, use Unity's Zenject to inject the dark mode boolean and UXML document. Listen for its value changes and apply the respective stylesheet:
```csharp private bool _isDarkMode; private UXMLDocument _uiDocument; public void Awake() { Zenject.Inject(_isDarkMode, _uiDocument); } public void OnToggleValueChanged(bool newValue) { _isDarkMode = newValue; if (_isDarkMode) { _uiDocument.styleSheetsStale = true; _uiDocument.styleSheets.Load(darkModeStylesheet); } else { // Load light mode stylesheet } } ```
Saving and Restoring Dark Mode Preference
To preserve user preferences, save the dark mode toggle status and restore it when the application is launched. You can achieve this by using PlayerPrefs in Unity, a simple key-value storage solution that persists between sessions.
In your Awake() method, retrieve the dark mode preference and apply the relevant stylesheet. Additionally, when the toggle value changes, update the PlayerPrefs value accordingly:
```csharp public void Awake() { Zenject.Inject(_isDarkMode, _uiDocument); _isDarkMode = PlayerPrefs.GetInt("DarkMode", 0) == 1; // 0 is for light mode, 1 is for dark mode } public void OnToggleValueChanged(bool newValue) { _isDarkMode = newValue; if (_isDarkMode) { PlayerPrefs.SetInt("DarkMode", 1); // Rest of the code... } else { PlayerPrefs.SetInt("DarkMode", 0); // Rest of the code... } } ```
And there you have it! Implementing Unity dark mode free of charge is accessible thanks to Unity's UI Toolkit and a bit of creative coding. Happy developing, and may your games and apps shine (or dim) according to users' preferences!