Unity3d脚本改变GameObject的Material(二)
2016-06-16 10:08
393 查看
对所有 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(); } }
相关文章推荐
- Kinect结合Unity3D引擎开发体感游戏(一)
- Unity3D中脚本的执行顺序和编译顺序
- Unity3D动态对象优化代码分享
- Unity3D获取当前键盘按键及Unity3D鼠标、键盘的基本操作
- Unity3d获取系统时间
- unity3d发布apk在android虚拟机中运行的详细步骤(unity3d导出android apk)
- Unity3D游戏引擎实现在Android中打开WebView的实例
- unity3d调用手机或电脑摄像头
- Unity3d发布IOS9应用时出现中文乱码的解决方法
- 分享一个开源的网络游戏服务器架构—HouHai
- Unity3D插件详细评测及教学下载
- Unity3D上路_01-2D太空射击游戏
- Unity3D上路_02-第一视角射击游戏
- Unity3D上路_03-塔防游戏
- Unity3D上路_04-基础资源介绍
- Unity3D上路_05-网络相关