123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- using System;
- using System.Collections;
- using System.IO;
- using System.Linq;
- using UnityEngine;
- using UnityEngine.Events;
- using UnityEngine.UI;
- namespace TriLib.Samples
- {
- public class FileOpenDialog : MonoBehaviour
- {
- public static FileOpenDialog Instance { get; private set; }
- public string Title
- {
- get
- {
- return this._headerText.text;
- }
- set
- {
- this._headerText.text = value;
- }
- }
- private event FileOpenEventHandle OnFileOpen;
- public void ShowFileOpenDialog(FileOpenEventHandle onFileOpen)
- {
- this.OnFileOpen = onFileOpen;
- this.ReloadItemNames();
- this._fileLoaderRenderer.SetActive(true);
- }
- public void HideFileOpenDialog()
- {
- this.DestroyItems();
- this._fileLoaderRenderer.SetActive(false);
- }
- public void HandleEvent(ItemType itemType, string filename)
- {
- if (itemType != ItemType.ParentDirectory)
- {
- if (itemType != ItemType.Directory)
- {
- this.OnFileOpen(Path.Combine(this._directory, filename));
- this.HideFileOpenDialog();
- }
- else
- {
- this._directory = filename;
- this.ReloadItemNames();
- }
- }
- else
- {
- DirectoryInfo parent = Directory.GetParent(this._directory);
- if (parent != null)
- {
- this._directory = parent.FullName;
- this.ReloadItemNames();
- }
- else
- {
- this.ShowDirectoryNames();
- }
- }
- }
- public void DestroyItems()
- {
- IEnumerator enumerator = this._containerTransform.GetEnumerator();
- try
- {
- while (enumerator.MoveNext())
- {
- object obj = enumerator.Current;
- Transform transform = (Transform)obj;
- UnityEngine.Object.Destroy(transform.gameObject);
- }
- }
- finally
- {
- IDisposable disposable;
- if ((disposable = (enumerator as IDisposable)) != null)
- {
- disposable.Dispose();
- }
- }
- }
- protected void Awake()
- {
- this._directory = Path.GetDirectoryName(Application.dataPath);
- this._closeButton.onClick.AddListener(new UnityAction(this.HideFileOpenDialog));
- FileOpenDialog.Instance = this;
- }
- private void ReloadItemNames()
- {
- this.DestroyItems();
- this.CreateItem(ItemType.ParentDirectory, "[Parent Directory]");
- string[] directories = Directory.GetDirectories(this._directory);
- foreach (string text in directories)
- {
- this.CreateItem(ItemType.Directory, text);
- }
- string[] array2 = Directory.GetFiles(this._directory, "*.*");
- if (!string.IsNullOrEmpty(this.Filter) && this.Filter != "*.*")
- {
- array2 = (from x in array2
- where this.Filter.Contains(Path.GetExtension(x).ToLower())
- select x).ToArray<string>();
- }
- foreach (string path in array2)
- {
- this.CreateItem(ItemType.File, Path.GetFileName(path));
- }
- }
- private void ShowDirectoryNames()
- {
- this.DestroyItems();
- string[] logicalDrives = Directory.GetLogicalDrives();
- foreach (string text in logicalDrives)
- {
- this.CreateItem(ItemType.Directory, text);
- }
- }
- private void CreateItem(ItemType itemType, string text)
- {
- FileText fileText = UnityEngine.Object.Instantiate<FileText>(this._fileTextPrefab, this._containerTransform);
- fileText.ItemType = itemType;
- fileText.Text = text;
- }
- public string Filter = "*.*";
- [SerializeField]
- private Transform _containerTransform;
- [SerializeField]
- private FileText _fileTextPrefab;
- [SerializeField]
- private GameObject _fileLoaderRenderer;
- [SerializeField]
- private Button _closeButton;
- [SerializeField]
- private Text _headerText;
- private string _directory;
- }
- }
|