SoftMaskSampleChooser.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System.IO;
  2. using System.Linq;
  3. using TMPro;
  4. #if UNITY_EDITOR
  5. using UnityEditor;
  6. #endif
  7. using UnityEngine;
  8. using UnityEngine.SceneManagement;
  9. using UnityEngine.UI;
  10. namespace SoftMasking.Samples {
  11. public class SoftMaskSampleChooser : MonoBehaviour {
  12. public Dropdown dropdown;
  13. public TextMeshProUGUI fallbackLabel;
  14. public void Start() {
  15. var activeSceneName = SceneManager.GetActiveScene().name;
  16. #if UNITY_EDITOR
  17. dropdown.options.RemoveAll(x => !IsSceneInBuild(x.text));
  18. if (dropdown.options.Count == 0)
  19. Fallback(activeSceneName);
  20. #endif
  21. var currentSampleIndex = dropdown.options.FindIndex(x => x.text == activeSceneName);
  22. if (currentSampleIndex >= 0) {
  23. dropdown.value = currentSampleIndex;
  24. dropdown.onValueChanged.AddListener(Choose);
  25. } else
  26. Fallback(activeSceneName);
  27. }
  28. void Fallback(string activeSceneName) {
  29. dropdown.gameObject.SetActive(false);
  30. fallbackLabel.gameObject.SetActive(true);
  31. fallbackLabel.text = activeSceneName;
  32. }
  33. public void Choose(int sampleIndex) {
  34. var sceneName = dropdown.options[sampleIndex].text;
  35. SceneManager.LoadScene(sceneName);
  36. }
  37. #if UNITY_EDITOR
  38. bool IsSceneInBuild(string sceneName) {
  39. return
  40. EditorBuildSettings.scenes.Any(
  41. s => Path.GetFileNameWithoutExtension(s.path) == sceneName && s.enabled);
  42. }
  43. #endif
  44. }
  45. }