纳金网
标题:
Unity判断材质球丢失或贴图丢失
[打印本页]
作者:
狂风大尉
时间:
2015-6-29 00:23
标题:
Unity判断材质球丢失或贴图丢失
最近主程要我写个工具来判断资源是否有材质球或贴图丢失的,我就简单的写了个一下代码:
using UnityEngine;
using System.Xml;
using System.IO;
using UnityEditor;
using System.Text;
using System.Collections;
using System.Collections.Generic;
public class ScanMaterials :EditorWindow{
[MenuItem("Tools/ScanMaterials")]
public static void Scan()
{
ReadXml();
}
static void ReadXml()
{
string configPath = "Assets/Editor/prefabconfig.xml";
XmlDocument xml = new XmlDocument();
xml.Load(configPath);
XmlNodeList childlist = xml.ChildNodes;
foreach (XmlNode childNode in childlist)
{
string childname = childNode.Name;
if (childname == "root")
{
GetPath(childNode);
}
}
}
static void GetPath(XmlNode node)
{
XmlNodeList childlist = node.ChildNodes;
if (childlist.Count == 0)
{
return;
}
foreach (XmlNode childNode in childlist)
{
string childname = childNode.Name;
if (childname == "loadpath")
{
GetPrefab(childNode);
}
}
}
static void GetPrefab(XmlNode node)
{
if (node.Name == "loadpath" && node.Attributes["path"] != null)
{
string childpath = node.Attributes["path"].Value;
string sourcepath = "Assets/" + childpath;
string[] allfiles = Directory.GetFiles(sourcepath, "*.prefab", SearchOption.AllDirectories);
foreach (string file in allfiles)
{
if (file.Contains(".prefab"))
{
string loadfile = file;
UnityEngine.Object loadObject = AssetDatabase.LoadMainAssetAtPath(loadfile);
if (loadObject == null)
{
Helper.LogError("load asset file error:" + loadfile);
return;
}
GameObject preObj = loadObject as GameObject;
//此处有点<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组件
//有大神知道原因请赐教,感激不尽!!!!!!!
GameObject obj = Instantiate(preObj) as GameObject;
Renderer[] renderers = obj.GetComponentsInChildren<Renderer>();
for (int i = 0; i < renderers.Length; i++)
{
Renderer render = renderers[i];
Judge(render.sharedMaterials, render.gameObject, preObj, loadfile);
}
GameObject.DestroyImmediate(obj);
}
}
}
}
static void Judge(Material[] materials, GameObject obj, GameObject parentObj, string path)
{
for (int y = 0; y < materials.Length; y++)
{
Material material = materials[y];
if (null == material)
{
string str = " Material is none";
StringBuilder sb = new StringBuilder(path);
sb.Append(" ");
sb.Append(parentObj.name);
sb.Append(" "+obj.name);
sb.Append(" ");
sb.Append(str);
Helper.LogError(sb.ToString());
}
else
{
if (material.mainTexture == null)
{
string str = " mainTexture is none";
StringBuilder sb = new StringBuilder(path);
sb.Append(" ");
sb.Append(parentObj.name);
sb.Append(" " + obj.name);
sb.Append(" ");
sb.Append(material.name);
sb.Append(" ");
sb.Append(str);
Helper.LogError(sb.ToString());
}
}
}
}
}
复制代码
欢迎光临 纳金网 (http://go.narkii.com/club/)
Powered by Discuz! X2.5