DynamicMsg.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace SoftKitty.InventoryEngine
  6. {
  7. /// <summary>
  8. /// This module displays popup messages and large flashy icons of items when players acquire new items.
  9. /// </summary>
  10. public class DynamicMsg : MonoBehaviour
  11. {
  12. #region Variables
  13. private static DynamicMsg instance;
  14. public GameObject MsgPrefab;
  15. public GameObject ItemPrefab;
  16. private List<CanvasGroup> MsgList = new List<CanvasGroup>();
  17. private float _alpha = 1F;
  18. #endregion
  19. #region Internal Methods
  20. public void ShowMsg(string _text)
  21. {
  22. GameObject newObj = Instantiate(MsgPrefab, MsgPrefab.transform.parent);
  23. newObj.transform.localScale = Vector3.one;
  24. newObj.GetComponentInChildren<Text>().text = _text;
  25. MsgList.Add(newObj.GetComponent<CanvasGroup>());
  26. newObj.SetActive(true);
  27. transform.SetAsLastSibling();
  28. SoundManager.Play2D("msg");
  29. }
  30. public void ShowItem(Item _item, int _number)
  31. {
  32. if (_item == null) return;
  33. GameObject newObj = Instantiate(ItemPrefab, ItemPrefab.transform.parent);
  34. newObj.transform.localScale = Vector3.one;
  35. newObj.GetComponent<ItemIcon>().SetAppearance(_item.Copy(), true, true);
  36. newObj.GetComponent<ItemIcon>().SetItemNumber(_number);
  37. newObj.GetComponent<ItemIcon>().SetUpgradeLevel(_item.upgradeLevel);
  38. newObj.SetActive(true);
  39. transform.SetAsLastSibling();
  40. SoundManager.Play2D("reward");
  41. }
  42. void Update()
  43. {
  44. _alpha = 1F;
  45. for (int i = MsgList.Count - 1; i >= 0; i--)
  46. {
  47. if (MsgList[i] == null)
  48. {
  49. MsgList.RemoveAt(i);
  50. }
  51. else
  52. {
  53. MsgList[i].alpha = _alpha;
  54. _alpha -= 0.2F;
  55. }
  56. }
  57. }
  58. private static void CreateInstance()
  59. {
  60. GameObject newObj = Instantiate(Resources.Load<GameObject>("InventoryEngine/DynamicMsg"), WindowsManager.GetMainCanvas().transform);
  61. newObj.transform.SetAsLastSibling();
  62. newObj.transform.localPosition = Vector3.zero;
  63. newObj.transform.localScale = Vector3.one;
  64. instance = newObj.GetComponent<DynamicMsg>();
  65. }
  66. #endregion
  67. /// <summary>
  68. /// Displays a message with the provided text.
  69. /// </summary>
  70. /// <param name="_text"></param>
  71. public static void PopMsg(string _text)
  72. {
  73. if (instance == null)CreateInstance();
  74. instance.ShowMsg(_text);
  75. }
  76. /// <summary>
  77. /// Shows a flashy big icon of an item.
  78. /// </summary>
  79. /// <param name="_item"></param>
  80. /// <param name="_number"></param>
  81. public static void PopItem(Item _item, int _number=1)
  82. {
  83. if (instance == null) CreateInstance();
  84. instance.ShowItem(_item, _number);
  85. }
  86. }
  87. }