UIButtonHandler.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class UIButtonHandler : MonoBehaviour
  6. {
  7. public GameObject target;
  8. public string functionName;
  9. public bool includeChildren = false;
  10. private void Start()
  11. {
  12. GetComponent<Button>().onClick.AddListener(Send);
  13. }
  14. void Send()
  15. {
  16. var curLive = PlayerPrefs.GetInt("Live");
  17. if (LivesManager.Instance.Lives == 0 && gameObject.name == "ButtonRequest")
  18. {
  19. //Debug.Log("curLive " + curLive);
  20. if (curLive == 1)
  21. {
  22. //MediaPlayerCtrl.Instance.PlayAd();
  23. MenuManager._instance.CurState = MenuManager.StateAd.Life;
  24. #if UNITY_EDITOR
  25. LivesManager.Instance.RechargeLife();
  26. #endif
  27. }
  28. }
  29. if (string.IsNullOrEmpty(functionName)) return;
  30. if (target == null) target = gameObject;
  31. if (includeChildren)
  32. {
  33. Transform[] transforms = target.GetComponentsInChildren<Transform>();
  34. for (int i = 0, imax = transforms.Length; i < imax; ++i)
  35. {
  36. Transform t = transforms[i];
  37. t.gameObject.SendMessage(functionName, gameObject, SendMessageOptions.DontRequireReceiver);
  38. }
  39. }
  40. else
  41. {
  42. target.SendMessage(functionName, gameObject, SendMessageOptions.DontRequireReceiver);
  43. }
  44. }
  45. }