本文章由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?

title=ObjExporter

三、初步成果

找到了一个站点: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这个组件,所以总会导出少一个,而这个竟然是人的主体。

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2FydHpoYW5n/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="这里写图片描写叙述" title="">

图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



步骤三

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2FydHpoYW5n/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="这里写图片描写叙述" title="">

图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】

title=ObjExporter">http://wiki.unity3d.com/index.php?

title=ObjExporter

【6】https://github.com/KellanHiggins/UnityFBXExporter/tree/master/Assets/Packages/UnityFBXExporter

七。最后但不是不重要

Asset导出成FBX的格式:https://github.com/cartzhang/UnityFBXExporter



与上面介绍的不是一个方法。可是思路都一样。这个源代码能够把纹理和材质都匹配上去,当然我也做了略微的改动。修复了之前的小bug。

非常感谢,欢迎留言!!

Unity3D Asset文件导出3DMax 可编辑格式的更多相关文章

  1. Unity3D Asset 导入&导出

    [Unity3D Asset 导入&导出] 通过Assets->Export Package..菜单可以导出当前选中Assets.若没有选中Assets,则会导出全部assets. 在弹 ...

  2. 将指定路径下的所有SVG文件导出成PNG等格式的图片(缩略图或原图大小)

    原文:将指定路径下的所有SVG文件导出成PNG等格式的图片(缩略图或原图大小) WPF的XAML文档(Main.xaml): <Window x:Class="SVG2Image.Ma ...

  3. Unity3D asset bundle 格式简析

    http://blog.codingnow.com/2014/08/unity3d_asset_bundle.html Unity3D 的 asset bundle 的格式并没有公开.但为了做更好的差 ...

  4. asp.net导出excel-一行代码实现excel、xml、pdf、word、html、csv等7种格式文件导出功能而且美观-SNF快速开发平台

    分享: 腾讯微博  新浪微博   搜狐微博   网易微博  腾讯朋友  百度贴吧  豆瓣   QQ好友  人人网 作者:王春天  原文地址:http://www.cnblogs.com/spring_ ...

  5. unity3d 资源文件从MAX或者MAYA中导出的注意事项

    unity3d 资源文件从MAX或者MAYA中导出的注意事项     1.首先,Unity3d 中,导出带动画的资源有2种导出方式可以选择:    1) 导出资源时,只导出一个文件,保留模型,骨骼和所 ...

  6. 【转】unity3d 资源文件从MAX或者MAYA中导出的注意事项

    转自游戏开发主席   1.首先,Unity3d 中,导出带动画的资源有2种导出方式可以选择:    1) 导出资源时,只导出一个文件,保留模型,骨骼和所有的动作帧(把所有的动作,比如idle,atta ...

  7. 将ACCESS 的数据库中的表的文件 导出了EXCEL格式

    将ACCESS 的数据库中的表的文件 导出了EXCEL格式 '''' '将ACCESS数据库中的某个表的信息 导出为EXCEL 文件格式 'srcfName ACCESS 数据库文件路径 'desfN ...

  8. C#进行Visio二次开发之文件导出及另存Web页面

    在我前面很多关于Visio的开发过程中,介绍了各种Visio的C#开发应用场景,包括对Visio的文档.模具文档.形状.属性数据.各种事件等相关的基础处理,以及Visio本身的整体项目应用,虽然时间过 ...

  9. C#中的文件导出大全

    s 得到 radiobuttonlist和CheckBoxList 选中值 得到radiobuttonlist 选中值:var CheckBoxList=document.all.optButtonL ...

随机推荐

  1. NodeJs学习记录(五)初学阶段关于ejs和路由

    1.因为只是用了一点皮毛,所以使用起来感觉基本和jsp无异, 逻辑代码块使用  <%  if() {} else  %> , 输出参数值使用 <%=title  %>, 有一个 ...

  2. [ TJOI 2010 ] 打扫房间

    \(\\\) Description 给出一个\(N\times M\) 的网格,一些格子是污点,求是否能用多个封闭的环路覆盖所有不是污点的格点. 封闭的环路覆盖的含义是,每条路径都必须是一个环,且每 ...

  3. (转) 淘淘商城系列——Redis的安装

    http://blog.csdn.net/yerenyuan_pku/article/details/72849612 通过上文的学习,我相信大家已经将首页的轮播图展示出来了,接下来我们将进入一个新的 ...

  4. adb 命令实用

    1.adb安装:adbinstall.bat:原理:将apk文件拖进此bat,install命令会强制(覆盖)安装apk安装包.代码如下: @echo on adb install -r % paus ...

  5. angular4打包以后,刷新报404

    项目打包以后,上传到服务器,可以正常的切换页面,但是一旦刷新就会报404,找不到页面,其解决方法是:在app.module.ts里面引入下面的模块: import {HashLocationStrat ...

  6. C/C++野指针

    野指针: 野指针不同于空指针,空指针是指一个指针的值为null,而野指针的值并不为null,野指针会指向一段实际的内存,只是它指向哪里我们并不知情,或者是它所指向的内存空间已经被释放,所以在实际使用的 ...

  7. 17Aspectij

    17Aspectij-2018/07/31 1.Aspectj基于xml 前置通知 method : 通知,及方法名 pointcut :切入点表达式,此表达式只能当前通知使用. pointcut-r ...

  8. UVA-1599 Ideal Path(双向BFS)

    题目: 给一个n个点m条边(2≤m≤100000, 1≤m≤200000)的无向图,每条边上都涂有一种颜色(用1到1000000000表示).求从结点1到结点n的一条路径, 使得经过的边数尽量少,在此 ...

  9. [luogu3067 USACO12OPEN] 平衡的奶牛群

    传送门 Solution 折半搜索模板题 考虑枚举每个点在左集合和右集合或者不在集合中,然后排序合并即可 Code //By Menteur_Hxy #include <cmath> #i ...

  10. 数据结构---链表ADT C++实现

    最近在学习数据结构,刚开始一直在看书,但是总是感觉似懂非懂,心想还不如自己操练一波,势必有所收获.现将实现代码发表此地,以备日后复习,若有错误或者建议,欢迎告知本人! 1. 节点类 class Nod ...