ULockWidget.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using System;
  2. using Excel2Json;
  3. using Fort23.Core;
  4. using Fort23.Mono;
  5. using Fort23.UTool;
  6. using UnityEngine;
  7. using UnityEngine.EventSystems;
  8. using UnityEngine.UI;
  9. public class ULockWidget : MonoBehaviour, IPointerClickHandler
  10. {
  11. private int uLockId;
  12. public int uLockFunction;
  13. public Action ulockCallBack;
  14. private UnlockConfig _config;
  15. private bool _currULock;
  16. private Button mUIButton;
  17. private bool mIsUnlock;
  18. private void Awake()
  19. {
  20. if (uLockFunction == 0)
  21. return;
  22. StartULock();
  23. if (mUIButton == null) return;
  24. }
  25. public void SetuLockFunction(int id)
  26. {
  27. uLockFunction = id;
  28. if (uLockFunction == 0)
  29. {
  30. mIsUnlock = true;
  31. UnlockShow(mIsUnlock);
  32. return;
  33. }
  34. StartULock();
  35. if (mUIButton == null) return;
  36. }
  37. public void StartULock()
  38. {
  39. if (uLockId <= 0)
  40. {
  41. _config = ULockManager.Instance.GetULockConfig(uLockFunction);
  42. }
  43. else
  44. {
  45. _config = ULockManager.Instance.GetULockConfig(uLockId, false);
  46. }
  47. mUIButton = transform.GetComponentInChildren<Button>();
  48. EventManager.Instance.RemoveEventListener(CustomEventType.RefreshULock, RefreshULock);
  49. EventManager.Instance.AddEventListener(CustomEventType.RefreshULock, RefreshULock);
  50. ULock();
  51. }
  52. public void UnlockCallBack()
  53. {
  54. ulockCallBack?.Invoke();
  55. }
  56. private void OnDestroy()
  57. {
  58. EventManager.Instance.RemoveEventListener(CustomEventType.RefreshULock, RefreshULock);
  59. ulockCallBack = null;
  60. }
  61. private void RefreshULock(IEventData e)
  62. {
  63. ULock();
  64. }
  65. private void ULock()
  66. {
  67. if (_config.ID == 0)
  68. {
  69. mIsUnlock = true;
  70. return;
  71. }
  72. mIsUnlock = ULockManager.Instance.IsULock(_config);
  73. UnlockShow(mIsUnlock);
  74. }
  75. /// <summary>
  76. /// 解锁的表现逻辑
  77. /// </summary>
  78. /// <param name="isUnlock">是否解锁</param>
  79. private void UnlockShow(bool isUnlock)
  80. {
  81. mIsUnlock = isUnlock;
  82. if (gameObject == null) return;
  83. Text[] texts = gameObject.GetComponentsInChildren<Text>();
  84. if (!isUnlock)
  85. {
  86. if (_config.lockType == 1)
  87. {
  88. gameObject.SetActive(false);
  89. }
  90. else if (_config.lockType == 2)
  91. {
  92. if (mUIButton != null)
  93. {
  94. mUIButton.enabled = false;
  95. }
  96. else
  97. {
  98. LogTool.Log("没有碰撞区域" + gameObject.name);
  99. }
  100. gameObject.transform.Gray(true);
  101. }
  102. _currULock = true;
  103. }
  104. else
  105. {
  106. if (_config.lockType == 1)
  107. {
  108. gameObject.SetActive(true);
  109. }
  110. else if (_config.lockType == 2)
  111. {
  112. if (mUIButton != null)
  113. {
  114. mUIButton.enabled = true;
  115. }
  116. //TODO 恢复正常颜色
  117. gameObject.transform.RecoverColor();
  118. }
  119. UnlockCallBack();
  120. EventManager.Instance.RemoveEventListener(CustomEventType.RefreshULock, RefreshULock);
  121. }
  122. }
  123. public void OnPointerClick(PointerEventData eventData)
  124. {
  125. LogTool.Log("点击~");
  126. if (!mIsUnlock)
  127. {
  128. TipMessagePanel.OpenTipMessagePanel(ULockManager.Instance.ShowUnlockTips(_config.ID), Vector2.zero);
  129. }
  130. }
  131. }