LevelSelector.cs 1.8 KB

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