123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- 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
- {
- /// <summary>
- /// 文件窗口,自动生成文件清单
- /// </summary>
- 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<FileListWindow>(typeof(SceneView));
- window.titleContent = new GUIContent("File Window");
- window.Show();
- }
- /// <summary>
- ///
- /// </summary>
- 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<DataInfo> list = table.FindAllData<DataInfo>();
- 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<DataInfo> list = XSql.Instance.ReadServerData<DataInfo>(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<DataInfo>();
- 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 = "";
- }
- }
- }
- }
- }
- }
|