using UnityEngine; using UnityEngine.UI; using System.Collections; using System.Collections.Generic; public class load_teammatesProfile : MonoBehaviour { private static Dictionary heroById; private static bool heroCacheReady; private static bool heroCacheBuilding; [Header("½ÇɫͷÏñ")] public Image teammate_profile_01; public Image teammate_profile_02; public Image teammate_profile_03; public Image teammate_profile_04; public Image teammate_profile_05; [Header("boarder")] public Image teammate_profile_boarder_01; public Image teammate_profile_boarder_02; public Image teammate_profile_boarder_03; public Image teammate_profile_boarder_04; public Image teammate_profile_boarder_05; [Header("Level Colors")] public Color levelColor_C; public Color levelColor_B; public Color levelColor_A; public Color levelColor_S; // Start is called once before the first execution of Update after the MonoBehaviour is created IEnumerator Start() { yield return EnsureHeroCache(); update_teammates_profile(); } private IEnumerator EnsureHeroCache() { if (heroCacheReady) yield break; if (heroCacheBuilding) { while (!heroCacheReady) { yield return null; } yield break; } heroCacheBuilding = true; var allHeroes = Resources.LoadAll(""); heroById = new Dictionary(); if (allHeroes != null) { for (int i = 0; i < allHeroes.Length; i++) { var hero = allHeroes[i]; if (hero != null && !heroById.ContainsKey(hero.ally_heroID)) { heroById.Add(hero.ally_heroID, hero); } if ((i % 32) == 0) { yield return null; } } } heroCacheReady = true; heroCacheBuilding = false; } private IEnumerator EnsureHeroCacheThenUpdate() { yield return EnsureHeroCache(); update_teammates_profile(); } public void update_teammates_profile() { if (!heroCacheReady) { StartCoroutine(EnsureHeroCacheThenUpdate()); return; } // Load selected hero IDs from PlayerPrefs int heroId1 = PlayerPrefs.GetInt("selected_heroSlot01_heroID", 0); int heroId2 = PlayerPrefs.GetInt("selected_heroSlot02_heroID", 0); int heroId3 = PlayerPrefs.GetInt("selected_heroSlot03_heroID", 0); int heroId4 = PlayerPrefs.GetInt("selected_heroSlot04_heroID", 0); int heroId5 = PlayerPrefs.GetInt("selected_heroSlot05_heroID", 0); // Set sprites for each profile image teammate_profile_01.sprite = GetHeroSquareProfile(heroId1); teammate_profile_02.sprite = GetHeroSquareProfile(heroId2); teammate_profile_03.sprite = GetHeroSquareProfile(heroId3); teammate_profile_04.sprite = GetHeroSquareProfile(heroId4); teammate_profile_05.sprite = GetHeroSquareProfile(heroId5); // Set border colors based on hero level teammate_profile_boarder_01.color = GetBorderColor(GetHeroLevel(heroId1)); teammate_profile_boarder_02.color = GetBorderColor(GetHeroLevel(heroId2)); teammate_profile_boarder_03.color = GetBorderColor(GetHeroLevel(heroId3)); teammate_profile_boarder_04.color = GetBorderColor(GetHeroLevel(heroId4)); teammate_profile_boarder_05.color = GetBorderColor(GetHeroLevel(heroId5)); } private Sprite GetHeroSquareProfile(int heroId) { if (heroId == 0 || heroById == null) return null; AllyHero_SO hero; if (heroById.TryGetValue(heroId, out hero) && hero != null) { return hero.ally_hero_squareProfile; } return null; } private string GetHeroLevel(int heroId) { if (heroId == 0) return "C"; if (heroById == null) return "C"; AllyHero_SO hero; if (!heroById.TryGetValue(heroId, out hero) || hero == null) return "C"; return GetRatingFromSO(hero); } private string GetRatingFromSO(AllyHero_SO so) { if (so == null || so.levelStats == null || so.levelStats.Count == 0) return "C"; // Build a sorted copy by requiredEXP ascending List sorted = new List(); foreach (var l in so.levelStats) if (l != null) sorted.Add(l); sorted.Sort((a, b) => a.requiredEXP.CompareTo(b.requiredEXP)); int currentExp = so.ally_currentEXP; int selectedIndex = 0; for (int i = 0; i < sorted.Count; i++) { if (currentExp >= sorted[i].requiredEXP) { selectedIndex = i; } else { break; } } // Map indices to ratings: index 0 -> C, 1 -> B, 2 -> A, 3+ -> S if (selectedIndex <= 0) return "C"; if (selectedIndex == 1) return "B"; if (selectedIndex == 2) return "A"; return "S"; } private Color GetBorderColor(string level) { Color color; switch (level) { case "C": color = levelColor_C; break; case "B": color = levelColor_B; break; case "A": color = levelColor_A; break; case "S": color = levelColor_S; break; default: color = Color.white; break; } color.a = 1f; return color; } }