EditorTools分析

其实就是分析一下EditorTools.cs文件。
文件所在目录为:Assets/Editor/EditorTools/EditorTools.cs

在编辑器中就是这样子的:
这里写图片描述

有三个函数:

//查找Resource资源中miss的脚本
    //这里的Resource指的是prefab
    //查找Assets/Resources/目录下的资源
    [MenuItem("Tools/FindMissScriptInResource")]
    public static void FindMissScriptInResource()
//检测Assets/Resources/effect/目录下的Effect是否有效
    //其实就是检查effect上的脚本是否缺失
    [MenuItem("Tools/CheckEffectInResource")]
    public static void CheckEffectInResource()
//检测Material材质在Resource资源中的所有引用关
    //就是检查选中的Material在那些资源里面被引用了,会Log出来.
    //一次只能选择一个Material
    //具体使用方法:
    //Editor内选中一个material,然后点击Tools下的CheckMaterialReference,
    //接下来会卡住一会,因为没写dialog啊,最后会Log出来引用的信息
    [MenuItem("Tools/CheckMaterialReference")]
    public static void CheckMaterialReference()
然后接下来是所有代码贴出来:
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System.IO;

public class EditorTools : MonoBehaviour {

    static int go_count = 0;
    static int components_count = 0;
    static int missing_count = 0;
    static List<string> mResources = new List<string>();        //里面放Assets/Resources/目录下的所有prefab
    static List<string> mEffectResources = new List<string>();  //存放Assets/Resources/effect/目录下的effect prefab

    //查找Resource资源中miss的脚本
    //这里的Resource指的是prefab
    [MenuItem("Tools/FindMissScriptInResource")]
    public static void FindMissScriptInResource()
    {
        go_count = 0;
        components_count = 0;
        missing_count = 0;

        mResources.Clear();

        //Resource资源路径
        string resourcePath = Application.dataPath + "/Resources/";

        string[] files = Directory.GetFiles(resourcePath, "*.*", SearchOption.AllDirectories);
        foreach (string file in files)
        {
            //这里其实排除meta文件有点多余,
            //因为BuildCommon.getFileSuffix(file)获得的后缀是真正的后缀,
            //例如GameLogin.prefab.meta,suffix是meta,而不会是prefab,
            //既然suffix是meta,则下面的“查找预制件”操作会完全跳过
            //但是还是觉得不改
            string suffix = BuildCommon.getFileSuffix(file);
            if (suffix == "meta")
                continue;

            //查找预制件件
            if (suffix == "prefab")
            {
                string realFile = file.Replace("\\", "/");
                realFile = realFile.Replace(Application.dataPath, "Assets");
                mResources.Add(realFile);       
            }
        }

        //查找所有miss文件
        foreach(string assetPath in mResources)
        {
            GameObject asset = AssetDatabase.LoadMainAssetAtPath(assetPath) as GameObject;
            FindInGO(asset);    
        }
        Debug.Log(string.Format("Searched {0} GameObjects, {1} components, found {2} missing", go_count, components_count, missing_count));
    }

    //检测Assets/Resources/effect/目录下的Effect是否有效
    //其实就是检查effect上的脚本是否缺失
    [MenuItem("Tools/CheckEffectInResource")]
    public static void CheckEffectInResource()
    {
        //获取所有资源路径       
        mEffectResources.Clear();

        //effect的资源路径
        //这目录下全是effect的prefab文件
        string resourcePath = Application.dataPath + "/Resources/effect";

        string[] files = Directory.GetFiles(resourcePath, "*.*", SearchOption.AllDirectories);
        foreach (string file in files)
        {
            //这里貌似也和上面的一样
            string suffix = BuildCommon.getFileSuffix(file);
            if (suffix == "meta")
                continue;

            //查找预制件件
            if (suffix == "prefab")
            {
                string realFile = file.Replace("\\", "/");
                realFile = realFile.Replace(Application.dataPath, "Assets");
                mEffectResources.Add(realFile);
            }
        }

        List<ParticleSystem> pss = new List<ParticleSystem>();
        //查找是否缺少EffectScript
        foreach (string assetPath in mEffectResources)
        {
            GameObject asset = AssetDatabase.LoadMainAssetAtPath(assetPath) as GameObject;

            //检测是否缺少EffectScript脚本
            EffectScript effectScript = asset.GetComponent<EffectScript>();
            if (effectScript == null)
                Debug.Log("the effect " + assetPath + " miss EffectScript!");

            //SL_DestroyByTime这个类已经被注释掉了,不使用了
            SL_DestroyByTime destTimeScript = asset.GetComponent<SL_DestroyByTime>();
            if (destTimeScript != null)
            {
                Debug.Log("do not use the SL_DestoryByTime script now!");                    
            }

            pss.Clear();
            //检测ParticleSystem
            RecusionParticleSystemObject(asset, ref pss);
            foreach(ParticleSystem ps in pss)
            {
                if (ps.renderer.castShadows || ps.renderer.receiveShadows)
                {
                    Debug.Log(asset.name + " " + ps.name + "is cast shadow or receive shadow");
                }         
            }         
        }
        Debug.Log("Check finised!");  
    }

    //读取gameObject的所有子transform,把particleSystem加进pss里
    public static void RecusionParticleSystemObject(GameObject gameObject, ref List<ParticleSystem> pss)
    {
        //获取子ParticleSystem        
        for (int i = 0; i < gameObject.transform.childCount; i++)
        {
            Transform ts = gameObject.transform.GetChild(i);
            GameObject psObj = ts.gameObject;
            if (psObj)
            {
                //保存ParticleSystem
                ParticleSystem psComp = psObj.GetComponent<ParticleSystem>();
                if (psComp != null)
                {
                    pss.Add(psComp);
                }
            }
        }
    }

















    private static void FindInGO(GameObject g)
    {
        go_count++;
        Component[] components = g.GetComponents<Component>();
        for (int i = 0; i < components.Length; i++)
        {
            components_count++;
            if (components[i] == null)
            {
                missing_count++;
                string s = g.name;
                Transform t = g.transform;
                while (t.parent != null)
                {
                    s = t.parent.name + "/" + s;
                    t = t.parent;
                }
                //这里意思是一般会miss掉得组件都是script
                //第二个参数代表信息提供者
                Debug.Log(s + " has an empty script attached in position: " + i, g);
            }
        }
        // Now recurse through each child GO (if there are any):
        foreach (Transform childT in g.transform)
        {
            //Debug.Log("Searching " + childT.name  + " " );
            FindInGO(childT.gameObject);
        }               
    }

    //检测Material材质在Resource资源中的所有引用关
    //就是检查选中的Material在那些资源里面被引用了,会Log出来.
    //一次只能选择一个Material
    //具体使用方法:
    //Editor内选中一个material,然后点击Tools下的CheckMaterialReference,
    //接下来会卡住一会,因为没写dialog啊,最后会Log出来引用的信息
    [MenuItem("Tools/CheckMaterialReference")]
    public static void CheckMaterialReference()
    {
        Object[] objects = Selection.objects;
        if (objects.Length != 1)
        {
            EditorUtility.DisplayDialog("Error", "you should select one items", "ok");
            return;
        }

        Object obj = objects[0];
        Material mat = obj as Material;
        if (mat == null)
        {
            EditorUtility.DisplayDialog("Error", "you should select material", "ok");
            return;
        }


        string matPath = AssetDatabase.GetAssetPath(mat);
        //Debug.Log(matPath);



        //获取所有资源路径       
        mResources.Clear();

        //Resource资源路径
        string resourcePath = Application.dataPath + "/Resources/";

        string[] files = Directory.GetFiles(resourcePath, "*.*", SearchOption.AllDirectories);
        foreach (string file in files)
        {
            string suffix = BuildCommon.getFileSuffix(file);
            if (suffix == "meta")
                continue;

            //查找预制件件
            if (suffix == "prefab")
            {
                string realFile = file.Replace("\\", "/");
                realFile = realFile.Replace(Application.dataPath, "Assets");
                mResources.Add(realFile);
            }
        }

        //查找所有引用该Material的文件
        foreach (string assetPath in mResources)
        {
            //获取包含的所有依赖
            string[] depencies = AssetDatabase.GetDependencies(new string[] { assetPath });
            foreach (string dep in depencies)
            {
                //如果是材质
                if (dep.EndsWith(".mat") && matPath == dep)
                {

                    Debug.Log(mat.name + "is referenced by" + assetPath);
                }
            }
        }
    }

}
EditorTools 是一款免费采集软件,是中小网站自动更新利器,全自动采集发布,运行期间静默工作,无须人工干预;独立软件免除网站性能消耗;安全稳定,可长年累月不间断工作。 声明:本软件适合需要长期更新内容的非临时性网站使用,不需要你对现有论坛或网站做任何修改。 更新说明: EditorTools 2 功能介绍【特色】绿色软件,免安装 【特色】设定好方案,即可24小时自动工作,不再需要人工干涉 【特色】小巧、低耗和良好的稳定性非常适合运行于服务器 【特色】所有规则都可以导入导出,灵活的资源重用 【特色】采用FTP上传文件,稳定、安全 【特色】与网站分离,通过独立制作的接口,可以支持任何网站或数据库 【采集】可选择倒序、顺序、随机采集文章 【采集】支持自动列表网址 【采集】支持对数据分布在多层页面的网站进行采集 【采集】自由设定采集数据项,并可单独过滤整理每个数据项 【采集】支持分页内容采集 【采集】支持任意格式、类型的文件(包括图片、视频)下载 【采集】可突破防盗链文件 【采集】支持动态文件网址分析 【采集】支持对需登录访问的网页的采集 【支持】可设定关键词采集 【支持】可设定防止采集的敏感词 【支持】可设置图片水印 【发布】支持发布带回复的文章,可广泛用于论坛、博客等项目 【发布】与采集数据分离的发布参数项,可自由对应采集数据或预设数值,极大增强发布规则的重用性 【发布】支持随机选用发布账号 【发布】支持任意发布项目语言翻译、简繁互译 【发布】支持编码转换、支持UBB代码 【发布】文件上传可选择好自动建立年月日子目录 【发布】模拟发布支持对无法安装接口的网站进行发布操作 【支持】方案可定时工作 【支持】防止网络运营商劫持HTTP功能 【支持】可手动进行单项采集发布 【支持】详尽的工作流程监视、信息反馈,让您迅速了解工作状态
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值