记录-
Scene中有依赖废弃的资源。
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;
public class FindDependences{
[MenuItem("Assets/Find Story References", false, 10)]
static private void Find()
{
EditorSettings.serializationMode = SerializationMode.ForceText;
// string path = AssetDatabase.GetAssetPath(Selection.activeObject);
List<string> uuids = FindSceneDep();
if (uuids != null)
{
//string guid = AssetDatabase.AssetPathToGUID(path);
string[] files = FindDepsFiles();
EditorApplication.update = delegate()
{
int startIndex = 0;
for (int i = 0; i < uuids.Count; i++)
{
bool isCancel = EditorUtility.DisplayCancelableProgressBar("匹配资源中", uuids[i], (float)startIndex / (float)uuids.Count);
startIndex++;
for (int j = 0; j < files.Length; j++)
{
if (Regex.IsMatch(File.ReadAllText(files[j]), uuids[i]))
{
string uuid = GetObjUUID(GetRelativeAssetsPath(files[j]));
//Debug.Log(file, AssetDatabase.LoadAssetAtPath<Object>(GetRelativeAssetsPath(file)));
ReadFile(SceneManager.GetActiveScene().path, uuid);
}
}
}
//if (isCancel || startIndex >= files.Length)
{
EditorUtility.ClearProgressBar();
EditorApplication.update = null;
startIndex = 0;
Debug.Log("匹配结束");
}
};
}
}
static string[] FindDepsFiles() {
List<string> withoutExtensions = new List<string>() { ".prefab", ".unity", ".mat", ".asset", ".playable" };
string[] files = Directory.GetFiles(Application.dataPath + "/Games/Story/", "*.*", SearchOption.AllDirectories)
.Where(s => withoutExtensions.Contains(Path.GetExtension(s).ToLower())).ToArray();
return files;
}
//查找scnes
static List<string> FindSceneDep()
{
List<string> uuids = new List<string>();
UnityEngine.Object[] selectedAsset = Selection.GetFiltered<UnityEngine.Object>(SelectionMode.DeepAssets);
for (int i = 0; i < selectedAsset.Length; i++)
{
UnityEngine.Object o = selectedAsset[i];
string path = AssetDatabase.GetAssetPath(o);
uuids.Add( AssetDatabase.AssetPathToGUID(path));
}
return uuids;
}
static void ReadFile(string path,string uuid) {
try
{
Debug.LogError(path);
//FileStream file = new FileStream(path, FileMode.OpenOrCreate);
//StreamReader sr = new StreamReader(file, Encoding.Default);
String[] lines = System.IO.File.ReadAllLines(path);
List<string> listS = new List<string>(lines);
for (int i = listS.Count - 1; i >= 0; i--)
{
if (listS[i].Contains(uuid))
{
Debug.LogError("delet " + uuid);
listS.RemoveAt(i + 1);
listS.RemoveAt(i);
}
}
string[] str = listS.ToArray();
System.IO.File.WriteAllLines(path, str);
}
catch (Exception e) {
Debug.LogError(e.ToString());
}
}
static string GetObjUUID(string file){
Debug.Log(AssetDatabase.AssetPathToGUID(file));
return AssetDatabase.AssetPathToGUID(file);
}
static private string GetRelativeAssetsPath(string path)
{
return "Assets" + Path.GetFullPath(path).Replace(Path.GetFullPath(Application.dataPath), "").Replace('\\', '/');
}
}