JJBB/Assets/Editor/Scripts/FindTableByRes.cs
2024-08-23 15:49:34 +08:00

232 lines
7.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using System;
// 选择单选或多选Project下素材寻找在配置表中出现的位置
// 寻找位置为表中类型为 STRING 的列
// 结果存放位置Application.streamingAssetsPath/FindTableByRes_Result.text
// Todo: 使用文件夹寻找/递归文件夹寻找
public class FindTableByRes
{
private static List<string> targets = new List<string>(); // 待找的素材名称(无后缀)
private static List<int> keyWordIndexs = new List<int>(); // 每个.tex文档的 string 列索引
private static Dictionary<int, List<string>> totalResult = new Dictionary<int, List<string>>(); // 总的结果
private static string tablePaht = Application.dataPath + "/Project3D/BundleData/Tables3D"; // 表的路径
private static string resultPath = Application.streamingAssetsPath + "/FindTableByRes_Result.txt";
private static string[] searchPatterns = new string[] { "*.png" };
private static int MaxTargetCount = 300; // 最高允许的查询数目,数目过多会使等待时间增加。
private static bool isNew = false;
[MenuItem("ProTool/ResTool/UnUseRes", true)]
private static bool Check()
{
if(Selection.objects.Length == 0)
{
return false;
}
else
{
return true;
}
}
[MenuItem("ProTool/ResTool/UnUseRes")]
static private void BeginTask()
{
isNew = true;
Init();
if(targets.Count > MaxTargetCount)
{
Debug.Log("Too much target , " + targets.Count);
return;
}
if (targets.Count == 0)
{
return;
}
string[] files = Directory.GetFiles(tablePaht, "*.txt");
for (int t = 0; t < targets.Count; ++t)
{
if (string.IsNullOrEmpty(targets[t]))
{
return;
}
List<string> results = new List<string>();
for (int i = 0; i < files.Length; ++i)
{
//Debug.Log("ProTool: working!");
string[] lines = File.ReadAllLines(files[i]);
if (lines.Length == 0)
{
return;
}
keyWordIndexs.Clear();
// 寻找关键列index
string[] curColNames = lines[1].Split('\t');
for (int j = 0; j < curColNames.Length; ++j)
{
if (string.Compare(curColNames[j], "string", true) == 0)
{
keyWordIndexs.Add(j);
}
}
// 更具index查找相应名称
if (keyWordIndexs.Count > 0)
{
for (int j = 1; j < lines.Length; ++j)
{
if (lines[j][0] == '#')
continue;
string[] lineWords = lines[j].Split('\t');
for (int k = 0; k < keyWordIndexs.Count; ++k)
{
int index = keyWordIndexs[k];
if (string.Compare(lineWords[index], targets[t], true) == 0)
{
results.Add(files[i] + "\t" + "at line " + j.ToString());
}
}
}
}
}
if (results.Count > 0)
{
totalResult.Add(t, results);
}
else
{
results.Add("None");
totalResult.Add(t, results);
}
}
OutputResult();
}
private static void Init()
{
targets.Clear();
keyWordIndexs.Clear();
totalResult.Clear();
//for(int i = 0; i < Selection.assetGUIDs.Length; ++i)
//{
// Debug.Log(AssetDatabase.GUIDToAssetPath(Selection.assetGUIDs[i]));
//}
foreach (UnityEngine.Object t in Selection.objects)
{
string path = AssetDatabase.GetAssetPath(t.GetInstanceID());
string absolutePath = Application.dataPath.Substring(0, Application.dataPath.LastIndexOf('/')) + "/" + path + "/";
if (Directory.Exists(absolutePath) == true)
{
for (int i = 0; i < searchPatterns.Length; ++i)
{
string[] temp = Directory.GetFiles(absolutePath, searchPatterns[i], SearchOption.AllDirectories);
for(int j = 0; j < temp.Length; ++j)
{
targets.Add(GetName(temp[j]));
}
}
}
else
{
targets.Add(t.name);
}
}
}
static private string GetName(string path)
{
string result;
if (path.LastIndexOf('\\') != -1)
{
result = path.Substring(path.LastIndexOf('\\') + 1, path.LastIndexOf('.') - path.LastIndexOf('\\') - 1);
}
else
{
result = path.Substring(path.LastIndexOf('/') + 1, path.LastIndexOf('.') - path.LastIndexOf('/') - 1);
}
return result;
}
static private void OutputResult()
{
int resultCount = 0;
foreach(KeyValuePair<int, List<string>> kv in totalResult)
{
resultCount += kv.Value.Count;
}
if(resultCount <= 5)
{
Debug.Log("ProTool: Done!");
foreach (KeyValuePair<int, List<string>> kv in totalResult)
{
foreach (string s in kv.Value)
{
Debug.Log(targets[kv.Key] + " : " + s);
}
}
}
else
{
try
{
FileStream fs = new FileStream(resultPath, FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
// 优先输出为结果为空的素材
foreach (KeyValuePair<int, List<string>> kv in totalResult)
{
if(kv.Value[0] == "None")
{
sw.WriteLine(targets[kv.Key]);
sw.WriteLine("\t" + kv.Value[0]);
sw.Write("\n");
}
}
// 再输出结果不为空的素材
foreach (KeyValuePair<int, List<string>> kv in totalResult)
{
if (kv.Value[0] != "None")
{
sw.WriteLine(targets[kv.Key]);
foreach (string s in kv.Value)
{
sw.WriteLine("\t" + s);
}
sw.Write("\n");
}
}
sw.Close();
fs.Close();
Debug.Log("ProTool: Done!");
Debug.Log("ProTool: Too much result, check at " + resultPath);
}
catch (Exception e)
{
Debug.Log(e.ToString());
}
}
}
}