运行时可导出
可导出多个材质
导出Mesh_0到Assets/目录下
#if UNITY_EDITOR
using UnityEditor;
[CustomEditor(typeof(ComperhenceMeshMonoViewer))]
public class ComperhenceMeshMonoViewerEditor : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
if (GUILayout.Button("导出 Mesh & Mat"))
{
string folderPath = EditorUtility.SaveFolderPanel("", "选择目录", "");
if (string.IsNullOrEmpty(folderPath)) {
return;
}
ComperhenceMeshMonoViewer _target = target as ComperhenceMeshMonoViewer;
//Material mat = _target.GetComponent<Renderer>().sharedMaterial;
List<string> lstNameIdentity = new List<string>();
Material[] mats = _target.GetComponent<Renderer>().sharedMaterials;
int cursor = 0;
foreach (var mat in mats)
{
var texs = GetCertainMaterialTextures(mat);
string go_name = _target.gameObject.name;
foreach (var tex in texs)
{
if (tex == null) continue;
//string saveRelativePath = tex;
//string frmPath = Application.dataPath + "/../" + saveRelativePath;
//GameUtility.SafeCopyFile(frmPath, folderPath + "/" + saveRelativePath);
Texture2D tt = TextureUtils.TextureToTexture2D(tex);
string tex_name = tex.name;
if (lstNameIdentity.Contains(tex_name) == false)
{
lstNameIdentity.Add(tex_name);
}
else
{
tex_name = tex.name + "_" + cursor;
}
GameUtility.SaveTexture(string.Format("{0}/{1}/{2}.png", folderPath, go_name, tex.name), tt);
cursor++;
}
GameUtility.SaveConfig(string.Format("{0}/{1}/{2}", folderPath, go_name, mat.shader.name), "");
}
}
}
public Texture[] GetCertainMaterialTextures(Material _mat)
{
List<Texture> results = new List<Texture>();
Shader shader = _mat.shader;
for (int i = 0; i < ShaderUtil.GetPropertyCount(shader); ++i)
{
if (ShaderUtil.GetPropertyType(shader, i) == ShaderUtil.ShaderPropertyType.TexEnv)
{
string propertyName = ShaderUtil.GetPropertyName(shader, i);
Texture tex = _mat.GetTexture(propertyName);
//string texPath = AssetDatabase.GetAssetPath(tex.GetInstanceID());
//results.Add(texPath);
results.Add(tex);
}
}
return results.ToArray();
}
}
#endif
(还缺一个 GameUtility 类,保存文件用,之后再补上吧)