using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; using System.IO; using System.Linq; using System; using PublicTools.XMLDataBase; using XRTool.UI; namespace XRTool.Util { /// /// 文件窗口,自动生成文件清单 /// public class FileListWindow : EditorWindow { private Rect rect; private string targetPath; private string fullPath; private string ignoreExtension = ".meta .cs"; private string[] fileTypes = new string[] { ".xml", ".json" }; private int selectIndex; [MenuItem("XRTool/Utilities/File Window", false, 0)] public static void OpenWindow() { // Dock it next to the Scene View. var window = GetWindow(typeof(SceneView)); window.titleContent = new GUIContent("File Window"); window.Show(); } /// /// /// private void OnGUI() { rect = EditorGUILayout.GetControlRect(GUILayout.Width(800)); targetPath = EditorGUI.TextField(rect, "目标路径", targetPath); fullPath = EditorGUILayout.TextField("全路径", fullPath); ignoreExtension = EditorGUILayout.TextField("忽略文件类型", ignoreExtension, GUILayout.Width(300)); selectIndex = EditorGUILayout.Popup(selectIndex, fileTypes); if (GUILayout.Button("测试读取1", GUILayout.Width(300))) { string sqlPath = Application.streamingAssetsPath; if (XSql.Instance.Open(sqlPath, true)) { string tableName = typeof(DataInfo).Name + fileTypes[selectIndex]; TableInterface table = XSql.Instance.OpenTable(tableName, fileTypes[selectIndex], true); if (table != null) { if (!table.Open()) { return; } List list = table.FindAllData(); if (list != null) { for (int i = 0; i < list.Count; i++) { UnityLog.Instance.Log(list[i].Path + list[i].Version); } } table.Close(); } } XSql.Instance.Close(); } if (GUILayout.Button("测试读取2", GUILayout.Width(300))) { string sqlPath = Application.streamingAssetsPath; string tableName = typeof(DataInfo).Name + fileTypes[selectIndex]; string path = Path.Combine(sqlPath, tableName); string data = File.ReadAllText(path); UnityLog.Instance.Log(data); List list = XSql.Instance.ReadServerData(data); if (list != null) { for (int i = 0; i < list.Count; i++) { UnityLog.Instance.Log(list[i].Path + list[i].Version); } } } if (GUILayout.Button("清除", GUILayout.Width(300))) { string sqlPath = Application.streamingAssetsPath; if (XSql.Instance.Open(sqlPath, true)) { string tableName = typeof(DataInfo).Name + fileTypes[selectIndex]; TableInterface table = XSql.Instance.OpenTable(tableName, fileTypes[selectIndex], true); if (table != null) { if (!table.Open()) { return; } table.DeleteAllData(); table.Close(); } } XSql.Instance.Close(); } if (GUILayout.Button("刷新路径", GUILayout.Width(300))) { if (Directory.Exists(fullPath)) { string sqlPath = Application.streamingAssetsPath; if (XSql.Instance.Open(sqlPath, true)) { string tableName = typeof(DataInfo).Name + fileTypes[selectIndex]; TableInterface table = XSql.Instance.OpenTable(tableName, fileTypes[selectIndex], true); if (table != null) { if (!table.Open()) { UnityLog.Instance.Log("表格打开失败,尝试创建", 2); if (!table.Create(typeof(DataInfo).Name)) { Debug.LogError("创建表格失败"); return; } } Uri tableUri = new Uri(Path.Combine(sqlPath, tableName)); DataFileUtil.FindFileBreadth(fullPath, file => { var uri = new Uri(file); if (uri != tableUri) { string path = file.Replace("\\", "/"); if (!DataFileUtil.isIgnoreExtension(ignoreExtension, Path.GetExtension(path))) { if (path.Contains(Application.dataPath)) { path = path.Substring(Application.dataPath.Length + 1); } string prikey = path.Replace("/", "."); DataInfo info = new DataInfo(); info.PriKey = prikey; info.Path = path; info.Version = DataFileUtil.GetMD5HashFromFile(file); table.InsertData(info); } } }); table.Close(); } else { Debug.Log("Table is null"); } XSql.Instance.Close(); } } } if ((Event.current.type == EventType.DragUpdated || Event.current.type == EventType.DragExited) && rect.Contains(Event.current.mousePosition)) { //改变鼠标的外表 DragAndDrop.visualMode = DragAndDropVisualMode.Generic; if (DragAndDrop.paths != null && DragAndDrop.paths.Length > 0) { targetPath = DragAndDrop.paths[0]; string path = targetPath.Replace("Assets/", ""); if (Directory.Exists(path)) { fullPath = path; } else { fullPath = Path.Combine(Application.dataPath, path); } if (File.Exists(fullPath)) { fullPath = Path.GetDirectoryName(fullPath); } if (!Directory.Exists(fullPath)) { fullPath = ""; } } } } } }