Solution to export FBX model when Unity is running

foreword

This article is about how to export game objects in the scene to FBX format files at runtime. The solution supports exporting the hierarchical structure of game objects under the Hierarchy, Transform component information, material maps, static Mesh grids, SkinnedMesh, bone information, and animations. It also supports exporting camera components and light components in the scene to FBX models.

Currently, this solution only supports the export of Legacy animations, and does not support the export of Generic and Humanoid animations. Because some APIs used in it can only be used under Legacy animation, so only Legacy animation can be exported. But all animation clips under the current game object can be exported.

1. Add FBX SDK runtime support

You can add the Autodesk FBX SDK for Unity to your Unity project in the following ways:

Click the "Windoes" option in the menu bar and select "Package Manager";

Click "+" in the upper left corner of the Package Manager panel, and then select Add package from git url;

Enter "[email protected]" in the pop-up input box, and then click "Add" to add the package;

After adding the Autodesk FBX SDK for Unity, the package only supports editor mode. If you want to be supported at runtime, you need to do the following:

Click the "Edit" option in the menu bar, then select "Project Settings", and select "Player" in the pop-up window;

Then in the "Other Settings" tab, find the "Script Compilation" option;

Enter "FBXSDK_RUNTIME" in the "Scripting Define Symbols" box, and then click the "Apply" button;

 Note: This package can only be supported by the three platforms of Windows/OSX/Linux when it is running, and other platforms cannot support it for the time being;

2. Import Exporter FBX Model

Import the unitypackage package: Exporter FBX Model

The download address of the package: click me

2.1 Introduction

After importing the package body, open the default sample scene: SampleScene

The whole project has only three scripts:

1. Editor script: ClipInfo: This script is an editor script, mainly used to serialize the existing animation clip data to the local, and then read it at runtime. This script will only be used once before being packaged into an EXE to ensure that there are files that can be read at runtime in the StreamingAssets file directory.

2. Runtime script: DeserializeAnimationInfo: The main function of this script is to deserialize the animation data in the StreamingAssets file directory at runtime.

The above two scripts can be completely discarded if there is no need to export animation;

3. Runtime script: Exporter FBX: This script is used to export the game objects in the scene to FBX files. The default export path is the StreamingAssets folder (the path can be modified in the code).

2.2 Operation

To export the FBX model at runtime, the most critical script is the ExporterFBX script, which can be mounted to any model in the scene. Drag the game object to be exported to the ExportModels field of the script.

The entry method of the script is ExportAll, which has three parameters:

List<GameObject> unityGos: list of Unity objects to be exported;

bool animationOnly: Whether to export only animations (do not export material maps and grid information);

bool notAnimation: whether to export animation (if true means not exporting animation, only exporting material texture and grid information);

If you do not need to export animation information, you only need to set the notAnimation parameter to true;

If you need to export animation information, you need to do the following:

1. In the editor mode, serialize the clip information of the animation to the local:

 Select Window->ClipInfo in the menu bar, and then a dialog box will pop up;

Drag the animation clip under the Project view to the Clip selection box under the ClipInfo panel, or directly select it behind the selection box; 

After specifying the animation clip, the key frame information of the animation will be displayed below, click the Save button at this time; the animation information can be serialized to the StreamingAssets folder. At this point, the editor script is no longer used (if you need to serialize other animations in the future, you still need to use it).

Then drag the script DeserializeAnimationInfo to the game object that needs to be exported . Then the scene can be packaged into exe for testing.

After the packaging is complete, run the packaged exe and press the "A" key on the keyboard to export the specified model to the FBX file. The "S" key modifies the animation at runtime.

3. Write on the back

This is just an example scene that I made, and what I did is definitely not perfect. Many places in the script can be optimized, and you can modify it according to your own needs.

The main script is the ExporterFBX script. I also wrote comments in this script. You can take a look and modify it;

For example, in the ExportAll method of the script, you can modify the export and save path of the FBX file; and whether you need to export animation information, you can also modify it. Hope this article can help you.

Guess you like

Origin blog.csdn.net/m0_68256659/article/details/124583496