Window Framework v1.2.4
Window Framework for Unity UI Toolkit
Loading...
Searching...
No Matches
GWG.WindowFramework.WindowFrameThemeData Class Reference

Represents a theme data asset for configuring and managing window frame themes in the Window Framework system. This class is a ScriptableObject that provides functionality for managing themes, tracking the active theme, and applying changes to the window frame styles. More...

Inheritance diagram for GWG.WindowFramework.WindowFrameThemeData:

Public Member Functions

void CleanPlayerThemes ()
 The beginigs of a method to load player themes from the persistent data path. However, Unity does not currently support loading stylesheets at runtime from the persistent data path. I can not imagine this being the case for much longer so i have left this in place and will update it when Unity allows this.
delegate void DefaultThemeChangedEventHandler (int newThemeIndex)
 Event that is raised when the default theme designation changes.
void EnsureDefaultStyleSheets ()
 Placeholder method for future player theme functionality - ensures default stylesheets exist in persistent storage. Creates directories and copies default theme stylesheets to the persistent data path for player modification.
void LoadPlayerThemes ()
 Placeholder method for future player theme functionality - loads player-created themes from persistent storage. Scans the persistent data path for player theme directories and attempts to load theme files.
void SetAsDefaultTheme (int themeIndex)
 Designates a specific theme as the default theme by clearing the default flag from all themes and setting it only on the specified theme index. Triggers the default theme changed event to notify subscribers.
void SetTheme (int newActiveThemeIndex)
 Updates the active theme by setting the specified theme index. If the provided index is invalid or out of range, a default theme index is applied instead. Upon successful update of the active theme, triggers the OnThemeChanged event to notify listeners about the theme change.
void SetToDefaultTheme ()
 Configures the theme list to ensure a default theme is set. If no default theme is already specified, the first theme in the list is assigned as the default. If a default theme exists, it ensures the active theme index is updated accordingly. Logs warnings or errors if the theme list is empty or no default theme is found. Triggers the theme changed event upon successful theme configuration.
delegate void ThemeChangedEventHandler (int newThemeIndex, StyleSheet newStylesheet)
 Event that is raised when the active theme changes.

Protected Member Functions

virtual void OnPropertyChanged ([CallerMemberName] string propertyName=null)
 Raises the PropertyChanged event for the specified property and triggers editor view repaints.
bool SetField< T > (ref T field, T value, [CallerMemberName] string propertyName=null)
 Sets a field value and triggers property change notification if the value has changed. Provides efficient property setting with automatic change detection and notification.

Properties

int ActiveThemeIndex [get, set]
 Represents the index of the currently active theme within the list of available themes. This property is used to determine which theme's stylesheet is applied to the window frame. When set, it validates the provided index to ensure it falls within the valid range of the theme list. Changes to this property trigger notifications to reflect updates in the theme selection.
StyleSheet CurrentStylesheet [get]
 Gets the current active stylesheet based on the selected theme in the theme list. This property dynamically determines which theme's StyleSheet should be returned by evaluating the active theme index and ensuring it points to a valid theme. If the active theme index is invalid or out of range, it will attempt to find a default theme or set the first theme in the list as the default. Returns null if no themes are available.
ThemeApplyMethods ThemeApplyMethod [get, set]
 Defines the method used to apply themes in the window framework. Determines the scope of the theme application, ranging from targeting only window frames to applying the theme to broader groups such as parent panels or all active panels in the scene.
List< WindowFrameThemeThemes [get, set]
 Represents a collection of themes used to manage and apply different stylesheets for the window framework. This list enables dynamic switching between themes at runtime and provides a structured way to handle theme data in the inspector.

Events

DefaultThemeChangedEventHandler OnDefaultThemeChanged
ThemeChangedEventHandler OnThemeChanged
PropertyChangedEventHandler PropertyChanged
 Event required by INotifyPropertyChanged interface for property change notifications. Used for editor integration and real-time property updates.

Private Member Functions

void OnDestroy ()
void OnDisable ()
void OnEnable ()
void OnValidate ()

Private Attributes

int _activeThemeIndex
ThemeApplyMethods _themeApplyMethod = ThemeApplyMethods.WindowsFramesOnly
List< WindowFrameTheme_themes = new List<WindowFrameTheme>()
string playerThemesFolderName = "WindowFrameworkThemes"
 The folder name used for storing player themes in the persistent data path.

Detailed Description

The WindowFrameThemeData class serves as the central hub for theme management within the Window Framework. It handles theme loading, validation, switching, and application across different scopes of the user interface.

Key Features:

  • Dynamic theme switching at runtime
  • Automatic default theme handling and validation
  • Event-driven theme change notifications
  • Configurable theme application methods
  • Built-in default theme fallback system
  • Future support for player-created themes

This class includes the ability to load and manage themes bundled with the application. It also provides methods to apply themes and invoke changes when the active theme is updated.

Integration:
This class implements INotifyPropertyChanged for editor integration and real-time property updates, making it suitable for both runtime and editor-time theme management scenarios.

// Create a new theme data asset
var themeData = ScriptableObject.CreateInstance<WindowFrameThemeData>();
// Add a custom theme
var customTheme = new WindowFrameTheme
{
name = "Dark Theme",
themeStyleSheet = darkThemeStyleSheet,
isDefault = false
};
themeData.Themes.Add(customTheme);
// Set theme application method
themeData.ThemeApplyMethod = ThemeApplyMethods.WindowsFramesOnly;
// Switch to a different theme
themeData.SetTheme(1);
// Listen for theme changes
themeData.OnThemeChanged += (index, stylesheet) =>
{
Debug.Log($"Theme changed to index {index}");
ApplyThemeToUI(stylesheet);
};
Represents a theme data asset for configuring and managing window frame themes in the Window Framewor...
Definition WindowFrameThemeData.cs:141
Definition WindowFrameTheme.cs:9
ThemeApplyMethods
Specifies the method used to apply themes within the window framework system. Determines the level or...
Definition WindowFrameThemeData.cs:67

Member Function Documentation

◆ CleanPlayerThemes()

void GWG.WindowFramework.WindowFrameThemeData.CleanPlayerThemes ( )

This method is part of the planned player themes feature which is currently not supported due to Unity's limitations with loading StyleSheets at runtime from the persistent data path. When this feature becomes available, this method will remove all themes marked as player-created.

// Check for player themes before cleanup
int playerThemeCount = themeData.Themes.Count(t => t.PlayerTheme);
if (playerThemeCount > 0)
{
Debug.Log($"Cleaning up {playerThemeCount} player themes");
themeData.CleanPlayerThemes();
}

◆ DefaultThemeChangedEventHandler()

delegate void GWG.WindowFramework.WindowFrameThemeData.DefaultThemeChangedEventHandler ( int newThemeIndex)

This event is triggered when a different theme is designated as the default theme. It's separate from the main theme change event because default theme changes may not necessarily change the currently active theme.

// Subscribe to default theme change events
themeData.OnDefaultThemeChanged += (newDefaultIndex) =>
{
Debug.Log($"Default theme changed to: {themeData.Themes[newDefaultIndex].name}");
// Update UI to show new default
UpdateDefaultThemeIndicator(newDefaultIndex);
};

◆ EnsureDefaultStyleSheets()

void GWG.WindowFramework.WindowFrameThemeData.EnsureDefaultStyleSheets ( )

This method is part of the planned player themes feature. When implemented, it will copy default theme stylesheets to the persistent data path so they can be modified by players. Currently non-functional due to Unity's StyleSheet loading limitations.

// Future usage when player themes are supported:
// This would be used to ensure player theme files exist
/*
themeData.playerThemesEnabled = true;
themeData.EnsureDefaultStyleSheets();
// Check if directories were created successfully
string themesPath = Path.Combine(Application.persistentDataPath, "WindowFrameworkThemes");
if (Directory.Exists(themesPath))
{
Debug.Log("Player themes directory structure created successfully");
}
*&zwj;/

◆ LoadPlayerThemes()

void GWG.WindowFramework.WindowFrameThemeData.LoadPlayerThemes ( )

This method is part of the planned player themes feature. When implemented, it will scan the persistent data path for player-created theme files and load them into the theme collection. Currently non-functional due to Unity's StyleSheet loading limitations.

// Future usage when player themes are supported:
// Load player themes at application start
/*
if (themeData.playerThemesEnabled)
{
int themeCountBefore = themeData.Themes.Count;
themeData.LoadPlayerThemes();
int playerThemesLoaded = themeData.Themes.Count - themeCountBefore;
Debug.Log($"Loaded {playerThemesLoaded} player themes");
}
*&zwj;/

◆ OnDestroy()

void GWG.WindowFramework.WindowFrameThemeData.OnDestroy ( )
private

◆ OnDisable()

void GWG.WindowFramework.WindowFrameThemeData.OnDisable ( )
private

◆ OnEnable()

void GWG.WindowFramework.WindowFrameThemeData.OnEnable ( )
private

◆ OnPropertyChanged()

virtual void GWG.WindowFramework.WindowFrameThemeData.OnPropertyChanged ( [CallerMemberName] string propertyName = null)
protectedvirtual
Parameters
propertyNameThe name of the property that changed. Automatically determined if not provided using CallerMemberName attribute.

This method triggers editor view repaints when properties change, ensuring that Inspector windows and other editor UI elements reflect current property values.

// Typically called from property setters (automatically):
public int SomeProperty
{
get { return _someProperty; }
set
{
if (_someProperty != value)
{
_someProperty = value;
OnPropertyChanged(); // Property name automatically detected
}
}
}
// Manual call with explicit property name:
int ActiveThemeIndex
Represents the index of the currently active theme within the list of available themes....
Definition WindowFrameThemeData.cs:463
virtual void OnPropertyChanged([CallerMemberName] string propertyName=null)
Raises the PropertyChanged event for the specified property and triggers editor view repaints.
Definition WindowFrameThemeData.cs:950

◆ OnValidate()

void GWG.WindowFramework.WindowFrameThemeData.OnValidate ( )
private

◆ SetAsDefaultTheme()

void GWG.WindowFramework.WindowFrameThemeData.SetAsDefaultTheme ( int themeIndex)
Parameters
themeIndexThe zero-based index of the theme to set as default.

This method updates the default theme designation by clearing the default flag from all themes and setting it only on the specified theme. This ensures that exactly one theme is marked as default at any time.

// Set the second theme as default
themeData.SetAsDefaultTheme(1);
// Set default theme with validation
int newDefaultIndex = 3;
if (newDefaultIndex >= 0 && newDefaultIndex < themeData.Themes.Count)
{
themeData.SetAsDefaultTheme(newDefaultIndex);
}
// Find and set a theme as default by name
string themeName = "Dark Theme";
int themeIndex = themeData.Themes.FindIndex(t => t.name == themeName);
if (themeIndex >= 0)
{
themeData.SetAsDefaultTheme(themeIndex);
Debug.Log($"Set '{themeName}' as default theme");
}

◆ SetField< T >()

bool GWG.WindowFramework.WindowFrameThemeData.SetField< T > ( ref T field,
T value,
[CallerMemberName] string propertyName = null )
protected
Template Parameters
TThe type of the field being set.
Parameters
fieldReference to the field to update.
valueThe new value to assign to the field.
propertyNameThe name of the property associated with the field. Automatically determined if not provided using CallerMemberName attribute.
Returns
True if the field value was changed; false if the new value was the same as the existing value.

This utility method provides efficient property setting with automatic change detection and notification. It only triggers events when the value actually changes, reducing unnecessary UI updates and event handling overhead.

// Example usage in a property setter
public int SomeProperty
{
get { return _someProperty; }
set { SetField(ref _someProperty, value); } // Property name auto-detected
}
// Check if value actually changed
private void UpdatePropertyConditionally(int newValue)
{
if (SetField(ref _someField, newValue))
{
Debug.Log("Property value actually changed");
OnValueChanged();
}
}

◆ SetTheme()

void GWG.WindowFramework.WindowFrameThemeData.SetTheme ( int newActiveThemeIndex)
Parameters
newActiveThemeIndexThe index of the theme to be set as active.

This method is the primary way to switch themes in the window framework. It performs validation on the provided index and updates the active theme accordingly. If the index is invalid, it falls back to the first theme (index 0).

Event Notification:
Upon successful theme change, the method triggers the OnThemeChanged event with the new theme index and associated StyleSheet, allowing subscribers to update their UI elements accordingly.

// Switch to a specific theme by index
themeData.SetTheme(2);
// Switch themes with validation
int desiredTheme = 4;
if (desiredTheme < themeData.Themes.Count)
{
themeData.SetTheme(desiredTheme);
Debug.Log($"Switched to theme: {themeData.Themes[desiredTheme].name}");
}
// Cycle through themes
int nextTheme = (themeData.ActiveThemeIndex + 1) % themeData.Themes.Count;
themeData.SetTheme(nextTheme);
// Switch to theme by name
string targetTheme = "Dark Mode";
int themeIndex = themeData.Themes.FindIndex(t => t.name == targetTheme);
if (themeIndex >= 0)
{
themeData.SetTheme(themeIndex);
Debug.Log($"Switched to theme: {targetTheme}");
}
// Handle theme selection from UI dropdown
private void OnThemeDropdownChanged(int selectedIndex)
{
themeData.SetTheme(selectedIndex);
// Save user preference
PlayerPrefs.SetInt("UserSelectedTheme", selectedIndex);
PlayerPrefs.Save();
}

◆ SetToDefaultTheme()

void GWG.WindowFramework.WindowFrameThemeData.SetToDefaultTheme ( )

This method ensures that a default theme is properly configured and active. It performs the following operations:

  1. Checks if any themes exist in the collection
  2. If no default theme is set, designates the first theme as default
  3. If a default theme exists, sets it as the active theme
  4. Triggers the theme changed event to notify subscribers
  5. Logs appropriate warnings or errors for invalid configurations
// Ensure default theme is active at application start
themeData.SetToDefaultTheme();
// Reset to default theme after user customization
themeData.SetToDefaultTheme();
// Use as part of theme system initialization
private void InitializeThemes()
{
// Load custom themes
LoadCustomThemes();
// Ensure default is set
themeData.SetToDefaultTheme();
// Apply theme to UI
ApplyCurrentTheme();
}
// Handle theme reset button
private void OnResetToDefaultClicked()
{
themeData.SetToDefaultTheme();
ShowMessage("Theme reset to default");
}

◆ ThemeChangedEventHandler()

delegate void GWG.WindowFramework.WindowFrameThemeData.ThemeChangedEventHandler ( int newThemeIndex,
StyleSheet newStylesheet )

This event provides notification when the active theme is changed, either through direct theme switching or automatic fallback to default themes. Subscribers receive both the new theme index and the associated StyleSheet for immediate application.

// Subscribe to theme change events
themeData.OnThemeChanged += (index, stylesheet) =>
{
Debug.Log($"Theme changed to: {themeData.Themes[index].name}");
// Apply new stylesheet to UI elements
if (stylesheet != null)
{
rootVisualElement.styleSheets.Clear();
rootVisualElement.styleSheets.Add(stylesheet);
}
};
// Unsubscribe when no longer needed
themeData.OnThemeChanged -= OnThemeChangedHandler;

Member Data Documentation

◆ _activeThemeIndex

int GWG.WindowFramework.WindowFrameThemeData._activeThemeIndex
private

◆ _themeApplyMethod

ThemeApplyMethods GWG.WindowFramework.WindowFrameThemeData._themeApplyMethod = ThemeApplyMethods.WindowsFramesOnly
private

◆ _themes

List<WindowFrameTheme> GWG.WindowFramework.WindowFrameThemeData._themes = new List<WindowFrameTheme>()
private

◆ playerThemesFolderName

string GWG.WindowFramework.WindowFrameThemeData.playerThemesFolderName = "WindowFrameworkThemes"
private

This is part of the planned player themes feature and is currently not functional. When Unity supports runtime StyleSheet loading, this will be used to organize player-created theme files in the persistent data directory.

Property Documentation

◆ ActiveThemeIndex

int GWG.WindowFramework.WindowFrameThemeData.ActiveThemeIndex
getset

An integer representing the zero-based index of the active theme. Must be within the valid range of the Themes collection.

This property controls which theme from the Themes collection is currently active and applied to the window framework. The index is automatically validated to ensure it remains within the bounds of the available themes.

// Set active theme by index
themeData.ActiveThemeIndex = 2;
// Get current active theme index
int currentIndex = themeData.ActiveThemeIndex;
Debug.Log($"Current theme index: {currentIndex}");
// Validate index before setting
int desiredIndex = 5;
if (desiredIndex >= 0 && desiredIndex < themeData.Themes.Count)
{
themeData.ActiveThemeIndex = desiredIndex;
}
// Cycle through themes
int nextIndex = (themeData.ActiveThemeIndex + 1) % themeData.Themes.Count;
themeData.ActiveThemeIndex = nextIndex;

◆ CurrentStylesheet

StyleSheet GWG.WindowFramework.WindowFrameThemeData.CurrentStylesheet
get

The StyleSheet of the currently active theme, or null if no themes are available.

This property performs automatic validation and fallback logic to handle invalid configurations. The validation steps include:

  1. Checks if any themes are available in the collection
  2. Validates that the active theme index is within valid bounds
  3. Attempts to find and use a designated default theme if index is invalid
  4. Sets the first theme as default if no default theme exists
  5. Returns null if no themes are available
// Get the current active stylesheet
StyleSheet activeSheet = themeData.CurrentStylesheet;
if (activeSheet != null)
{
// Apply the stylesheet to a visual element
visualElement.styleSheets.Add(activeSheet);
Debug.Log($"Applied theme: {themeData.Themes[themeData.ActiveThemeIndex].name}");
}
else
{
Debug.LogWarning("No active theme available");
}
// Use in conjunction with theme switching
themeData.SetTheme(2);
StyleSheet newSheet = themeData.CurrentStylesheet; // Gets the new theme's stylesheet

◆ ThemeApplyMethod

ThemeApplyMethods GWG.WindowFramework.WindowFrameThemeData.ThemeApplyMethod
getset

A ThemeApplyMethods enumeration value that determines the scope of theme application within the UI hierarchy.

This property controls how broadly themes are applied throughout the user interface. The selection affects both performance and visual consistency of theme changes.

Performance Impact:
Broader application methods require more processing time during theme switches. Choose the most restrictive method that meets your visual requirements.

// Set theme application to window frames only (most efficient)
themeData.ThemeApplyMethod = ThemeApplyMethods.WindowsFramesOnly;
// Apply to parent panel for broader coverage
themeData.ThemeApplyMethod = ThemeApplyMethods.ParentPanel;
// Dynamic selection based on performance requirements
if (QualitySettings.GetQualityLevel() >= 3)
{
themeData.ThemeApplyMethod = ThemeApplyMethods.AllPanels;
}
else
{
themeData.ThemeApplyMethod = ThemeApplyMethods.WindowsFramesOnly;
}

◆ Themes

List<WindowFrameTheme> GWG.WindowFramework.WindowFrameThemeData.Themes
getset

A List<T> of WindowFrameTheme objects representing all available themes that can be applied to the window framework.

This collection serves as the central repository for all theme configurations within the window framework. It supports dynamic modification at runtime and provides the foundation for theme switching functionality.

Collection Integrity:
The framework ensures that at least one theme is always available and that exactly one theme is designated as the default theme. Invalid configurations are automatically corrected during initialization.

// Add a new theme to the collection
var newTheme = new WindowFrameTheme
{
name = "Custom Dark Theme",
themeStyleSheet = customStyleSheet,
isDefault = false
};
themeData.Themes.Add(newTheme);
// Find a specific theme
var darkTheme = themeData.Themes.Find(t => t.name.Contains("Dark"));
if (darkTheme != null)
{
Debug.Log($"Found dark theme: {darkTheme.name}");
}
// Iterate through all themes
for (int i = 0; i < themeData.Themes.Count; i++)
{
var theme = themeData.Themes[i];
Debug.Log($"Theme {i}: {theme.name} (Default: {theme.isDefault})");
}

Event Documentation

◆ OnDefaultThemeChanged

DefaultThemeChangedEventHandler GWG.WindowFramework.WindowFrameThemeData.OnDefaultThemeChanged

◆ OnThemeChanged

ThemeChangedEventHandler GWG.WindowFramework.WindowFrameThemeData.OnThemeChanged

◆ PropertyChanged

PropertyChangedEventHandler GWG.WindowFramework.WindowFrameThemeData.PropertyChanged

This event enables automatic UI updates in the Unity Editor when properties change, providing immediate visual feedback during theme configuration and development.

// Subscribe to property changes (typically done by editor systems)
themeData.PropertyChanged += (sender, args) =>
{
Debug.Log($"Property '{args.PropertyName}' changed on {sender}");
};