在运行时使用C#脚本将游戏对象导出为FBX



我正在尝试构建一个文件室编辑器。因此,用户放置家具、旋转家具等。所有对象都保存为Room父对象的子对象。我需要对功能进行编程,现在将房间及其孩子保存为FBX或OBJ,这样就可以通过任何3D查看软件发送和查看,甚至可以在搅拌机之类的东西中打开它(纹理还不是问题(

我试着考虑使用Streamwriter,但这似乎无法实现。我看过https://docs.unity3d.com/ScriptReference/PrefabUtility.html但这不会导出预制件,而是将它们存储在项目文件中。

有人对我可以用什么来处理这个有任何想法或建议吗

FBX SDK绑定可以在游戏过程中执行,允许在运行时导入和导出。

注意:FBX SDK绑定仅在默认情况下为编辑器,不会包含在构建中。为了将包包含在构建,将FBXSDK_RUNTIME定义添加到编辑>项目设置>播放器>其他设置>脚本定义符号。

using UnityEngine;
using UnityEngine.UI;
using Autodesk.Fbx;
using System.IO;
public class WriteFBXonEvent : MonoBehaviour
{
//Make sure to attach these Buttons in the Inspector
public Button m_HitMeButton;
void Start()
{
Button btn = m_HitMeButton.GetComponent<Button>();
btn.onClick.AddListener(TaskOnClick);
}
void TaskOnClick()
{
// Build the fbx scene file path 
// (player/player_data/emptySceneFromRuntime.fbx)
string fbxFilePath = Application.dataPath;
fbxFilePath = Path.Combine(fbxFilePath, "emptySceneFromRuntime.fbx");
fbxFilePath = Path.GetFullPath(fbxFilePath);
Debug.Log(string.Format("The file that will be written is {0}", fbxFilePath));
using (var fbxManager = FbxManager.Create())
{
FbxIOSettings fbxIOSettings = FbxIOSettings.Create(fbxManager, Globals.IOSROOT);
// Configure the IO settings.
fbxManager.SetIOSettings(fbxIOSettings);
// Create the exporter 
var fbxExporter = FbxExporter.Create(fbxManager, "Exporter");
// Initialize the exporter.
int fileFormat = fbxManager.GetIOPluginRegistry().FindWriterIDByDescription("FBX ascii (*.fbx)");
bool status = fbxExporter.Initialize(fbxFilePath, fileFormat, fbxIOSettings);
// Check that initialization of the fbxExporter was successful
if (!status)
{
Debug.LogError(string.Format("failed to initialize exporter, reason: {0}",
fbxExporter.GetStatus().GetErrorString()));
return;
}
// Create a scene
var fbxScene = FbxScene.Create(fbxManager, "Scene");
// create scene info
FbxDocumentInfo fbxSceneInfo = FbxDocumentInfo.Create(fbxManager, "SceneInfo");
// set some scene info values
fbxSceneInfo.mTitle = "fromRuntime";
fbxSceneInfo.mSubject = "Exported from a Unity runtime";
fbxSceneInfo.mAuthor = "Unity Technologies";
fbxSceneInfo.mRevision = "1.0";
fbxSceneInfo.mKeywords = "export runtime";
fbxSceneInfo.mComment = "This is to demonstrate the capability of exporting from a Unity runtime, using the FBX SDK C# bindings";
fbxScene.SetSceneInfo(fbxSceneInfo);
// Export the scene to the file.
status = fbxExporter.Export(fbxScene);
// cleanup
fbxScene.Destroy();
fbxExporter.Destroy();
}
}
}

最新更新