Native Integration
App UI supports communication with the operating system through native plugins. For now plugins have been developed for the following platforms:
Screen Informations
Thanks to the Screen class from Unity, you can get information about the screen size and orientation. However we found that the relationship between the dpi and the UI Toolkit PanelSettings is different from a platform to another.
We provide a mainScreenScale property which gives an accurate scale factor that takes into account the dpi of the screen but also user-defined display scaling from the operating system.
When you enable the Auto Scale Mode on the UI Toolkit PanelSettings, the UI elements will be scaled according to the referenceDpi value which is calculated using the mainScreenScale property.
System Theme
App UI provides a systemTheme property that allows you to get the current system theme. We also provide a systemThemeChanged event that you can subscribe to in order to be notified when the system theme changes.
It is common to see apps that let the user choose between a light and a dark theme, or stick to the system theme. You can offer the same feature by using the systemTheme property.
Here is an example using the systemTheme property inside a RadioGroup:
// Get the closest provider, assuming you call this method from a UI Toolkit element
// contained in a hierarchy that has a ThemeContext provider
var provider = this.GetContextProvider<ThemeContext>();
// Create the callback that will be called when the system theme changes
void OnSystemThemeChanged(string systemTheme) => provider.theme = systemTheme;
// Setup the RadioGroup
var themeSwitcher = new RadioGroup();
themeSwitcher.Add(new Radio("System"));
themeSwitcher.Add(new Radio("Dark"));
themeSwitcher.Add(new Radio("Light"));
void SetTheme()
{
Platform.systemThemeChanged -= OnSystemThemeChanged;
switch (themeSwitcher.value)
{
case 0:
provider.theme = Platform.systemTheme;
Platform.systemThemeChanged += OnSystemThemeChanged;
break;
case 1:
provider.theme = "dark";
break;
case 2:
provider.theme = "light";
break;
}
PlayerPrefs.SetInt("theme", themeSwitcher.value);
}
themeSwitcher.RegisterValueChangedCallback(_ => SetTheme());
// Load and set the theme value
themeSwitcher.SetValueWithoutNotify(PlayerPrefs.GetInt("theme", 1));
SetTheme();
To know more about theming, see the dedicated documentation page.
Haptic Feedback
App UI provides a RunHapticFeedback method that allows you to trigger haptic feedback on supported platforms. For now, the only supported platforms are iOS and Android.
Note
In the current state of App UI components, none of them use haptic feedback directly. However, you can use this method to trigger haptic feedback when you need it.