using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System.Collections; public class newTeamSelector : MonoBehaviour { [Header("µÈ¼¶Í¼Æ¬CBAS")] public Sprite levelIcon_sprite_C; public Sprite levelIcon_sprite_B; public Sprite levelIcon_sprite_A; public Sprite levelIcon_sprite_S; [Tooltip("Prefab for a single hero slot. Must contain slots_heroSlots component.")] public GameObject slotPrefab; [Tooltip("Container (GameObject with HorizontalLayoutGroup) where slots will be instantiated")] public RectTransform container; [Tooltip("Number of slots to create (default 5)")] public int slotCount = 5; [Tooltip("Optional override colors used for header images per slot. If not set, prefab's headerImage_colors will be used.")] public Color[] overrideHeaderColors; [Tooltip("If true, create slots automatically on Start")] public bool instantiateOnStart = true; private readonly List createdSlots = new List(); void Start() { if (instantiateOnStart) CreateSlots(); } void Update() { } public void CreateSlots() { if (slotPrefab == null || container == null) { Debug.LogError("newTeamSelector: slotPrefab or container is not assigned."); return; } // Clear existing for (int i = container.childCount - 1; i >= 0; i--) { var c = container.GetChild(i); #if UNITY_EDITOR if (!Application.isPlaying) DestroyImmediate(c.gameObject); else Destroy(c.gameObject); #else Destroy(c.gameObject); #endif } createdSlots.Clear(); for (int i = 0; i < slotCount; i++) { var go = Instantiate(slotPrefab, container); go.transform.SetParent(container, false); go.name = $"heroSlot_{i}"; var slotComp = go.GetComponent(); if (slotComp == null) { Debug.LogWarning("newTeamSelector: instantiated prefab does not contain slots_heroSlots component."); continue; } slotComp.heroSlot_slotID = i; // choose color: overrideHeaderColors takes priority, then prefab's headerImage_colors if available Color chosen = Color.white; if (overrideHeaderColors != null && i < overrideHeaderColors.Length) { chosen = overrideHeaderColors[i]; } else if (slotComp.headerImage_colors != null && i < slotComp.headerImage_colors.Length) { chosen = slotComp.headerImage_colors[i]; } // Ensure visible alpha chosen.a = Mathf.Clamp01(chosen.a); if (chosen.a <= 0f) chosen.a = 1f; // Apply immediately if possible if (slotComp.heroSlot_colorImage != null) { slotComp.heroSlot_colorImage.color = chosen; } createdSlots.Add(slotComp); } // Force immediate layout rebuild so HorizontalLayoutGroup arranges children right away LayoutRebuilder.ForceRebuildLayoutImmediate(container); // Re-apply colors next frame to override any Start() logic on instantiated prefabs StartCoroutine(ApplyHeaderColorsNextFrame()); } private IEnumerator ApplyHeaderColorsNextFrame() { yield return null; // wait one frame for (int i = 0; i < createdSlots.Count; i++) { var slot = createdSlots[i]; if (slot == null) continue; Color chosen = Color.white; if (overrideHeaderColors != null && i < overrideHeaderColors.Length) { chosen = overrideHeaderColors[i]; } else if (slot.headerImage_colors != null && i < slot.headerImage_colors.Length) { chosen = slot.headerImage_colors[i]; } chosen.a = 1f; if (slot.heroSlot_colorImage == null) { var img = slot.GetComponentInChildren(true); if (img != null) slot.heroSlot_colorImage = img; } if (slot.heroSlot_colorImage != null) slot.heroSlot_colorImage.color = chosen; } // ensure layout rebuilt after modifications LayoutRebuilder.ForceRebuildLayoutImmediate(container); } public List GetCreatedSlots() => createdSlots; }