Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using UnityEngine.UI;
- using System.Linq;
- using System.Text.RegularExpressions;
- using System.Collections.Generic;
- public class MenuSetup : MonoBehaviour {
- //Creates buttons when loaded
- public GameObject ChapterButtons;
- GameObject ChapterButtonClone;
- //Button Text for chapters
- Text ButtonText;
- // Use this for initialization
- void Start()
- {
- //sceneCount Converts the Total amount of scenes into a number
- int sceneCount = UnityEngine.SceneManagement.SceneManager.sceneCountInBuildSettings;
- List<string> scenes = new List<string>(new string[sceneCount]);
- int[] sceneLoader = new int[sceneCount];
- //int [] sceneLoader = new int[sceneCount];
- //Converts Scene list into String list
- for (int i = 0; i < sceneCount; i++)
- {
- scenes[i] = System.IO.Path.GetFileNameWithoutExtension(UnityEngine.SceneManagement.SceneUtility.GetScenePathByBuildIndex(i));
- //print("Before: " + scenes[i]);
- }
- //Sorts String into a list
- //Calls the function PadNumbers to So it can Sort naturaly (Example: 3, 11, 24 - instead of : 11, 24, 3)
- var sortedList = scenes.OrderBy(x => PadNumbers(x)).ToList();
- //Counts through the list
- for (int i = 0; i < sceneCount;i++)
- {
- //Every Scene that has the Keyword, Creates Buttons/hyperText
- if (sortedList[i].StartsWith("Chapter"))
- {
- ChapterButtonClone = Instantiate(ChapterButtons) as GameObject;
- ChapterButtonClone.transform.SetParent(GameObject.Find("MainMenuPanel").transform);
- ChapterButtonClone.transform.localScale = new Vector3(1, 1, 1);
- ButtonText = ChapterButtonClone.transform.Find("Text").GetComponent<Text>();
- ChapterButtonClone.GetComponent<Button>().onClick.AddListener(() => SceneManager.LoadScene(test));
- ChapterButtonClone.name = sortedList[i];
- ButtonText.text = sortedList[i];
- }
- }
- }
- //Function used to sort the list "naturally" correctly
- public string PadNumbers(string sortedList)
- {
- return Regex.Replace(sortedList, "[0-9]+", match => match.Value.PadLeft(10, '0'));
- }
- }
Add Comment
Please, Sign In to add comment