using System.Collections; using System.Collections.Generic; using UnityEngine; using DG.Tweening; using UnityEngine.UI; public class CardUIManager : MonoBehaviour { [SerializeField] private CanvasGroup canvasGroup; [SerializeField] private PuzzleCardItem m_cardItemPrefab; [SerializeField] private Transform m_cardItemParent; [SerializeField] private List m_cardList; [SerializeField] private ButtonInteraction m_selectBtn; [SerializeField] private Transform m_newCardParent; [SerializeField] private PuzzleTarotCardItem m_tarotCardItemPrefab; [SerializeField] private PuzzleTarotCardsConfig m_tarotCardList; private List m_cardPool; [SerializeField] private PuzzleExplainUI PuzzleExplainUI; private int currCardIndex; private List puzzleCardItemList; public void Init(PuzzleDrawTarot PuzzleDrawTarot) { currCardIndex = 0; gameObject.SetActive(true); canvasGroup.alpha = 0f; canvasGroup.DOFade(1f, 1f); if(puzzleCardItemList==null) { puzzleCardItemList= new List(); } for(int j=0;j(); } for(int j=0;j(); puzzleCardItemList.Add(currItem); } m_selectBtn.Init(SelectCard); m_cardPool = new List(m_tarotCardList.Cards); } public void SelectCard() { if(currCardIndex>=puzzleCardItemList.Count)return; PuzzleCardAnimItem currSelectItem = FindNearestImage(); currSelectItem.Show(false); StartCoroutine(DelayShow(currSelectItem)); GameObject go = Instantiate(currSelectItem.gameObject,m_newCardParent); go.transform.position = currSelectItem.transform.position; PuzzleCardAnimItem newItem = go.GetComponent(); newItem.MoveTo(puzzleCardItemList[currCardIndex]); StartCoroutine(DelayCreateTarotCardItem(newItem,currCardIndex)); currCardIndex++; } List puzzleTarotCardItemList; IEnumerator DelayCreateTarotCardItem(PuzzleCardAnimItem newItem, int currIndex) { GameObject go = Instantiate(m_tarotCardItemPrefab.gameObject,m_newCardParent); go.transform.position = puzzleCardItemList[currCardIndex].transform.position; PuzzleTarotCardItem tarotCardItem = go.GetComponent(); PuzzleTarotCard randomTarotCard = GetTarotCard(); tarotCardItem.Init(randomTarotCard); puzzleTarotCardItemList.Add(tarotCardItem); m_cardPool.Remove(randomTarotCard); go.transform.localEulerAngles = new Vector3(0,90,0); yield return new WaitForSeconds(1f); newItem.transform.DORotate(new Vector3(0,90,0),1f).SetEase(Ease.Linear); yield return new WaitForSeconds(1f); go.transform.DORotate(new Vector3(0,0,0),1f).SetEase(Ease.Linear); yield return new WaitForSeconds(1f); if(currIndex>=puzzleCardItemList.Count-1) { canvasGroup.DOFade(0f, 1f).OnComplete(()=> { gameObject.SetActive(false); PuzzleExplainUI.Init(puzzleTarotCardItemList); }); } } PuzzleTarotCard GetTarotCard() { int currIndex = Random.Range(0, m_cardPool.Count); return m_cardPool[currIndex]; } IEnumerator DelayShow(PuzzleCardAnimItem currSelectItem) { yield return new WaitForSeconds(2f); currSelectItem.Show(true); } public PuzzleCardAnimItem FindNearestImage() { if (m_cardList.Count == 0) { Debug.LogError("Image列表为空!"); return null; } PuzzleCardAnimItem nearestImage = m_cardList[0]; float minDistance = Mathf.Abs(nearestImage.GetComponent().position.x - m_selectBtn.GetComponent().position.x); foreach (PuzzleCardAnimItem image in m_cardList) { float distance = Mathf.Abs(image.GetComponent().position.x - m_selectBtn.GetComponent().position.x); if (distance < minDistance) { minDistance = distance; nearestImage = image; } } return nearestImage; } }