142 lines
5.3 KiB
C#
142 lines
5.3 KiB
C#
/*
|
|
* FancyScrollView (https://github.com/setchi/FancyScrollView)
|
|
* Copyright (c) 2020 setchi
|
|
* Licensed under MIT (https://github.com/setchi/FancyScrollView/blob/master/LICENSE)
|
|
*/
|
|
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using DG.Tweening;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
class UI_Panel_Character : MonoBehaviour
|
|
{
|
|
[SerializeField] float anim_Time = 0.5f;
|
|
float Anim_Speed => 1f / anim_Time;
|
|
[SerializeField] List<Animator> ui_Anim;
|
|
[SerializeField] Transform content_Character_Slot;
|
|
[SerializeField] Button button_Character_Slot_Prefab;
|
|
[SerializeField] Text text_Character_Name;
|
|
[SerializeField] Image Image_Character_Illustration;
|
|
[SerializeField] Image Image_Character_Illustration_BG;
|
|
|
|
[Header("Data Paths")]
|
|
[Tooltip("Path relative to Resources folder for Editor mode")]
|
|
public string editorResourcePath = "so/ally";
|
|
[Tooltip("Path relative to Resources folder for Runtime mode")]
|
|
public string runtimeResourcePath = "so/ally";
|
|
|
|
private List<AllyHero_SO> allyHeroList = new List<AllyHero_SO>();
|
|
|
|
private void Update()
|
|
{
|
|
if (Input.GetKeyDown(KeyCode.Escape))
|
|
{
|
|
gameObject.SetActive(false);
|
|
}
|
|
}
|
|
void Start()
|
|
{
|
|
if (Image_Character_Illustration.transform.parent.TryGetComponent(out CanvasGroup canvasGroup))
|
|
{
|
|
canvasGroup.alpha = 0;
|
|
}
|
|
else if(canvasGroup == null)
|
|
{
|
|
// Documentation text normalized.
|
|
return;
|
|
}
|
|
|
|
foreach (var item in ui_Anim)
|
|
{
|
|
item.speed = Anim_Speed;
|
|
}
|
|
content_Character_Slot.Get_Childrens_Component<UI_Button_Character_Head_Slot>(true, true);
|
|
|
|
// Load AllyHero_SO data
|
|
string path = Application.isEditor ? editorResourcePath : runtimeResourcePath;
|
|
var loadedData = Resources.LoadAll<AllyHero_SO>(path);
|
|
allyHeroList = new List<AllyHero_SO>(loadedData);
|
|
// Ensure consistent order with UI_Panel_Main
|
|
allyHeroList.Sort((a, b) => a.ally_heroID.CompareTo(b.ally_heroID));
|
|
|
|
for (int i = 0; i < allyHeroList.Count; i++)
|
|
{
|
|
var obj = Instantiate(button_Character_Slot_Prefab, content_Character_Slot);
|
|
var data = allyHeroList[i];
|
|
obj.image.sprite = data.ally_hero_squareProfile;
|
|
if (obj.TryGetComponent(out UI_Button_Character_Head_Slot head_Slot))
|
|
{
|
|
head_Slot.index = i;
|
|
int capturedIndex = i;
|
|
obj.onClick.AddListener(() => Set_Character_Index(capturedIndex));
|
|
}
|
|
}
|
|
}
|
|
public void Set_Character_Index(int index)
|
|
{
|
|
if (UI_Panel_Main.Singleton.Character_Index == index) return;
|
|
UI_Panel_Main.Singleton.Character_Index = index;
|
|
Set_Character();
|
|
}
|
|
private void OnEnable()
|
|
{
|
|
Set_Character();
|
|
}
|
|
Coroutine c_Character_Illustration_Anim;
|
|
public void Set_Character()
|
|
{
|
|
if (c_Character_Illustration_Anim != null)
|
|
{
|
|
StopCoroutine(c_Character_Illustration_Anim);
|
|
}
|
|
if (Image_Character_Illustration.transform.parent.TryGetComponent(out CanvasGroup canvasGroup))
|
|
{
|
|
c_Character_Illustration_Anim = StartCoroutine(C_Character_Illustration_Anim(canvasGroup, Set_Character_Show));
|
|
}
|
|
}
|
|
public void Set_Character_Show()
|
|
{
|
|
int index = UI_Panel_Main.Singleton.Character_Index;
|
|
if (allyHeroList == null || index < 0 || index >= allyHeroList.Count) return;
|
|
|
|
var data = allyHeroList[index];
|
|
var pos1 = Image_Character_Illustration.rectTransform.anchoredPosition;
|
|
Image_Character_Illustration.sprite = data.ally_hero_HD_image;
|
|
DOTween.To(value =>
|
|
{ Image_Character_Illustration.rectTransform.anchoredPosition = new Vector2(value, pos1.y); },
|
|
startValue: Image_Character_Illustration.rectTransform.anchoredPosition.x - 200,
|
|
pos1.x, anim_Time);
|
|
// Image_Character_Illustration.SetNativeSize(); // 注释掉以保持自适应而不改变大小
|
|
var pos = Image_Character_Illustration_BG.rectTransform.anchoredPosition;
|
|
Image_Character_Illustration_BG.sprite = data.ally_hero_HD_image;
|
|
DOTween.To(value =>
|
|
{ Image_Character_Illustration_BG.rectTransform.anchoredPosition = new Vector2(value, pos.y); },
|
|
startValue: Image_Character_Illustration_BG.rectTransform.anchoredPosition.x - 200,
|
|
pos.x, anim_Time);
|
|
// Image_Character_Illustration_BG.SetNativeSize(); // 注释掉以保持自适应而不改变大小
|
|
Image_Character_Illustration_BG.transform.localScale = new(2.7f, 2.7f, 2.7f);
|
|
|
|
|
|
string text = data.ally_heroName;
|
|
var t = DOTween.To(() => string.Empty, value => text_Character_Name.text = value, text, anim_Time).SetEase(Ease.Linear);
|
|
t.SetOptions(true);
|
|
}
|
|
IEnumerator C_Character_Illustration_Anim(CanvasGroup canvasGroup, Action onHide)
|
|
{
|
|
while (canvasGroup.alpha > 0)
|
|
{
|
|
canvasGroup.alpha -= Anim_Speed * Time.deltaTime;
|
|
yield return null;
|
|
}
|
|
onHide?.Invoke();
|
|
while (canvasGroup.alpha < 1)
|
|
{
|
|
canvasGroup.alpha += Anim_Speed * Time.deltaTime;
|
|
yield return null;
|
|
}
|
|
}
|
|
}
|