一个检测特效是否丢失材质的脚本

using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;

public class TestVfxValid : MonoBehaviour {
    static string project_path = Application.dataPath.Substring(0, Application.dataPath.Length - 6);
    static int go_count = 0, components_count = 0, missing_count = 0;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }

    //[]

    //string[] GetSelectPaths()
    //{
    //    UnityEngine.Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.Unfiltered);
    //    //此处添加需要命名的资源后缀名,注意大小写。
    //    string[] paths = new string[SelectedAsset.Length];

    //    for (int i = 0; i < paths.Length; i++)
    //    {
    //        paths[i] = project_path + AssetDatabase.GetAssetPath(SelectedAsset[i]);
    //    }

    //    return paths;
    //}

    public void OnGUI()
    {
        if (GUILayout.Button("Find Missing Scripts in selected GameObjects"))
        {
            FindInSelected();
        }
        if (GUILayout.Button("Find Missing Mat in selected Particle"))
        {
            FindInSelectedMissMat();
        }
    }

    private static string GetRootStr(GameObject g)
    {
        string s = g.name;
        Transform t = g.transform;
        while (t.parent != null)
        {
            s = t.parent.name;
            t = t.parent;
        }

        return s;
    }

    private static string GetPathStr(GameObject g)
    {
        string s = g.name;
        Transform t = g.transform;
        while (t.parent != null)
        {
            s = t.parent.name + "/" + s;
            t = t.parent;
        }

        return s;
    }

    #region 检查丢材质的特效节点
    private static void FindInSelectedMissMat()
    {
        GameObject[] Go = Selection.gameObjects;
        go_count = 0;
        components_count = 0;
        missing_count = 0;

        //FileStream fs = new FileStream("log/MissMaterialLog.txt", FileMode.OpenOrCreate);
        string path = project_path + "/log/";
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }
        StreamWriter swriter = new StreamWriter(path + "MissMaterialLog.txt",false);

        foreach (GameObject g in Go)
        {
            FindMissMat(g, swriter);
        }

        string str = string.Format("Searched {0} GameObjects, {1} components, found {2} missing", go_count, components_count, missing_count);
        Debug.Log(str);
        //swriter.WriteLine(str);

        swriter.Close();
        swriter = null;
        //fs.Close();
        //fs = null;
    }

    private static void FindMissMat(GameObject go,StreamWriter swriter)
    {
        go_count++;
        ParticleSystem[] pars = go.GetComponentsInChildren<ParticleSystem>();
        for (int i = 0; i < pars.Length; i++)
        {
            components_count++;
            ParticleSystemRenderer rend = pars[i].GetComponent<ParticleSystemRenderer>();
            if (rend != null && rend.material.mainTexture == null)
            {
                missing_count++;
                if(rend.gameObject.name == "play")
                {
                    continue;
                }
                string s = GetPathStr(rend.gameObject);
                //Object.DestroyImmediate(components[i]);
                //s = s + " has an empty script attached in position: " + i;
                Debug.Log(s, rend);
                swriter.WriteLine(s);
            }
        }
    }
    #endregion

    #region 检查丢脚本组件的节点
    private static void FindInSelected()
    {
        GameObject[] Go = Selection.gameObjects;
        go_count = 0;
        components_count = 0;
        missing_count = 0;
        foreach (GameObject g in Go)
        {
            FindInGO(g);
        }
        Debug.Log(string.Format("Searched {0} GameObjects, {1} components, found {2} missing", go_count, components_count, missing_count));
    }

    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 = GetPathStr(g);
                //Object.DestroyImmediate(components[i]);
                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);
        }
    }
    #endregion

}

用法很简单:

  1. 把所有的特效拖到Hierachy视图中
  2. 然后随便找个节点挂上这个脚本
  3. 运行游戏
  4. Game视图会出现两个选项,第一个是检测是否有脚本丢失,第二个就是检测是否有材质丢失

开源是一种精神

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值