SoftMaskSampleChooser.cs 1.5 KB

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