您的位置:首页 > 移动开发 > Unity3D

unity perfab 资源检查

2017-04-14 16:14 267 查看
using UnityEngine;

using UnityEditor;

using System.IO;

using System.Collections.Generic;

using System.Text.RegularExpressions;

public class ResourceTool : EditorWindow

{

    [MenuItem("Test/Resource Tools")]

    static void ShowResourceTool()

    {

        //创建窗口

        Rect rc = new Rect(0, 0, 800, 600);

        ResourceTool window = (ResourceTool)EditorWindow.GetWindow(typeof(ResourceTool), true, "Test Resource Tool");

        window.Show();

    }

    private int selectionGridInt = 0;

    string[] selectionStrings;

    Vector2 scrollPosition;

    Dictionary<string, List<string>> Dependences = new Dictionary<string, List<string>>();

    Dictionary<string, List<string>> References = new Dictionary<string, List<string>>();

    List<string> currentDepends = new List<string>();

    List<string> currentReferences = new List<string>();

    public void Awake()

    {

        this.Dependences.Clear();

        this.References.Clear();

    }

    bool disableSelectEvent = false;

    void OnSelectionChange()

    {

        if (!disableSelectEvent && Selection.activeObject != null && Dependences.Count > 0)

        {

            UnityEngine.Debug.Log("OnSelectionChange:" + Selection.activeObject.name);

            string curpath = AssetDatabase.GetAssetPath(Selection.activeObject);

            this.Dependences.TryGetValue(curpath, out this.currentDepends);

            this.References.TryGetValue(curpath, out this.currentReferences);

            this.Repaint();

        }

        disableSelectEvent = false;

    }

    void SelectAsset(string path)

    {

        disableSelectEvent = true;

        Selection.activeObject = AssetDatabase.LoadMainAssetAtPath(path);

    }

    void OnGUI()

    {

        if (GUILayout.Button("UI Resources Check Mode", GUILayout.Width(200)))

        {

            //perfab 目录可以有多个

            this.GenerateDependence(new string[] { "Assets/Game/Textures", "Assets/Game/Textures/fx/"});

        }

        if (GUILayout.Button("Cleanup UI Resources", GUILayout.Width(200)))

        {

            this.CleanupResources();

        }

        if (GUILayout.Button("Modify Particle Material", GUILayout.Width(200)))

        {

            string[] files = Directory.GetFiles("Assets/FX/Materials");

            foreach (string file in files)

            {

                Material mat = (Material)AssetDatabase.LoadMainAssetAtPath(file);

                if (mat != null)

                {

                    switch (mat.shader.name)

                    {

                        case "Particles/Additive":

                            mat.shader = Shader.Find("Mobile/Particles/Additive");

                            break;

                        case "Particles/Alpha Blended":

                            mat.shader = Shader.Find("Mobile/Particles/Alpha Blended");

                            break;

                        case "Particles/Multiply":

                            mat.shader = Shader.Find("Mobile/Particles/Multiply");

                            break;

                        default:

                            break;

                    }

                }

            }

        }

        //Dependences check area

        scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUIStyle.none);

        GUILayout.BeginArea(new Rect(0, 0, 400, 1000));

        GUILayout.Label("References:");

        if (this.currentReferences != null)

        {

            foreach (string refer in this.currentReferences)

            {

                if (GUILayout.Button(refer, GUILayout.Width(380)))

                {

                    this.SelectAsset(refer);

                }

            }

        }

        GUILayout.EndArea();

        GUILayout.BeginArea(new Rect(400, 0, 400, 1000));

        GUILayout.Label("Depends:");

        if (this.currentDepends != null)

        {

            foreach (string depend in this.currentDepends)

            {

                if (GUILayout.Button(depend, GUILayout.Width(380)))

                {

                    this.SelectAsset(depend);

                }

            }

        }

        GUILayout.EndArea();

        if (selectionStrings != null)

        {

            selectionGridInt = GUILayout.SelectionGrid(selectionGridInt, selectionStrings, 1);

            if (selectionGridInt >= selectionStrings.Length)

                selectionGridInt = selectionStrings.Length - 1;

            if (selectionGridInt > 0)

                Selection.activeObject = AssetDatabase.LoadMainAssetAtPath(selectionStrings[selectionGridInt]);

        }

        GUILayout.EndScrollView();

    }

    void CleanupResources()

    {

        //查找未被使用的资源

        Dictionary<string, List<string>> AssetsRefMap = new Dictionary<string, List<string>>();

        //string[] files = Directory.GetFiles("Assets/Resources/UI/","*", SearchOption.AllDirectories);

        //foreach (string file in files)

        //{

        //    if (file.EndsWith(".meta"))

        //        continue;

        //    if (file.EndsWith(".prefab"))

        //        continue;

        //    Assets[file.Replace("/","/")] = 0;   

        //}

        string UIRES = "Assets/UI_OLD/";

        string[] files = Directory.GetFiles(UIRES, "*", SearchOption.AllDirectories);

        foreach (string file in files)

        {

            if (file.EndsWith(".meta"))

                continue;

            AssetsRefMap[file.Replace("\\", "/")] = new List<string>();

        }

        files = Directory.GetFiles("ProjectSettings/", "*.asset", SearchOption.AllDirectories);

        foreach (string key in files)

        {

            string[] depends = AssetDatabase.GetDependencies(new string[] { key });

            foreach (string dep in depends)

            {

                if (dep != key && AssetsRefMap.ContainsKey(dep))

                {

                    AssetsRefMap[dep].Add(key);

                }

            }

        }

        files = Directory.GetFiles("Assets/BuildOnlyAssets/UI/prefab/", "*.prefab", SearchOption.AllDirectories);

        foreach (string key in files)

        {

            string[] depends = AssetDatabase.GetDependencies(new string[] { key });

            foreach (string dep in depends)

            {

                if (dep != key && AssetsRefMap.ContainsKey(dep))

                {

                    AssetsRefMap[dep].Add(key);

                }

            }

        }

        files = Directory.GetFiles("Assets/Prefabs/", "*.prefab", SearchOption.AllDirectories);

        foreach (string key in files)

        {

            string[] depends = AssetDatabase.GetDependencies(new string[] { key });

            foreach (string dep in depends)

            {

                if (dep != key && AssetsRefMap.ContainsKey(dep))

                {

                    AssetsRefMap[dep].Add(key);

                }

            }

        }

        List<string> result = new List<string>();

        foreach (KeyValuePair<string, List<string>> kv in AssetsRefMap)

        {

            if (kv.Value.Count == 0)

            {

                //result.Add(kv.Key);

                File.Delete(kv.Key);

            }

            else

            {

                result.Add(kv.Key);

                foreach (string file in kv.Value)

                {

                    result.Add(file);

                }

            }

        }

        this.selectionStrings = result.ToArray();

    }

    void CheckDependsFX()

    {

        //查找未被使用的资源

        Dictionary<string, int> Assets = new Dictionary<string, int>();

        //string[] files = Directory.GetFiles("Assets/Resources/UI/","*", SearchOption.AllDirectories);

        //foreach (string file in files)

        //{

        //    if (file.EndsWith(".meta"))

        //        continue;

        //    if (file.EndsWith(".prefab"))

        //        continue;

        //    Assets[file.Replace("/","/")] = 0;   

        //}

        string[] files = Directory.GetFiles("Assets/UI/", "*", SearchOption.AllDirectories);

        foreach (string file in files)

        {

            if (file.EndsWith(".meta"))

                continue;

            Assets[file.Replace("\\", "/")] = 0;

        }

        files = Directory.GetFiles("Assets/Resources/UI/prefab/", "*.prefab", SearchOption.AllDirectories);

        foreach (string key in files)

        {

            string[] depends = AssetDatabase.GetDependencies(new string[] { key });

            foreach (string dep in depends)

            {

                if (dep != key && Assets.ContainsKey(dep))

                {

                    Assets[dep]++;

                }

            }

        }

        List<string> result = new List<string>();

        foreach (KeyValuePair<string, int> kv in Assets)

        {

            if (kv.Value == 0)

                result.Add(kv.Key);

        }

        this.selectionStrings = result.ToArray();

    }

    public void GenerateDependence(string[] paths)

    {

        Debug.Log("GenerateDependence");

        //查找未被使用的资源

        List<string> files = new List<string>();

        foreach (string path in paths)

        {

            files.AddRange(Directory.GetFiles(path, "*.prefab", SearchOption.AllDirectories));

        }

        for (int i = 0; i < files.Count; i++)

        {

            string key = files[i];

            string asset = key.Replace('\\', '/');

            string[] depends = AssetDatabase.GetDependencies(new string[] { asset });

            foreach (string dep in depends)

            {

                if (dep != asset)

                {

                    AddDepends(asset, dep);

                }

            }

            EditorUtility.DisplayProgressBar("Collect Dependences", asset, (float)i / (float)files.Count);

        }

        EditorUtility.ClearProgressBar();

    }

    void AddDepends(string asset, string depend)

    {

        if (!this.Dependences.ContainsKey(asset))

            this.Dependences[asset] = new List<string>();

        if (!this.Dependences[asset].Contains(depend))

            this.Dependences[asset].Add(depend);

        if (!this.References.ContainsKey(depend))

            this.References[depend] = new List<string>();

        if (!this.References[depend].Contains(asset))

            this.References[depend].Add(asset);

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: