FileOpenDialog.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System;
  2. using System.Collections;
  3. using System.IO;
  4. using System.Linq;
  5. using UnityEngine;
  6. using UnityEngine.Events;
  7. using UnityEngine.UI;
  8. namespace TriLib.Samples
  9. {
  10. public class FileOpenDialog : MonoBehaviour
  11. {
  12. public static FileOpenDialog Instance { get; private set; }
  13. public string Title
  14. {
  15. get
  16. {
  17. return this._headerText.text;
  18. }
  19. set
  20. {
  21. this._headerText.text = value;
  22. }
  23. }
  24. private event FileOpenEventHandle OnFileOpen;
  25. public void ShowFileOpenDialog(FileOpenEventHandle onFileOpen)
  26. {
  27. this.OnFileOpen = onFileOpen;
  28. this.ReloadItemNames();
  29. this._fileLoaderRenderer.SetActive(true);
  30. }
  31. public void HideFileOpenDialog()
  32. {
  33. this.DestroyItems();
  34. this._fileLoaderRenderer.SetActive(false);
  35. }
  36. public void HandleEvent(ItemType itemType, string filename)
  37. {
  38. if (itemType != ItemType.ParentDirectory)
  39. {
  40. if (itemType != ItemType.Directory)
  41. {
  42. this.OnFileOpen(Path.Combine(this._directory, filename));
  43. this.HideFileOpenDialog();
  44. }
  45. else
  46. {
  47. this._directory = filename;
  48. this.ReloadItemNames();
  49. }
  50. }
  51. else
  52. {
  53. DirectoryInfo parent = Directory.GetParent(this._directory);
  54. if (parent != null)
  55. {
  56. this._directory = parent.FullName;
  57. this.ReloadItemNames();
  58. }
  59. else
  60. {
  61. this.ShowDirectoryNames();
  62. }
  63. }
  64. }
  65. public void DestroyItems()
  66. {
  67. IEnumerator enumerator = this._containerTransform.GetEnumerator();
  68. try
  69. {
  70. while (enumerator.MoveNext())
  71. {
  72. object obj = enumerator.Current;
  73. Transform transform = (Transform)obj;
  74. UnityEngine.Object.Destroy(transform.gameObject);
  75. }
  76. }
  77. finally
  78. {
  79. IDisposable disposable;
  80. if ((disposable = (enumerator as IDisposable)) != null)
  81. {
  82. disposable.Dispose();
  83. }
  84. }
  85. }
  86. protected void Awake()
  87. {
  88. this._directory = Path.GetDirectoryName(Application.dataPath);
  89. this._closeButton.onClick.AddListener(new UnityAction(this.HideFileOpenDialog));
  90. FileOpenDialog.Instance = this;
  91. }
  92. private void ReloadItemNames()
  93. {
  94. this.DestroyItems();
  95. this.CreateItem(ItemType.ParentDirectory, "[Parent Directory]");
  96. string[] directories = Directory.GetDirectories(this._directory);
  97. foreach (string text in directories)
  98. {
  99. this.CreateItem(ItemType.Directory, text);
  100. }
  101. string[] array2 = Directory.GetFiles(this._directory, "*.*");
  102. if (!string.IsNullOrEmpty(this.Filter) && this.Filter != "*.*")
  103. {
  104. array2 = (from x in array2
  105. where this.Filter.Contains(Path.GetExtension(x).ToLower())
  106. select x).ToArray<string>();
  107. }
  108. foreach (string path in array2)
  109. {
  110. this.CreateItem(ItemType.File, Path.GetFileName(path));
  111. }
  112. }
  113. private void ShowDirectoryNames()
  114. {
  115. this.DestroyItems();
  116. string[] logicalDrives = Directory.GetLogicalDrives();
  117. foreach (string text in logicalDrives)
  118. {
  119. this.CreateItem(ItemType.Directory, text);
  120. }
  121. }
  122. private void CreateItem(ItemType itemType, string text)
  123. {
  124. FileText fileText = UnityEngine.Object.Instantiate<FileText>(this._fileTextPrefab, this._containerTransform);
  125. fileText.ItemType = itemType;
  126. fileText.Text = text;
  127. }
  128. public string Filter = "*.*";
  129. [SerializeField]
  130. private Transform _containerTransform;
  131. [SerializeField]
  132. private FileText _fileTextPrefab;
  133. [SerializeField]
  134. private GameObject _fileLoaderRenderer;
  135. [SerializeField]
  136. private Button _closeButton;
  137. [SerializeField]
  138. private Text _headerText;
  139. private string _directory;
  140. }
  141. }