FileOpenDialog.cs 3.5 KB

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