查看: 1252|回复: 0
打印 上一主题 下一主题

[其他] Unity判断材质球丢失或贴图丢失

[复制链接]

2722

主题

42

听众

3万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
38268
精华
111

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2015-6-29 00:23:53 |只看该作者 |倒序浏览

最近主程要我写个工具来判断资源是否有材质球或贴图丢失的,我就简单的写了个一下代码:
  1. using UnityEngine;
  2. using System.Xml;
  3. using System.IO;
  4. using UnityEditor;
  5. using System.Text;
  6. using System.Collections;
  7. using System.Collections.Generic;

  8. public class ScanMaterials :EditorWindow{
  9.     [MenuItem("Tools/ScanMaterials")]
  10.         public static void Scan()
  11.     {
  12.         ReadXml();
  13.     }

  14.     static void ReadXml()
  15.     {
  16.         string configPath = "Assets/Editor/prefabconfig.xml";


  17.         XmlDocument xml = new XmlDocument();
  18.         xml.Load(configPath);
  19.         XmlNodeList childlist = xml.ChildNodes;
  20.         foreach (XmlNode childNode in childlist)
  21.         {
  22.             string childname = childNode.Name;

  23.             if (childname == "root")
  24.             {
  25.                 GetPath(childNode);
  26.             }
  27.         }
  28.     }

  29.     static void GetPath(XmlNode node)
  30.     {
  31.         XmlNodeList childlist = node.ChildNodes;
  32.         if (childlist.Count == 0)
  33.         {
  34.             return;
  35.         }
  36.         foreach (XmlNode childNode in childlist)
  37.         {
  38.             string childname = childNode.Name;

  39.             if (childname == "loadpath")
  40.             {
  41.                 GetPrefab(childNode);
  42.             }
  43.         }
  44.     }

  45.     static void GetPrefab(XmlNode node)
  46.     {
  47.         if (node.Name == "loadpath" && node.Attributes["path"] != null)
  48.         {
  49.             string childpath = node.Attributes["path"].Value;
  50.             
  51.             string sourcepath = "Assets/" + childpath;
  52.             string[] allfiles = Directory.GetFiles(sourcepath, "*.prefab", SearchOption.AllDirectories);
  53.             foreach (string file in allfiles)
  54.             {
  55.                 if (file.Contains(".prefab"))
  56.                 {
  57.                     string loadfile = file;

  58.                     UnityEngine.Object loadObject = AssetDatabase.LoadMainAssetAtPath(loadfile);
  59.                     if (loadObject == null)
  60.                     {
  61.                         Helper.LogError("load asset file error:" + loadfile);
  62.                         return;
  63.                     }
  64.                     GameObject preObj = loadObject as GameObject;
  65.                     //此处有点<a  target="_blank" class="relatedlink" style="-webkit-transition: all 0.3s ease 0s; transition: all 0.3s ease 0s; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: blue;">问题</a>:就是我不实例化物体对象就获取不到子物体的Renderer组件
  66.                     //有大神知道原因请赐教,感激不尽!!!!!!!
  67.                     GameObject obj = Instantiate(preObj) as GameObject;
  68.                     Renderer[] renderers = obj.GetComponentsInChildren<Renderer>();
  69.                     for (int i = 0; i < renderers.Length; i++)
  70.                     {
  71.                         Renderer render = renderers[i];
  72.                         Judge(render.sharedMaterials, render.gameObject, preObj, loadfile);
  73.                     }
  74.                     GameObject.DestroyImmediate(obj);
  75.                 }
  76.                
  77.             }
  78.         }
  79.     }

  80.     static void Judge(Material[] materials, GameObject obj, GameObject parentObj, string path)
  81.     {
  82.         for (int y = 0; y < materials.Length; y++)
  83.         {
  84.             Material material = materials[y];
  85.             if (null == material)
  86.             {
  87.                 string str = " Material is none";
  88.                 StringBuilder sb = new StringBuilder(path);
  89.                 sb.Append("  ");
  90.                 sb.Append(parentObj.name);
  91.                 sb.Append("  "+obj.name);
  92.                 sb.Append("   ");
  93.                 sb.Append(str);
  94.                 Helper.LogError(sb.ToString());
  95.             }
  96.             else
  97.             {
  98.                 if (material.mainTexture == null)
  99.                 {
  100.                     string str = " mainTexture is none";
  101.                     StringBuilder sb = new StringBuilder(path);
  102.                     sb.Append("  ");
  103.                     sb.Append(parentObj.name);
  104.                     sb.Append("  " + obj.name);
  105.                     sb.Append("   ");
  106.                     sb.Append(material.name);
  107.                     sb.Append("   ");
  108.                     sb.Append(str);
  109.                     Helper.LogError(sb.ToString());
  110.                 }
  111.             }
  112.         }
  113.     }
  114. }
复制代码
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2025-7-31 13:13 , Processed in 0.171168 second(s), 29 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部