Guest User

Untitled

a guest
Jul 18th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.SceneManagement;
  3. using UnityEngine.UI;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6. using System.Collections.Generic;
  7.  
  8.  
  9.  
  10. public class MenuSetup : MonoBehaviour {
  11.  
  12.  
  13. //Creates buttons when loaded
  14. public GameObject ChapterButtons;
  15. GameObject ChapterButtonClone;
  16. //Button Text for chapters
  17. Text ButtonText;
  18.  
  19.  
  20.  
  21.  
  22. // Use this for initialization
  23. void Start()
  24. {
  25.  
  26.  
  27. //sceneCount Converts the Total amount of scenes into a number
  28. int sceneCount = UnityEngine.SceneManagement.SceneManager.sceneCountInBuildSettings;
  29. List<string> scenes = new List<string>(new string[sceneCount]);
  30. int[] sceneLoader = new int[sceneCount];
  31.  
  32. //int [] sceneLoader = new int[sceneCount];
  33.  
  34.  
  35. //Converts Scene list into String list
  36. for (int i = 0; i < sceneCount; i++)
  37. {
  38. scenes[i] = System.IO.Path.GetFileNameWithoutExtension(UnityEngine.SceneManagement.SceneUtility.GetScenePathByBuildIndex(i));
  39.  
  40. //print("Before: " + scenes[i]);
  41. }
  42.  
  43. //Sorts String into a list
  44. //Calls the function PadNumbers to So it can Sort naturaly (Example: 3, 11, 24 - instead of : 11, 24, 3)
  45. var sortedList = scenes.OrderBy(x => PadNumbers(x)).ToList();
  46.  
  47.  
  48.  
  49. //Counts through the list
  50. for (int i = 0; i < sceneCount;i++)
  51. {
  52.  
  53.  
  54.  
  55. //Every Scene that has the Keyword, Creates Buttons/hyperText
  56. if (sortedList[i].StartsWith("Chapter"))
  57. {
  58.  
  59.  
  60. ChapterButtonClone = Instantiate(ChapterButtons) as GameObject;
  61. ChapterButtonClone.transform.SetParent(GameObject.Find("MainMenuPanel").transform);
  62. ChapterButtonClone.transform.localScale = new Vector3(1, 1, 1);
  63. ButtonText = ChapterButtonClone.transform.Find("Text").GetComponent<Text>();
  64. ChapterButtonClone.GetComponent<Button>().onClick.AddListener(() => SceneManager.LoadScene(test));
  65. ChapterButtonClone.name = sortedList[i];
  66. ButtonText.text = sortedList[i];
  67.  
  68.  
  69.  
  70. }
  71.  
  72. }
  73.  
  74.  
  75. }
  76.  
  77. //Function used to sort the list "naturally" correctly
  78. public string PadNumbers(string sortedList)
  79. {
  80. return Regex.Replace(sortedList, "[0-9]+", match => match.Value.PadLeft(10, '0'));
  81. }
  82.  
  83.  
  84.  
  85. }
Add Comment
Please, Sign In to add comment