LevelSelector.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using System.Collections;
  4. public class LevelSelector : MonoBehaviour
  5. {
  6. public GameObject levelButtonPrefab;
  7. public GameObject parent;
  8. public List<ButtonLevelSelector> ButtonLevelSelectors = new List<ButtonLevelSelector>();
  9. const int OFFSET = 140;
  10. const int BUTTON_SPACING = 120;
  11. void Start()
  12. {
  13. //PlayerPrefs.SetInt("Level", 5);
  14. int numOfLevels = LevelsManager.Instance.levels.Length;
  15. int posY = OFFSET;
  16. for (int i = 0; i < numOfLevels; i++)
  17. {
  18. Transform button = NGUITools.AddChild(parent, levelButtonPrefab).transform;
  19. Vector3 pos = new Vector3(0, posY, 0);
  20. button.localPosition = pos;
  21. posY -= BUTTON_SPACING;
  22. var label = button.GetComponentInChildren<UILabel>();
  23. label.text = string.Format(Localization.instance.Get("levelButton.text"), i+1); //+1 since players prefer 1-based
  24. ButtonLevelSelector buttonLevelSelector = button.GetComponent<ButtonLevelSelector>();
  25. buttonLevelSelector.levelIndex = i;
  26. ButtonLevelSelectors.Add(buttonLevelSelector);
  27. }
  28. ActivateCountButtonLevel();
  29. }
  30. void ActivateCountButtonLevel()
  31. {
  32. int playerLevel = PlayerPrefs.GetInt("Level");
  33. if(playerLevel==0)
  34. {
  35. PlayerPrefs.SetInt("Level", 1);
  36. }
  37. playerLevel = PlayerPrefs.GetInt("Level");
  38. //Debug.Log(playerLevel);
  39. for (int i = 0; i < ButtonLevelSelectors.Count; i++)
  40. {
  41. if(i>playerLevel-1)
  42. {
  43. ButtonLevelSelectors[i].ButtonWithTwoState.DeactivateButton();
  44. }
  45. else
  46. {
  47. ButtonLevelSelectors[i].ButtonWithTwoState.ActivateButton();
  48. }
  49. }
  50. }
  51. void OnEnable()
  52. {
  53. //Debug.Log("ActivateCountButtonLevel");
  54. ActivateCountButtonLevel();
  55. }
  56. /*void Update()
  57. {
  58. }*/
  59. }