### Create Custom Gradient Synchronizer (C#) Source: https://github.com/haruma-k/upalette/blob/master/README.md This example shows how to create a custom Synchronizer in C# to reflect Gradient values to a custom component. It requires the 'uPalette.Runtime.Core.Synchronizer.Gradient' namespace and a component with a Gradient property. The synchronizer overrides GetValue and SetValue methods. ```csharp using UnityEngine; using uPalette.Runtime.Core.Synchronizer.Gradient; public class SampleGradient : MonoBehaviour { [SerializeField] private Gradient _gradient; public Gradient Gradient { get => _gradient; set => _gradient = value; } } [AddComponentMenu("")] [DisallowMultipleComponent] [RequireComponent(typeof(SampleGradient))] [GradientSynchronizer(typeof(SampleGradient), "Gradient")] public sealed class GraphicColorSynchronizer : GradientSynchronizer { protected override Gradient GetValue() { return _component.Gradient; } protected override void SetValue(Gradient value) { _component.Gradient = value; } } ``` -------------------------------- ### Get and Monitor Entry Values from Script in uPalette Source: https://github.com/haruma-k/upalette/blob/master/README.md This C# code demonstrates how to retrieve and monitor entry values from uPalette's `PaletteStore`. It uses `GetActiveValue()` to get an observable property and `Subscribe()` to monitor changes. ```csharp using System; using UnityEngine; using uPalette.Generated; using uPalette.Runtime.Core; public class Example : MonoBehaviour { private void Start() { // Get the color palette. var colorPalette = PaletteStore.Instance.ColorPalette; // Get the color entry id from the auto-generated ColorEntry enum. var targetColorEntryId = ColorEntry.KeyColor1.ToEntryId(); var colorProperty = colorPalette.GetActiveValue(targetColorEntryId); // If you want to get the current value, use the Value property. var targetValue = colorProperty.Value; // If you want to get the value when the theme is changed, subscribe the property. IObserver observer; var disposable = colorProperty.Subscribe(observer); } } ``` -------------------------------- ### Install uPalette via Unity Package Manager Source: https://context7.com/haruma-k/upalette/llms.txt This code snippet demonstrates how to install the uPalette package in a Unity project using the Package Manager. It uses a Git URL to specify the package location and supports specifying a specific version. ```Unity Package Manager # Unity Package Manager Git URL https://github.com/Haruma-K/uPalette.git?path=/Assets/uPalette # With specific version https://github.com/Haruma-K/uPalette.git?path=/Assets/uPalette#2.5.3 ``` -------------------------------- ### Get Active Theme in C# Source: https://context7.com/haruma-k/upalette/llms.txt This C# script retrieves and displays information about the currently active theme in a uPalette color palette. It accesses the active theme's ID and name and subscribes to changes to update the UI. The script utilizes the uPalette.Runtime.Core namespace and assumes a PaletteStore instance is available. ```csharp using UnityEngine; using uPalette.Runtime.Core; public class ThemeInfo : MonoBehaviour { void Start() { var colorPalette = PaletteStore.Instance.ColorPalette; // Get current active theme var activeTheme = colorPalette.ActiveTheme.Value; Debug.Log($"Active theme ID: {activeTheme.Id}"); Debug.Log($"Active theme name: {activeTheme.Name.Value}"); // Subscribe to theme changes colorPalette.ActiveTheme.Subscribe(theme => { Debug.Log($"Theme changed to: {theme.Name.Value}"); }); } } ``` -------------------------------- ### Safely Get Active Color Value in C# Source: https://context7.com/haruma-k/upalette/llms.txt This C# code demonstrates how to safely retrieve an entry value using Palette.TryGetActiveValue(), including error handling when the entry might not exist. It checks if the entry exists before accessing its value. ```csharp using UnityEngine; using uPalette.Runtime.Core; public class SafeColorAccess : MonoBehaviour { [SerializeField] private string entryId; void Start() { var colorPalette = PaletteStore.Instance.ColorPalette; // Safely try to get the value if (colorPalette.TryGetActiveValue(entryId, out var colorProperty)) { Color color = colorProperty.Value; GetComponent().color = color; } else { Debug.LogWarning($"Entry {entryId} not found in color palette"); } } } ``` -------------------------------- ### Get Active Color Value in C# Source: https://context7.com/haruma-k/upalette/llms.txt This C# code retrieves the current value of a color entry from the active theme using the Palette.GetActiveValue() method. It demonstrates how to access the current value directly and subscribe to value changes for reactive programming. ```csharp using UnityEngine; using uPalette.Runtime.Core; using uPalette.Generated; public class GetColorValue : MonoBehaviour { void Start() { var colorPalette = PaletteStore.Instance.ColorPalette; // Get entry ID from auto-generated enum var entryId = ColorEntry.PrimaryColor.ToEntryId(); // Get the active value as an observable property var colorProperty = colorPalette.GetActiveValue(entryId); // Access current value directly Color currentColor = colorProperty.Value; Debug.Log($"Primary color: {currentColor}"); // Subscribe to value changes (e.g., when theme switches) colorProperty.Subscribe(newColor => { Debug.Log($"Color changed to: {newColor}"); GetComponent().material.color = newColor; }); } } ``` -------------------------------- ### Switch Color Themes via Script in Unity Source: https://github.com/haruma-k/upalette/blob/master/README.md Demonstrates how to switch between different color themes at runtime using C# scripting in Unity. It iterates through available color themes and activates them when corresponding buttons are pressed. This requires the uPalette runtime core and automatically generated theme enums. ```csharp using System; using UnityEngine; using uPalette.Generated; using uPalette.Runtime.Core; public class Example : MonoBehaviour { public void OnGUI() { foreach (ColorTheme colorTheme in Enum.GetValues(typeof(ColorTheme))) if (GUILayout.Button(colorTheme.ToString())) { var colorPalette = PaletteStore.Instance.ColorPalette; colorPalette.SetActiveTheme(colorTheme.ToThemeId()); } } } ``` -------------------------------- ### Access PaletteStore Instance in C# Source: https://context7.com/haruma-k/upalette/llms.txt This C# code demonstrates how to access the singleton PaletteStore instance to retrieve palettes for colors, gradients, and character styles. It also shows how to configure the missing entry behavior. ```csharp using UnityEngine; using uPalette.Runtime.Core; public class PaletteAccess : MonoBehaviour { void Start() { // Get the singleton PaletteStore instance var paletteStore = PaletteStore.Instance; // Access individual palettes var colorPalette = paletteStore.ColorPalette; // Palette var gradientPalette = paletteStore.GradientPalette; // Palette var characterStylePalette = paletteStore.CharacterStylePalette; // Palette var characterStyleTMPPalette = paletteStore.CharacterStyleTMPPalette; // Palette // Configure missing entry behavior paletteStore.MissingEntryErrorLevel = MissingEntryErrorLevel.Warning; // Options: None, Warning, Error, Exception } } ``` -------------------------------- ### Manual PaletteStore Loading in uPalette Source: https://github.com/haruma-k/upalette/blob/master/README.md This C# code snippet shows how to manually load the `PaletteStore` when automatic runtime data loading is disabled. This is necessary when distributing Palette Data via AssetBundles. ```csharp // You must load the PaletteStore manually before loading GUIs that use uPalette. var _ = Resources.Load("PaletteStore"); ``` -------------------------------- ### Implement Custom Image Color Synchronizer (C#) Source: https://github.com/haruma-k/upalette/blob/master/README.md This C# snippet illustrates creating a custom Synchronizer for the Image component's Color property, with an option to control alpha synchronization. It requires the 'UnityEngine.UI.Image' component and uses attributes like [DisallowMultipleComponent] and [ColorSynchronizer]. ```csharp using UnityEngine; using UnityEngine.UI; [DisallowMultipleComponent] [RequireComponent(typeof(Image))] [ColorSynchronizer(typeof(Image), "Color")] public sealed class CustomImageColorSynchronizer : ColorSynchronizer { [SerializeField] private bool _syncAlpha = true; protected override Color GetValue() { return Component.color; } protected override void SetValue(Color value) { if (!_syncAlpha) { value.a = Component.color.a; } Component.color = value; } } ``` -------------------------------- ### Access Palette Entries and Themes in C# Source: https://context7.com/haruma-k/upalette/llms.txt This C# script demonstrates how to iterate through entries and themes within a uPalette color palette. It accesses and logs the names and IDs of entries and themes, along with the values associated with each theme. The script also shows how to check for the existence of entries and themes. It utilizes the uPalette.Runtime.Core namespace. ```csharp using UnityEngine; using uPalette.Runtime.Core; public class PaletteInspector : MonoBehaviour { void Start() { var colorPalette = PaletteStore.Instance.ColorPalette; // Iterate all entries foreach (var entry in colorPalette.Entries.Values) { Debug.Log($"Entry: {entry.Name.Value} (ID: {entry.Id})"); // Access values for specific themes foreach (var themeValue in entry.Values) { Debug.Log($" Theme {themeValue.Key}: {themeValue.Value.Value}"); } } // Iterate all themes foreach (var theme in colorPalette.Themes.Values) { Debug.Log($"Theme: {theme.Name.Value} (ID: {theme.Id})"); } // Check if entry/theme exists bool hasEntry = colorPalette.HasEntry("some-entry-id"); bool hasTheme = colorPalette.HasTheme("some-theme-id"); } } ``` -------------------------------- ### Set Active Theme in C# Source: https://context7.com/haruma-k/upalette/llms.txt This C# code demonstrates how to switch the active theme at runtime using Palette.SetActiveTheme(). It creates buttons for each available theme and updates the color, gradient, and character style palettes. ```csharp using System; using UnityEngine; using uPalette.Runtime.Core; using uPalette.Generated; public class ThemeSwitcher : MonoBehaviour { void OnGUI() { // Create buttons for each available theme foreach (ColorTheme theme in Enum.GetValues(typeof(ColorTheme))) { if (GUILayout.Button(theme.ToString())) { var colorPalette = PaletteStore.Instance.ColorPalette; colorPalette.SetActiveTheme(theme.ToThemeId()); // Also switch gradient and character style themes if needed var gradientPalette = PaletteStore.Instance.GradientPalette; gradientPalette.SetActiveTheme(GradientTheme.Default.ToThemeId()); } } } } ``` -------------------------------- ### Add and Remove Palette Entries in C# Source: https://context7.com/haruma-k/upalette/llms.txt This C# script demonstrates how to programmatically add, remove, and restore palette entries at runtime or within editor scripts using uPalette. It shows how to create a new entry, set its value for the active theme, remove the entry, and then restore it. The script also includes editor-specific code to mark the palette as dirty and save the changes. ```csharp using UnityEngine; using uPalette.Runtime.Core; #if UNITY_EDITOR using UnityEditor; #endif public class PaletteEditor : MonoBehaviour { void CreateNewEntry() { var colorPalette = PaletteStore.Instance.ColorPalette; // Add a new entry var newEntry = colorPalette.AddEntry(); newEntry.Name.Value = "CustomRed"; // Set value for active theme var activeThemeId = colorPalette.ActiveTheme.Value.Id; if (newEntry.TryGetValue(activeThemeId, out var valueProperty)) { valueProperty.Value = Color.red; } // Remove an entry colorPalette.RemoveEntry(newEntry.Id); // Restore a removed entry var restoredEntry = colorPalette.RestoreEntry(newEntry.Id); #if UNITY_EDITOR // Mark dirty and save in editor EditorUtility.SetDirty(PaletteStore.Instance); AssetDatabase.SaveAssets(); #endif } } ``` -------------------------------- ### Add and Remove Palette Themes in C# Source: https://context7.com/haruma-k/upalette/llms.txt This C# script demonstrates how to programmatically add, remove, and reorder themes within a uPalette color palette. It shows how to create a new theme, set entry values for the new theme, reorder themes, and remove a theme. The script also includes editor-specific code to mark the palette as dirty and save the changes. ```csharp using UnityEngine; using uPalette.Runtime.Core; #if UNITY_EDITOR using UnityEditor; #endif public class ThemeManager : MonoBehaviour { void ManageThemes() { var colorPalette = PaletteStore.Instance.ColorPalette; // Add a new theme var darkTheme = colorPalette.AddTheme(); darkTheme.Name.Value = "Dark Mode"; // Set entry values for the new theme foreach (var entry in colorPalette.Entries.Values) { if (entry.TryGetValue(darkTheme.Id, out var valueProperty)) { // Darken all colors for dark theme var originalColor = colorPalette.GetActiveValue(entry.Id).Value; valueProperty.Value = originalColor * 0.5f; } } // Reorder themes colorPalette.SetThemeOrder(darkTheme.Id, 0); // Remove theme (cannot remove last theme) if (colorPalette.Themes.Count > 1) { colorPalette.RemoveTheme(darkTheme.Id); } #if UNITY_EDITOR EditorUtility.SetDirty(PaletteStore.Instance); AssetDatabase.SaveAssets(); #endif } } ``` -------------------------------- ### Synchronize Event Components for uPalette Source: https://github.com/haruma-k/upalette/blob/master/README.md These components allow you to receive notifications when entry values change without applying the value directly. They are useful for reacting to changes in Color, Gradient, Character Style, and Character Style TMP entries. ```csharp using UnityEngine.Events; public class ColorSynchronizeEvent : MonoBehaviour { public UnityEvent OnValueChanged; // ... implementation to detect value changes and invoke OnValueChanged } public class GradientSynchronizeEvent : MonoBehaviour { public UnityEvent OnValueChanged; // ... implementation to detect value changes and invoke OnValueChanged } public class CharacterStyleSynchronizeEvent : MonoBehaviour { public UnityEvent OnValueChanged; // ... implementation to detect value changes and invoke OnValueChanged } public class CharacterStyleTMPSynchronizeEvent : MonoBehaviour { public UnityEvent OnValueChanged; // ... implementation to detect value changes and invoke OnValueChanged } ``` -------------------------------- ### Automatic Enum Generation for uPalette Entries and Themes Source: https://github.com/haruma-k/upalette/blob/master/README.md This feature automatically generates C# enums for accessing Theme and Entry information from scripts. Configuration is done via Project Settings, and the generated enums provide extension methods like `ToEntryId()` for easy access. ```csharp using System; namespace uPalette.Generated { public enum ColorEntry { Red, Green, Blue, } } // Extension method to get Entry ID from enum public static class PaletteEntryExtensions { public static int ToEntryId(this ColorEntry entry) { // Implementation to convert enum to entry ID return (int)entry; } } ``` ```csharp using uPalette.Generated; public class Example { private void Foo() { // Accessing entry ID using the generated enum ColorEntry.Red.ToEntryId(); } } ``` -------------------------------- ### Edit uPalette Data from Scripts (C#) Source: https://github.com/haruma-k/upalette/blob/master/README.md This snippet demonstrates how to retrieve and modify uPalette data from scripts. It emphasizes the importance of setting the dirty flag on ScriptableObjects after edits and optionally saving assets. Dependencies include UnityEditor utilities. ```csharp using UnityEditor; // Get PaletteStore. var paletteStore = PaletteStore.Instance; // Get each palette. var colorPalette = PaletteStore.Instance.ColorPalette; var gradientPalette = PaletteStore.Instance.GradientPalette; var characterStylePalette = PaletteStore.Instance.CharacterStylePalette; var characterStyleTMPPalette = PaletteStore.Instance.CharacterStyleTMPPalette; // Set the dirty flag after editing. EditorUtility.SetDirty(paletteStore); // Save assets if you need. AssetDatabase.SaveAssets(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.