QuestPanel.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using DataTools;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class QuestPanel : MonoBehaviour
  5. {
  6. [SerializeField]
  7. private DataGame data;
  8. [SerializeField]
  9. private QuestUIData questUIDataPrefab;
  10. [SerializeField]
  11. private Transform content;
  12. private int currentChapter = -1;
  13. private DataTools.Quest currentQuest;
  14. private List<QuestUIData> questUIDatas;
  15. private void Awake()
  16. {
  17. questUIDatas = new List<QuestUIData>();
  18. }
  19. private void Start()
  20. {
  21. GameGlobal.Instance.OnQuestChanged += OnQuestChanged;
  22. }
  23. private void OnQuestChanged(DataTools.Quest quest)
  24. {
  25. if(currentQuest != null)
  26. {
  27. QuestUIData questUIData = questUIDatas.Find(q => q.Id == quest.Id);
  28. if (questUIData)
  29. questUIData.Completed();
  30. }
  31. currentQuest = quest;
  32. }
  33. private void OnEnable()
  34. {
  35. if (currentChapter != GameGlobal.Instance.CurrentChapter)
  36. {
  37. CleanData();
  38. currentChapter = GameGlobal.Instance.CurrentChapter;
  39. foreach (DataTools.Quest quest in data.GameData.Chapters[currentChapter].Quests)
  40. {
  41. QuestUIData questUIData = Instantiate(questUIDataPrefab, content);
  42. questUIData.Setup(quest);
  43. questUIDatas.Add(questUIData);
  44. }
  45. }
  46. }
  47. private void CleanData()
  48. {
  49. foreach (QuestUIData quest in questUIDatas)
  50. Destroy(quest.gameObject);
  51. questUIDatas.Clear();
  52. }
  53. }