Implement preset path in file dialog.

This commit is contained in:
2022-12-16 17:50:26 +08:00
parent e6d94f248c
commit 5d17555744
4 changed files with 51 additions and 15 deletions

View File

@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
@@ -27,6 +28,12 @@ namespace Cryville.Common.Unity {
set { m_filter = value; }
}
public Dictionary<string, string> m_presetPaths = new Dictionary<string, string>();
public Dictionary<string, string> PresetPaths {
get { return m_presetPaths; }
set { m_presetPaths = value; }
}
GameObject prefabButton;
void Start() {
@@ -68,7 +75,7 @@ namespace Cryville.Common.Unity {
void ChangeDirectory(DirectoryInfo s) {
CurrentDirectory = s;
UpdateGUI(1);
}
}
void SelectFile(string s) {
fileName = s;
@@ -79,23 +86,33 @@ namespace Cryville.Common.Unity {
title.GetComponent<Text>().text = CurrentDirectory.FullName;
if (depth <= 0) {
CallHelper.Purge(drives);
CallHelper.Purge(drives);
#if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN
var dl = Directory.GetLogicalDrives();
foreach (string d in dl) {
var dl = Directory.GetLogicalDrives();
foreach (string d in dl) {
GameObject btn = Instantiate(prefabButton);
btn.GetComponentInChildren<Text>().text = d;
btn.GetComponentInChildren<Text>().text = d;
btn.GetComponentInChildren<Button>().onClick.AddListener(() => ChangeDirectory(new DirectoryInfo(d)));
btn.transform.SetParent(drives, false);
}
btn.transform.SetParent(drives, false);
}
#elif UNITY_ANDROID
GameObject sbtn = GameObject.Instantiate<GameObject>(prefabButton);
sbtn.GetComponentInChildren<Text>().text = "Storage";
sbtn.GetComponentInChildren<Text>().text = "Storage";
sbtn.GetComponentInChildren<Button>().onClick.AddListener(() => OnDriveChanged(new DirectoryInfo(androidStorage)));
sbtn.transform.SetParent(drives, false);
sbtn.transform.SetParent(drives, false);
#else
#error No update GUI logic
#endif
foreach (var p in m_presetPaths) {
var d = new DirectoryInfo(p.Value);
if (d.Exists) {
GameObject btn = Instantiate(prefabButton);
btn.GetComponentInChildren<Text>().text = p.Key;
btn.GetComponentInChildren<Button>().onClick.AddListener(() => ChangeDirectory(d));
btn.transform.SetParent(drives, false);
}
}
}
CallHelper.Purge(dirs);
DirectoryInfo[] subdirs = CurrentDirectory.GetDirectories();
@@ -117,10 +134,10 @@ namespace Cryville.Common.Unity {
foreach (string ext in m_filter) {
if (d.Extension == ext) {
GameObject btn = Instantiate(prefabButton);
btn.GetComponentInChildren<Text>().text = d.Name + " / " + (d.Length / 1024.0).ToString("0.0 KiB");
var ts = d.FullName;
btn.GetComponentInChildren<Text>().text = d.Name + " / " + (d.Length / 1024.0).ToString("0.0 KiB");
var ts = d.FullName;
btn.GetComponentInChildren<Button>().onClick.AddListener(() => SelectFile(ts));
btn.transform.SetParent(files, false);
btn.transform.SetParent(files, false);
break;
}
}