using System; using System.Collections.Generic; using System.IO; using UnityEngine; using UnityEngine.UI; namespace Cryville.Common.Unity { public class FileDialog : MonoBehaviour { Transform panel; Transform title; Transform drives; Transform dirs; Transform files; public event Action OnClose; #if UNITY_ANDROID && !UNITY_EDITOR_WIN string androidStorage = ""; #endif string fileName = null; public string FileName { get { return fileName; } } public string[] m_filter = new string[]{}; public string[] Filter { set { m_filter = value; } } public IReadOnlyDictionary m_presetPaths = new Dictionary(); public IReadOnlyDictionary PresetPaths { get { return m_presetPaths; } set { m_presetPaths = value; } } GameObject prefabButton; void Start() { prefabButton = Resources.Load("Common/Button"); panel = gameObject.transform.Find("Panel"); title = panel.Find("Title/Text"); drives = panel.Find("Drives/DrivesInner"); dirs = panel.Find("Directories/DirectoriesInner"); files = panel.Find("Files/FilesInner"); if (CurrentDirectory == null) { #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN CurrentDirectory = new DirectoryInfo(Directory.GetCurrentDirectory()); #elif UNITY_ANDROID using (AndroidJavaClass ajc = new AndroidJavaClass("android.os.Environment")) using (AndroidJavaObject file = ajc.CallStatic("getExternalStorageDirectory")) { androidStorage = file.Call("getAbsolutePath"); CurrentDirectory = new DirectoryInfo(androidStorage); } #else #error No default directory #endif } UpdateGUI(0); } public void Show() { fileName = null; gameObject.SetActive(true); } public void Close() { var ev = OnClose; if (ev != null) ev.Invoke(); gameObject.SetActive(false); } public DirectoryInfo CurrentDirectory; void ChangeDirectory(DirectoryInfo s) { CurrentDirectory = s; UpdateGUI(1); } void SelectFile(string s) { fileName = s; Close(); } void UpdateGUI(int depth) { title.GetComponent().text = CurrentDirectory.FullName; if (depth <= 0) { CallHelper.Purge(drives); #if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN var dl = Directory.GetLogicalDrives(); foreach (string d in dl) { GameObject btn = Instantiate(prefabButton); btn.GetComponentInChildren().text = d; btn.GetComponentInChildren