ButtonWithTwoStates.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using UnityEngine;
  2. using System.Collections;
  3. /// <summary>
  4. /// This component is responsible for toggling between two sprites.
  5. /// The sprites should be created as NGUI sprites, and should be named as follows
  6. /// BackgroundNormal - for the unpressed normal state
  7. /// BackgroundPressed - for the pressed state
  8. /// </summary>
  9. public class ButtonWithTwoStates : MonoBehaviour
  10. {
  11. GameObject normal, pressed;
  12. //public bool LevelButton;
  13. private BoxCollider _colider;
  14. public bool Toggle;
  15. public bool StartMenu;
  16. void Awake()
  17. {
  18. _colider = GetComponent<BoxCollider>();
  19. normal = transform.Find("BackgroundNormal").gameObject;
  20. pressed = transform.Find("BackgroundPressed").gameObject;
  21. DeactivateButton();
  22. }
  23. void OnPress(bool isPressed)
  24. {
  25. normal.SetActive(!isPressed);
  26. pressed.SetActive(isPressed);
  27. }
  28. public void OnEnable()
  29. {
  30. if (StartMenu)
  31. {
  32. //Debug.Log(PlayerPrefs.GetInt("GetCoupon"));
  33. if (PlayerPrefs.GetInt("GetCoupon") == 1)
  34. {
  35. ActivateButton();
  36. }
  37. else
  38. {
  39. DeactivateButton();
  40. }
  41. }
  42. else
  43. {
  44. //Debug.Log(PlayerPrefs.GetInt("GetCoupon"));
  45. if (PlayerPrefs.GetInt("GetCoupon") == 1 )
  46. {
  47. ActivateButton();
  48. }
  49. else
  50. {
  51. DeactivateButton();
  52. }
  53. }
  54. }
  55. public void DeactivateButton()
  56. {
  57. if (normal == null)
  58. {
  59. return;
  60. }
  61. normal.SetActive(false);
  62. pressed.SetActive(true);
  63. _colider.enabled = false;
  64. }
  65. public void ActivateButton()
  66. {
  67. if (normal == null)
  68. {
  69. return;
  70. }
  71. normal.SetActive(true);
  72. pressed.SetActive(false);
  73. _colider.enabled = true;
  74. }
  75. }