unity 删除依赖

记录- 

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('\\', '/');
    }
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

钱王拳王

感谢 first big ge

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值