本文章由cartzhang編寫,轉載請注明出處。 全部權利保留。
文章鏈接:http://blog.csdn.net/cartzhang/article/details/60878354
作者:cartzhang
一、前言
美術想要一個把unity中*.asset的模型導出來,導成3D Max能夠打開的模式。fbx或obj.
須要導出的格式:
圖1
也就是須要一個工具,個人認為這個問題。肯定之前Unity的前輩就有解決方法了。於是乎網上一通下載和測試。
二、解包工具集合
網絡上找來了各種測試。可是沒有一個適合我的。非常多都是失敗。打不開。
參考宣雨松的博客。找了還是沒有結果。
圖3
解包工具有非常多種類,
disunity github地址: https://github.com/ata4/disunity
還有就是AssetAssetsExport,還有Unity Studio.
別人的博客里面都有比較多的介紹和說明。這里就具體說了。
最后還網上wiki里,找到了一個合適的我自己的解包。
http://wiki.unity3d.com/index.php?
三、初步成果
找到了一個站點:http://wiki.unity3d.com/index.php?
title=ObjExporter
能夠導出部分對象。
例如以下圖:
圖0
而原來unity中模型是這個樣子的。
圖4
導出的僅僅有武器和頭盔,沒有人物主體body.
四、bug改動
事實上也不能算bug,或許人家沒有這種須要呢。
Component[] meshfilter = selection[i].GetComponentsInChildren<MeshFilter>();
MeshFilter[] mf = new MeshFilter[meshfilter.Length];
int m = 0;
for (; m < meshfilter.Length; m++)
{
exportedObjects++;
mf[m] = (MeshFilter)meshfilter[m];
}
代碼中是要查找全部組件中的MeshFilter,發現SkinnedMeshRender組件竟然沒有這個MeshFilter這個組件,所以總會導出少一個,而這個竟然是人的主體。
圖5
本來說讓美術自己加入一個MeshFilter組件,然后依據mesh render中的mesh自己來加入一個相應的mesh.
既然是程序。那就想辦法。思路非常明顯,既然是有meshrender,就從這入手唄。
代碼還是不難度。
// 沒有meshFilter。加入一個meshFilter.
SkinnedMeshRenderer[] meshfilterRender = selection[i].GetComponentsInChildren<SkinnedMeshRenderer>();
for (int j = 0; j < meshfilterRender.Length; j++)
{
if (meshfilterRender[j].GetComponent<MeshFilter>() == null)
{
meshfilterRender[j].gameObject.AddComponent<MeshFilter>();
meshfilterRender[j].GetComponent<MeshFilter>().sharedMesh = Instantiate(meshfilterRender[j].sharedMesh);
}
}
這樣改動過,就會自己主動在沒有MeshFilter,可是有skinnedMeshRender組件的節點下,加入一個MeshFilter,然后就能夠正常導出成.obj文件,與.FBX是相似的。都能夠被3D max編輯使用。
圖7
最后的在VS中看的模型,由於沒有安裝3Dmax.
圖6
盡管看起來簡陋,可是滿足他們小須要,就好了。
貼出基本的代碼:
/* Based on ObjExporter.cs, this "wrapper" lets you export to .OBJ directly from the editor menu. This should be put in your "Editor"-folder. Use by selecting the objects you want to export, and select the appropriate menu item from "Custom->Export". Exported models are put in a folder called "ExportedObj" in the root of your Unity-project. Textures should also be copied and placed in the same folder. N.B. there may be a bug so if the custom option doesn't come up refer to this thread http://answers.unity3d.com/questions/317951/how-to-use-editorobjexporter-obj-saving-script-fro.html Updated for Unity 5.3 2017-03-07 @cartzhang fixed can not create obj file in folder. */
using UnityEngine;
using UnityEditor;
using UnityEditor.SceneManagement;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System;
struct ObjMaterial
{
public string name;
public string textureName;
}
public class EditorObjExporter : ScriptableObject
{
private static int vertexOffset = 0;
private static int normalOffset = 0;
private static int uvOffset = 0;
//User should probably be able to change this. It is currently left as an excercise for
//the reader.
private static string targetFolder = "ExportedObj";
private static string MeshToString(MeshFilter mf, Dictionary<string, ObjMaterial> materialList)
{
Debug.Assert(null != mf);
Mesh m = mf.sharedMesh;
Material[] mats = mf.GetComponent<Renderer>().sharedMaterials;
StringBuilder sb = new StringBuilder();
if (null == m)
return sb.ToString();
sb.Append("g ").Append(mf.name).Append("\n");
foreach (Vector3 lv in m.vertices)
{
Vector3 wv = mf.transform.TransformPoint(lv);
//This is sort of ugly - inverting x-component since we're in
//a different coordinate system than "everyone" is "used to".
sb.Append(string.Format("v {0} {1} {2}\n", -wv.x, wv.y, wv.z));
}
sb.Append("\n");
foreach (Vector3 lv in m.normals)
{
Vector3 wv = mf.transform.TransformDirection(lv);
sb.Append(string.Format("vn {0} {1} {2}\n", -wv.x, wv.y, wv.z));
}
sb.Append("\n");
foreach (Vector3 v in m.uv)
{
sb.Append(string.Format("vt {0} {1}\n", v.x, v.y));
}
for (int material = 0; material < m.subMeshCount; material++)
{
sb.Append("\n");
sb.Append("usemtl ").Append(mats[material].name).Append("\n");
sb.Append("usemap ").Append(mats[material].name).Append("\n");
//See if this material is already in the materiallist.
try
{
ObjMaterial objMaterial = new ObjMaterial();
objMaterial.name = mats[material].name;
if (mats[material].mainTexture)
objMaterial.textureName = AssetDatabase.GetAssetPath(mats[material].mainTexture);
else
objMaterial.textureName = null;
materialList.Add(objMaterial.name, objMaterial);
}
catch (ArgumentException)
{
//Already in the dictionary
}
int[] triangles = m.GetTriangles(material);
for (int i = 0; i < triangles.Length; i += 3)
{
//Because we inverted the x-component, we also needed to alter the triangle winding.
sb.Append(string.Format("f {1}/{1}/{1} {0}/{0}/{0} {2}/{2}/{2}\n",
triangles[i] + 1 + vertexOffset, triangles[i + 1] + 1 + normalOffset, triangles[i + 2] + 1 + uvOffset));
}
}
vertexOffset += m.vertices.Length;
normalOffset += m.normals.Length;
uvOffset += m.uv.Length;
return sb.ToString();
}
private static void Clear()
{
vertexOffset = 0;
normalOffset = 0;
uvOffset = 0;
}
private static Dictionary<string, ObjMaterial> PrepareFileWrite()
{
Clear();
return new Dictionary<string, ObjMaterial>();
}
private static void MaterialsToFile(Dictionary<string, ObjMaterial> materialList, string folder, string filename)
{
using (StreamWriter sw = new StreamWriter(folder + Path.DirectorySeparatorChar + filename + ".mtl"))
{
foreach (KeyValuePair<string, ObjMaterial> kvp in materialList)
{
sw.Write("\n");
sw.Write("newmtl {0}\n", kvp.Key);
sw.Write("Ka 0.6 0.6 0.6\n");
sw.Write("Kd 0.6 0.6 0.6\n");
sw.Write("Ks 0.9 0.9 0.9\n");
sw.Write("d 1.0\n");
sw.Write("Ns 0.0\n");
sw.Write("illum 2\n");
if (kvp.Value.textureName != null)
{
string destinationFile = kvp.Value.textureName;
int stripIndex = destinationFile.LastIndexOf(Path.DirectorySeparatorChar);
if (stripIndex >= 0)
destinationFile = destinationFile.Substring(stripIndex + 1).Trim();
string relativeFile = destinationFile;
destinationFile = folder + Path.DirectorySeparatorChar + destinationFile;
Debug.Log("Copying texture from " + kvp.Value.textureName + " to " + destinationFile);
try
{
//Copy the source file
File.Copy(kvp.Value.textureName, destinationFile);
}
catch
{
}
sw.Write("map_Kd {0}", relativeFile);
}
sw.Write("\n\n\n");
}
}
}
private static void MeshToFile(MeshFilter mf, string folder, string filename)
{
Dictionary<string, ObjMaterial> materialList = PrepareFileWrite();
using (StreamWriter sw = new StreamWriter(folder + Path.DirectorySeparatorChar + filename + ".obj"))
{
sw.Write("mtllib ./" + filename + ".mtl\n");
sw.Write(MeshToString(mf, materialList));
}
MaterialsToFile(materialList, folder, filename);
}
private static void MeshesToFile(MeshFilter[] mf, string folder, string filename)
{
Dictionary<string, ObjMaterial> materialList = PrepareFileWrite();
using (StreamWriter sw = new StreamWriter(folder + Path.DirectorySeparatorChar + filename + ".obj"))
{
sw.Write("mtllib ./" + filename + ".mtl\n");
for (int i = 0; i < mf.Length; i++)
{
sw.Write(MeshToString(mf[i], materialList));
}
}
MaterialsToFile(materialList, folder, filename);
}
private static bool CreateTargetFolder()
{
try
{
System.IO.Directory.CreateDirectory(targetFolder);
}
catch
{
EditorUtility.DisplayDialog("Error!", "Failed to create target folder!", "");
return false;
}
return true;
}
[MenuItem("Custom/Export/Export whole selection to single OBJ")]
static void ExportWholeSelectionToSingle()
{
if (!CreateTargetFolder())
return;
Transform[] selection = Selection.GetTransforms(SelectionMode.Editable | SelectionMode.ExcludePrefab);
if (selection.Length == 0)
{
EditorUtility.DisplayDialog("No source object selected!", "Please select one or more target objects", "");
return;
}
int exportedObjects = 0;
ArrayList mfList = new ArrayList();
for (int i = 0; i < selection.Length; i++)
{
Component[] meshfilter = selection[i].GetComponentsInChildren(typeof(MeshFilter));
for (int m = 0; m < meshfilter.Length; m++)
{
exportedObjects++;
mfList.Add(meshfilter[m]);
}
}
if (exportedObjects > 0)
{
MeshFilter[] mf = new MeshFilter[mfList.Count];
for (int i = 0; i < mfList.Count; i++)
{
mf[i] = (MeshFilter)mfList[i];
}
string filename = EditorSceneManager.GetActiveScene().name + "_" + exportedObjects;
int stripIndex = filename.LastIndexOf(Path.DirectorySeparatorChar);
if (stripIndex >= 0)
filename = filename.Substring(stripIndex + 1).Trim();
MeshesToFile(mf, targetFolder, filename);
EditorUtility.DisplayDialog("Objects exported", "Exported " + exportedObjects + " objects to " + filename, "");
}
else
EditorUtility.DisplayDialog("Objects not exported", "Make sure at least some of your selected objects have mesh filters!", "");
}
[MenuItem("Custom/Export/Export each selected to single OBJ")]
static void ExportEachSelectionToSingle()
{
if (!CreateTargetFolder())
return;
Transform[] selection = Selection.GetTransforms(SelectionMode.Editable | SelectionMode.ExcludePrefab);
if (selection.Length == 0)
{
EditorUtility.DisplayDialog("No source object selected!", "Please select one or more target objects", "");
return;
}
int exportedObjects = 0;
for (int i = 0; i < selection.Length; i++)
{
// 沒有meshFilter,加入一個meshFilter.
SkinnedMeshRenderer[] meshfilterRender = selection[i].GetComponentsInChildren<SkinnedMeshRenderer>();
for (int j = 0; j < meshfilterRender.Length; j++)
{
if (meshfilterRender[j].GetComponent<MeshFilter>() == null)
{
meshfilterRender[j].gameObject.AddComponent<MeshFilter>();
meshfilterRender[j].GetComponent<MeshFilter>().sharedMesh = Instantiate(meshfilterRender[j].sharedMesh);
}
}
Component[] meshfilter = selection[i].GetComponentsInChildren<MeshFilter>();
MeshFilter[] mf = new MeshFilter[meshfilter.Length];
int m = 0;
for (; m < meshfilter.Length; m++)
{
exportedObjects++;
mf[m] = (MeshFilter)meshfilter[m];
}
MeshesToFile(mf, targetFolder, selection[i].name + "_" + i);
}
if (exportedObjects > 0)
{
EditorUtility.DisplayDialog("Objects exported", "Exported " + exportedObjects + " objects", "");
}
else
EditorUtility.DisplayDialog("Objects not exported", "Make sure at least some of your selected objects have mesh filters!", "");
}
}
能夠直接復制到直接的項目中使用。
五、怎么使用呢?
首先把代碼復制到項目中,直接下載project也行。
步驟一
圖8
步驟二
圖10
步驟三
圖11
就能夠使用你的模型編輯工具來查看了。
五、源代碼和演示樣例project
源代碼地址:
https://github.com/cartzhang/unity_lab/blob/master/ExportFbx/UnityAssetExportFBX/Assets/Editor/OBJExport/EditorObjExporter.cs
演示樣例project地址:
https://github.com/cartzhang/unity_lab/tree/master/ExportFbx/UnityAssetExportFBX
博客圖片地址:
https://github.com/cartzhang/unity_lab/tree/master/ExportFbx/Img
Github readme:
https://github.com/cartzhang/unity_lab/blob/master/ExportFbx/Unity%20asset%E6%96%87%E4%BB%B6%20%E5%AF%BC%E5%87%BAOBJ.md
六、參考
【1】http://www.xuanyusong.com/archives/3618
【2】https://forums.inxile-entertainment.com/viewtopic.php?t=13724
【3】http://www.cnblogs.com/Niger123/p/4261763.html
【4】http://prog3.com/sbdm/download/download/akof1314/9097153
【5】http://wiki.unity3d.com/index.php?
【6】https://github.com/KellanHiggins/UnityFBXExporter/tree/master/Assets/Packages/UnityFBXExporter
七。最后但不是不重要
Asset導出成FBX的格式:https://github.com/cartzhang/UnityFBXExporter
與上面介紹的不是一個方法。可是思路都一樣。這個源代碼能夠把紋理和材質都匹配上去,當然我也做了略微的改動。修復了之前的小bug。
非常感謝,歡迎留言!!