一個檢測特效是否丟失材質的腳本

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視圖會出現兩個選項,第一個是檢測是否有腳本丟失,第二個就是檢測是否有材質丟失

開源是一種精神

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章