Unity3d脚本改变GameObject的Material(二)

对所有 perfab进行扫描,并替换 Particle中的默认材质为自定义材质(主要用来依赖打包做准备)

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

public class ReplaceMaterial
{

    static void SetObjRecursively(ref bool bFind ,GameObject rootObj,Material mat)
    {
        if (null == rootObj)
        {
            return;
        }
        if (null != rootObj.particleSystem)
        {
            if (null != rootObj.particleSystem.renderer)
            {
                if (null !=  rootObj.particleSystem.renderer.sharedMaterial
                    &&rootObj.particleSystem.renderer.sharedMaterial.name.Contains("Default-Particle"))
                {
                    Debug.Log(rootObj.name);
                    rootObj.particleSystem.renderer.material = mat;
                    bFind = true;
                }
            }
        }
        System.Collections.Generic.IEnumerable<GameObject> subObj = rootObj.GetDirectChildren();
        if (null != subObj)
        {
            System.Collections.Generic.IEnumerator<GameObject> e = subObj.GetEnumerator();
            while (e.MoveNext())
            {
                SetObjRecursively(ref bFind, e.Current, mat);
            }
        }

    }

    static void ScanAllPrefabUseDefaultParticle()
    {

        List<string> perfabPathLs = new List<string>();
        Debug.Log(Application.dataPath);
        GetPerfabRecursively(Application.dataPath, "*.prefab", ref perfabPathLs);

        int count = 0;
        int totalcount = perfabPathLs.Count;
        UnityEngine.Object matObj = AssetDatabase.LoadMainAssetAtPath("Assets/sfx/default_fx.mat");
        Material mat = matObj as Material;

        int replaceCount = 0;
        foreach (var onePath in perfabPathLs)
        {
            UnityEngine.Object obj = AssetDatabase.LoadMainAssetAtPath(onePath);
            UnityEngine.GameObject gObj = obj as UnityEngine.GameObject;
            bool bFind = false;

            SetObjRecursively(ref bFind, gObj, mat);
            if (bFind)
            {
                Debug.Log(onePath);
                replaceCount++;
                AssetDatabase.SaveAssets();//可以在保存的时候检查到资源本身的错误
            }
            if (0 == (++count)%10)
            {
                EditorUtility.DisplayCancelableProgressBar("Scan perfab", "wait....", count * 1f / totalcount);
            }
        }
        EditorUtility.ClearProgressBar();
        //AssetDatabase.SaveAssets();
        Debug.Log("Replace TotalCount:" + replaceCount);
    }

    static void GetPerfabRecursively(string srcFolder, string searchPattern, ref List<string> perfabLs)
    {
        //string searchPattern = "*.perfab";
        string searchFolder = srcFolder.Replace(@"\", @"/");
        string searchFolderTemp = searchFolder.ToLower();
        if (Directory.Exists(searchFolderTemp))
        {
            string[] files = Directory.GetFiles(searchFolderTemp, searchPattern);
            foreach (var onefile in files)
            {
                string srcFile = onefile.Replace(@"\", @"/");
                string lowerFile =StanderPath( srcFile.ToLower());
                perfabLs.Add(lowerFile);
            }
            string[] dirs = Directory.GetDirectories(searchFolderTemp);
            foreach (string oneDir in dirs)
            {
                GetPerfabRecursively(oneDir, searchPattern, ref perfabLs);
            }
        }
    }

    static string StanderPath(string srcPath)
    {
        int index = srcPath.IndexOf("assets/");
        string resultStr = srcPath.Substring(index, srcPath.Length - index);
        return resultStr;
    }

    [MenuItem("EctypeEditor/Scan AllPerfab")]
    static void ScanAllPrefab()
    {
        ScanAllPrefabUseDefaultParticle();
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值