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

问题描述 投票:0回答:2

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

我尝试使用 Streamwriter,但似乎没有能力。 我查看了 https://docs.unity3d.com/ScriptReference/PrefabUtility.html 但这不会导出预制件,而只是将它们存储在项目文件中。

有人对我可以用什么来解决这个问题有任何想法或建议吗

c# unity-game-engine
2个回答
4
投票

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();
        }
    }
}

0
投票

这是我编写的脚本,用于将对象列表导出为 FBX 文件。它在运行时和编辑器中使用 Autodesk.Fbx 类工作

https://github.com/chadrickevans/RuntimeFBXExporter.git

© www.soinside.com 2019 - 2024. All rights reserved.