Themeable Components
by gatewayadventuretravel in Design > Software
19 Views, 0 Favorites, 0 Comments
Themeable Components
help change text and image faster to make a col
ThemeManager
using UnityEngine;
using System;
using TMPro.EditorUtilities;
using System.Collections.Generic;
using UnityEngine.UI;
//using ColorThiefDotNet;
public class ThemeManager : MonoBehaviour
{
public static event Action OnThemeChange;
public Color[] colors;
public Image image;
public int numberOfColors = 5;
public List<Color> palette = new List<Color>();
[ExecuteAlways]
public void ChangeTheme()
{
// Trigger the event
OnThemeChange?.Invoke();
}
private void OnValidate()
{
ChangeTheme();
}
/* public void ExtractColors()
{
if (image == null)
{
Debug.LogError("Image component not assigned.");
return;
}
// Convert Image sprite to Texture2D
Sprite sprite = image.sprite;
Texture2D texture = sprite.texture;
// Get pixel data from the texture
Color32[] pixels = texture.GetPixels32();
// Convert pixel data to byte array
byte[] imageData = new byte[pixels.Length * 4];
for (int i = 0; i < pixels.Length; i++)
{
imageData[i * 4] = pixels[i].r;
imageData[i * 4 + 1] = pixels[i].g;
imageData[i * 4 + 2] = pixels[i].b;
imageData[i * 4 + 3] = pixels[i].a;
}
// Create a ColorThief object
ColorThief colorThief = new ColorThief();
// Extract color palette
List<QuantizedColor> colorPalette = colorThief.GetPalette(imageData, numberOfColors);
// Clear the existing palette
palette.Clear();
// Convert QuantizedColor to Unity Color and add to palette
foreach (var color in colorPalette)
{
palette.Add(new Color(color.Color.R / 255f, color.Color.G / 255f, color.Color.B / 255f));
}
// Log the extracted colors
foreach (var color in palette)
{
Debug.Log(color);
}
}
*/
}
ThemeableComponent
using System.Collections.Generic;
using TMPro;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
using UnityEngine.UI;
[ExecuteAlways]
public class ThemeableComponent : MonoBehaviour
{
public ThemeManager themeManager;
public bool primary= true;
public bool secondary = false;
public bool tertiary = false;
public List<Image> images;
public List<TMP_Text> tmpTexts;
//public List<int> colorInt;
private Image img;
private TMP_Text txt;
public int c = 0;
private void OnValidate()
{
if(!primary& !secondary& !tertiary)
{
if (c == 0)
{
primary = true;
}
else if (c == 1)
{
secondary = true;
}
else if (c == 2)
{
tertiary = true;
}
else
{
}
}
if ((primary && secondary) ||(tertiary && secondary) || (primary && tertiary))
{
if (c == 0)
{
primary = false;
}
else if (c == 1)
{
secondary = false;
}
else if (c == 2)
{
tertiary = false;
}
else
{
}
if (primary)
{
c = 0;
}
else if (secondary)
{
c = 1;
}
else if (tertiary)
{
c = 2;
}
}
}
private void OnEnable()
{
ThemeManager.OnThemeChange += syncThemecolors;
}
private void OnDisable()
{
ThemeManager.OnThemeChange -= syncThemecolors;
}
public void syncThemecolors()
{
img = this.GetComponent<Image>();
txt = this.GetComponent<TMP_Text>();
if (img)
{
img.color = themeManager.colors[c];
}
if (txt)
{
txt.color = themeManager.colors[c];
}
/* foreach (Image image in images)
{
image.color = themeManager.colors[colorInt[0]];
}*/
}
}