void LoadWorlds() { worldsManager = GameObject.Find("/Managers/WorldsManager").GetComponent <WorldsManager>(); mapInfos = worldsManager.GetWorldList().ToArray(); mapSelectionButtons = new MapSelectionButton[mapInfos.Length]; foreach (Transform child in mapsHolder) { Destroy(child.gameObject); } int i = 0; foreach (var mapInfo in mapInfos) { GameObject newMap = Instantiate(mapPrefab, mapsHolder); MapSelectionButton mapSelectionButton = newMap.GetComponent <MapSelectionButton>(); mapSelectionButtons[i] = mapSelectionButton; mapSelectionButton.Init(mapInfo.name, mapInfo.folderName, mapInfo.date); Func <int, Action> CreateClickAction = (index) => () => SelectMap(index); mapSelectionButton.OnClicked += CreateClickAction(i); i++; } }
private void CreateUI() { try { string[] maps = Directory.GetDirectories(Application.persistentDataPath + "/maps"); for (int i = 0; i < maps.Length; i++) { // Text Object displaying map name GameObject textObj = Instantiate(textPrefab); uiObjects.Add(textObj); textObj.transform.SetParent(transform); rt = textObj.GetComponent <RectTransform>(); rt.anchoredPosition = new Vector2(30.0f, -15.0f - 60.0f * i); rt.localScale = new Vector2(1.0f / 0.7f, 1.0f / 0.6f); Text t = textObj.GetComponent <Text>(); t.text = maps[i].Replace(Application.persistentDataPath + "/maps\\", ""); // Load Map Button GameObject loadButtonObj = Instantiate(loadButtonPrefab); uiObjects.Add(loadButtonObj); loadButtonObj.transform.SetParent(transform); rt = loadButtonObj.GetComponent <RectTransform>(); rt.anchoredPosition = new Vector2(-270.0f, -15.0f - 60.0f * i); rt.localScale = new Vector2(1.0f / 0.7f, 1.0f / 0.6f); MapSelectionButton btnLoad = loadButtonObj.GetComponent <MapSelectionButton>(); btnLoad.path = maps[i]; // Delete Map Button GameObject deleteButtonObj = Instantiate(deleteButtonPrefab); uiObjects.Add(deleteButtonObj); deleteButtonObj.transform.SetParent(transform); rt = deleteButtonObj.GetComponent <RectTransform>(); rt.anchoredPosition = new Vector2(-30.0f, -15.0f - 60.0f * i); rt.localScale = new Vector2(1.0f / 0.7f, 1.0f / 0.6f); MapSelectionButton btnDelete = deleteButtonObj.GetComponent <MapSelectionButton>(); btnDelete.path = maps[i]; } } catch (IOException) { print("Error : cannot find " + Application.persistentDataPath + "/maps directory"); } }