using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; using Newtonsoft.Json; public class LevelSelect : MonoBehaviour { public GameObject levelPrefab; public int LevelCount = 25; public static Dictionary levelProgress; // public static Dictionary levelProgress { // get{ // if(m_levelProgress == null){ // if(!PlayerPrefs.HasKey("progress") ){ // Debug.Log("No key for progress found, generating new"); // m_levelProgress=new Dictionary(); // }else{ // try{ // m_levelProgress= JsonConvert.DeserializeObject>(PlayerPrefs.GetString("progress")); // }catch{ // Debug.LogError("Failed parsing progress from save, Creating new save"); // m_levelProgress = new Dictionary(); // } // } // } // return m_levelProgress; // }set{ // m_levelProgress=value; // // Debug.Log() // PlayerPrefs.SetString("progress", JsonConvert.SerializeObject(value)); // PlayerPrefs.Save(); // Debug.Log("saving: " +JsonConvert.SerializeObject(value)); // } // } public static void SetLevel(int level, int stars){ if(levelProgress == null){ levelProgress = new Dictionary(); } if(levelProgress.ContainsKey(level)){ levelProgress[level] = stars; }else{ levelProgress.Add(level,stars); } Debug.Log(JsonConvert.SerializeObject(levelProgress)); PlayerPrefs.SetString("progress", JsonConvert.SerializeObject(levelProgress)); PlayerPrefs.Save(); } public Sprite locked,unlocked; public static void Init(){ if(PlayerPrefs.HasKey("progress")){ string savedata = PlayerPrefs.GetString("progress"); levelProgress = JsonConvert.DeserializeObject>(savedata); Debug.Log(savedata); }else{ levelProgress = new Dictionary(); } } void Start() { // levelProgress.Add(0,2); Init(); //Populate level selection for(int i=0; i < LevelCount;i++){ int curIndex = i; GameObject newLevel = Instantiate(levelPrefab, levelPrefab.transform.parent); newLevel.transform.GetChild(0).GetComponent().text = $"Level {i+1}"; if(i==0){ newLevel.transform.GetChild(1).GetComponent().sprite = unlocked; if(levelProgress.ContainsKey(0)){ newLevel.transform.GetChild(2).gameObject.SetActive(true); for(int x =0; x < 3; x++){ newLevel.transform.GetChild(2).GetChild(x).GetComponent().color = (levelProgress[i] > x) ? Color.yellow : Color.gray; } }else{ } }else{ if(levelProgress.ContainsKey(i-1)){ newLevel.transform.GetChild(2).gameObject.SetActive(true); newLevel.transform.GetChild(1).GetComponent().sprite = unlocked; if(levelProgress.ContainsKey(i)){ for(int x =0; x < 3; x++){ newLevel.transform.GetChild(2).GetChild(x).GetComponent().color = (levelProgress[i] > x) ? Color.yellow : Color.gray; } } }else{ newLevel.transform.GetChild(1).GetComponent().sprite = locked; newLevel.GetComponent