Unity C# 检索prefab、animation、scenes未使用的脚本

using UnityEngine;
using System;
using System.IO;
using System.Text;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Core;
using UnityEditor;
using UnityEngine.UI;
using System.Collections.Generic;

public static class CheckPrefabScript {
    public static List scriptStrs = new List();

    public static List unUsed = new List();

    [MenuItem("Tools/Check all prefab Script")]
    public static void DoCheck() {
        string[] prefabPath = AssetDatabase.FindAssets("t:Prefab", new string[] { "Assets" });
        string[] scenePath = AssetDatabase.FindAssets("t:Scene", new string[] { "Assets" });
        string[] animationPath = AssetDatabase.FindAssets("t:Animation", new string[] { "Assets" });

        List allPath = new List();
        Debug.Log(prefabPath.Length + "-" + scenePath.Length + "-" + animationPath.Length);
        for(int i = 0; i < prefabPath.Length; i++) {
            string path = AssetDatabase.GUIDToAssetPath(prefabPath[i]);
            var obj = AssetDatabase.LoadAssetAtPath(path, typeof(GameObject)) as GameObject;
            if(obj != null) {
                try {
                    string[] lines = File.ReadAllLines(path);
                    for(int j = 0; j < lines.Length; j++) {
                        if(lines[j].Contains("m_Script:") && lines[j].Contains("80fc9aa3f08635c46a963cf07336bf14")) {
                            string id = lines[j].Split(',')[0].Split(':')[2].Trim();
                            if(!scriptStrs.Contains(id)) {
                                scriptStrs.Add(id);
                            }
                        }
                    }

                } catch(System.Exception e) {
                    ADebug.LogError(e.Message);
                }
            }
        }
        for(int i = 0; i < scenePath.Length; i++) {
            string path = AssetDatabase.GUIDToAssetPath(scenePath[i]);
            try {
                string[] lines = File.ReadAllLines(path);
                for(int j = 0; j < lines.Length; j++) {
                    if(lines[j].Contains("m_Script:") && lines[j].Contains("80fc9aa3f08635c46a963cf07336bf14")) {
                        string id = lines[j].Split(',')[0].Split(':')[2].Trim();
                        if(!scriptStrs.Contains(id)) {
                            scriptStrs.Add(id);
                        }
                    }
                }

            } catch(System.Exception e) {
                ADebug.LogError(e.Message);
            }
        }

        for(int i = 0; i < animationPath.Length; i++) {

            string path = AssetDatabase.GUIDToAssetPath(animationPath[i]);
            //Debug.Log(path);
            try {
                string[] lines = File.ReadAllLines(path);
                for(int j = 0; j < lines.Length; j++) {
                    if(lines[j].Contains("script:") && lines[j].Contains("80fc9aa3f08635c46a963cf07336bf14")) {
                        string id = lines[j].Split(',')[0].Split(':')[2].Trim();
                        if(!scriptStrs.Contains(id)) {
                            scriptStrs.Add(id);
                        }
                    }
                }

            } catch(System.Exception e) {
                ADebug.LogError(e.Message);
            }
        }


        Type[] types = typeof(ViewActivityRechargeTalentLogic).Assembly.GetTypes();
        //Debug.Log(types);
        for(int i = 0; i < types.Length; i++) {
            if(types[i].IsSubclassOf(typeof(MonoBehaviour))) {

                if(!scriptStrs.Contains(FileIDUtil.Compute(types[i]).ToString())) {
                    unUsed.Add(FileIDUtil.Compute(types[i]).ToString() + "  " + types[i].Name);
                }
                //else {
                //  Debug.Log(FileIDUtil.Compute(types[i]).ToString() + " >> " + types[i].Name);
                // }
            }
        }

        Save(unUsed, "CheckPrefabScript_unUsed");
        // Save(scriptStrs, "CheckPrefabScript_Script");
    }

    public static void Save(List list, string name) {
        string localpath = Path.Combine(PluginKits.PersistentDataPath, name);
        if(!Directory.Exists(localpath)) {
            Directory.CreateDirectory(localpath);
        }
        string msg = "";
        for(int i = 0; i < list.Count; i++) {
            if(i > 0)
                msg += "\r\n";
            msg += list[i];
        }
        WriteIntoTxt(localpath, msg, name);
        Debug.Log(name + " Do End >> " + list.Count);
    }

    static StreamWriter writer;
    //把所有的数据写入文本中
    public static void WriteIntoTxt(string path, string message, string name) {

        FileInfo file = new FileInfo(path + "/" + name + ".txt");
        if(!file.Exists) {
            writer = file.CreateText();
        } else {
            file.Delete();
            writer = file.CreateText();
        }
        writer.Write(message);
        writer.Close();
        writer.Dispose();

    }
}

 

using System;
using System.Linq;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Reflection;
using UnityEditor;

// Taken from http://www.superstarcoders.com/blogs/posts/md4-hash-algorithm-in-c-sharp.aspx
// Probably not the best implementation of MD4, but it works.
public class MD4 : HashAlgorithm {
    private uint _a;
    private uint _b;
    private uint _c;
    private uint _d;
    private uint[] _x;
    private int _bytesProcessed;

    public MD4() {
        _x = new uint[16];

        Initialize();
    }

    public override void Initialize() {
        _a = 0x67452301;
        _b = 0xefcdab89;
        _c = 0x98badcfe;
        _d = 0x10325476;

        _bytesProcessed = 0;
    }

    protected override void HashCore(byte[] array, int offset, int length) {
        ProcessMessage(Bytes(array, offset, length));
    }

    protected override byte[] HashFinal() {
        try {
            ProcessMessage(Padding());

            return new[] { _a, _b, _c, _d } .SelectMany(word => Bytes(word)).ToArray();
        } finally {
            Initialize();
        }
    }

    private void ProcessMessage(IEnumerable bytes) {
        foreach(byte b in bytes) {
            int c = _bytesProcessed & 63;
            int i = c >> 2;
            int s = (c & 3) << 3;

            _x[i] = (_x[i] & ~((uint)255 << s)) | ((uint)b << s);

            if(c == 63) {
                Process16WordBlock();
            }

            _bytesProcessed++;
        }
    }

    private static IEnumerable Bytes(byte[] bytes, int offset, int length) {
        for(int i = offset; i < length; i++) {
            yield return bytes[i];
        }
    }

    private IEnumerable Bytes(uint word) {
        yield return (byte)(word & 255);
        yield return (byte)((word >> 8) & 255);
        yield return (byte)((word >> 16) & 255);
        yield return (byte)((word >> 24) & 255);
    }

    private IEnumerable Repeat(byte value, int count) {
        for(int i = 0; i < count; i++) {
            yield return value;
        }
    }

    private IEnumerable Padding() {
        return Repeat(128, 1)
               .Concat(Repeat(0, ((_bytesProcessed + 8) & 0x7fffffc0) + 55 - _bytesProcessed))
               .Concat(Bytes((uint)_bytesProcessed << 3))
               .Concat(Repeat(0, 4));
    }

    private void Process16WordBlock() {
        uint aa = _a;
        uint bb = _b;
        uint cc = _c;
        uint dd = _d;

        foreach(int k in new[] { 0, 4, 8, 12 }) {
            aa = Round1Operation(aa, bb, cc, dd, _x[k], 3);
            dd = Round1Operation(dd, aa, bb, cc, _x[k + 1], 7);
            cc = Round1Operation(cc, dd, aa, bb, _x[k + 2], 11);
            bb = Round1Operation(bb, cc, dd, aa, _x[k + 3], 19);
        }

        foreach(int k in new[] { 0, 1, 2, 3 }) {
            aa = Round2Operation(aa, bb, cc, dd, _x[k], 3);
            dd = Round2Operation(dd, aa, bb, cc, _x[k + 4], 5);
            cc = Round2Operation(cc, dd, aa, bb, _x[k + 8], 9);
            bb = Round2Operation(bb, cc, dd, aa, _x[k + 12], 13);
        }

        foreach(int k in new[] { 0, 2, 1, 3 }) {
            aa = Round3Operation(aa, bb, cc, dd, _x[k], 3);
            dd = Round3Operation(dd, aa, bb, cc, _x[k + 8], 9);
            cc = Round3Operation(cc, dd, aa, bb, _x[k + 4], 11);
            bb = Round3Operation(bb, cc, dd, aa, _x[k + 12], 15);
        }

        unchecked {
            _a += aa;
            _b += bb;
            _c += cc;
            _d += dd;
        }
    }

    private static uint ROL(uint value, int numberOfBits) {
        return (value << numberOfBits) | (value >> (32 - numberOfBits));
    }

    private static uint Round1Operation(uint a, uint b, uint c, uint d, uint xk, int s) {
        unchecked {
            return ROL(a + ((b & c) | (~b & d)) + xk, s);
        }
    }

    private static uint Round2Operation(uint a, uint b, uint c, uint d, uint xk, int s) {
        unchecked {
            return ROL(a + ((b & c) | (b & d) | (c & d)) + xk + 0x5a827999, s);
        }
    }

    private static uint Round3Operation(uint a, uint b, uint c, uint d, uint xk, int s) {
        unchecked {
            return ROL(a + (b ^ c ^ d) + xk + 0x6ed9eba1, s);
        }
    }
}

public static class FileIDUtil {
    public static int Compute(Type t) {
        string toBeHashed = "s\0\0\0" + t.Namespace + t.Name;

        using(HashAlgorithm hash = new MD4()) {
            byte[] hashed = hash.ComputeHash(System.Text.Encoding.UTF8.GetBytes(toBeHashed));

            int result = 0;

            for(int i = 3; i >= 0; --i) {
                result <<= 8;
                result |= hashed[i];
            }

            return result;
        }
    }

    public static int GetFileID(UnityEngine.Object obj) {
        PropertyInfo inspectorModeInfo = typeof(SerializedObject).GetProperty("inspectorMode", BindingFlags.NonPublic | BindingFlags.Instance);
        SerializedObject serializedObject = new SerializedObject(obj);

        inspectorModeInfo.SetValue(serializedObject, InspectorMode.Debug, null);
        SerializedProperty localIdProp = serializedObject.FindProperty("m_LocalIdentfierInFile");
        int localId = localIdProp.intValue;

        return localId;
    }

    public static int GetFileType(UnityEngine.Object obj) {
        int hideFlags = (int)obj.hideFlags;
        bool notEditable = (hideFlags & (int)UnityEngine.HideFlags.NotEditable) != 0;
        bool isDefaultAsset = (hideFlags & ((int)UnityEngine.HideFlags.HideAndDontSave + (int)UnityEngine.HideFlags.HideInInspector)) != 0;

        if(hideFlags == 0)
            return 2;

        if(notEditable)
            return 3;

        if(isDefaultAsset)
            return 0;

        // not sure
        return 1;

    }
}

 

你可能感兴趣的:(Unity3D)