Files
bansonic_beta_main/Assets/scripts/selectYourSongFirst/load_teammatesProfile.cs
T

137 lines
4.5 KiB
C#

using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
public class load_teammatesProfile : MonoBehaviour
{
[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
void Start()
{
update_teammates_profile();
}
public void update_teammates_profile()
{
// 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) return null;
// Load all AllyHero_SO and find the one with matching ID
var allHeroes = Resources.LoadAll<AllyHero_SO>("");
foreach (var hero in allHeroes)
{
if (hero != null && hero.ally_heroID == heroId)
{
return hero.ally_hero_squareProfile;
}
}
return null; // Not found
}
private string GetHeroLevel(int heroId)
{
if (heroId == 0) return "C"; // default
var allHeroes = Resources.LoadAll<AllyHero_SO>("");
AllyHero_SO hero = null;
foreach (var h in allHeroes)
{
if (h != null && h.ally_heroID == heroId)
{
hero = h;
break;
}
}
if (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<AllyHero_SO.AllyLevelInfo> sorted = new List<AllyHero_SO.AllyLevelInfo>();
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;
}
}