using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; // Couleurs du membre appliquées à l'interface. // // Marque un élément avec le tag C1 ou C2 et il prend la couleur correspondante : // _G.Color1 ou _G.Color2, remplies par Starting selon le membre. // // Le tag suffit, quel que soit le composant : Image, TMP_Text et Text dérivent // tous de Graphic, donc une seule règle les couvre. public class COLORSET : MonoBehaviour { private const string Tag1 = "C1"; private const string Tag2 = "C2"; // Un tag non déclaré dans le projet lève une exception à chaque recherche : // on le signale une fois, puis on l'ignore. private static readonly HashSet _missingTags = new(); public static void On() { addcolor(Tag1, _G.Color1); addcolor(Tag2, _G.Color2); } public static void addcolor(string tag, string hex) { if (_missingTags.Contains(tag)) return; GameObject[] tagged; try { tagged = GameObject.FindGameObjectsWithTag(tag); } catch (UnityException) { _missingTags.Add(tag); Debug.LogWarning("COLORSET : le tag " + tag + " n'est pas déclaré dans le projet."); return; } foreach (GameObject obj in tagged) { Graphic graphic = obj.GetComponent(); if (graphic == null) continue; Color color = Parse(hex, graphic.color); // L'opacité réglée sur l'élément est conservée : un fond translucide // le reste après coloration. color.a = graphic.color.a; graphic.color = color; } } // "373435" ou "#373435" : les deux écritures sont acceptées. public static Color Parse(string hex, Color fallback) { if (string.IsNullOrEmpty(hex)) return fallback; if (!hex.StartsWith("#")) hex = "#" + hex; return ColorUtility.TryParseHtmlString(hex, out Color color) ? color : fallback; } }