How to import user defined attributes from FBX files?

Here I've added string attribute named StringData and vector attribute named VectorData.

这里我已经在物体添加了 vector 和 VectorData 属性.

And the attached example project shows you how to write import time post processor script.
以下有例子供参考学习,如何用脚本实现读取信息。

Editor/CustomImportProcessor.cs:

using UnityEngine;
using UnityEditor;
using System.IO;
 
class CustomImportProcessor : AssetPostprocessor {
    void OnPostprocessGameObjectWithUserProperties(GameObject go, string[] names, System.Object[] values) {
        ModelImporter importer = (ModelImporter)assetImporter;
        var asset_name = Path.GetFileName(importer.assetPath);
        Debug.LogFormat("OnPostprocessGameObjectWithUserProperties(go = {0}) asset = {1}", go.name, asset_name);
        string str = null;
        Vector3 vec3 = Vector3.zero;
        for (int i = 0; i < names.Length; i++) {
            var name = names[i];
            var val = values[i];
            switch (name) {
            case "StringData":
                str = (string)val;
                break;
            case "VectorData":
                vec3 = (Vector3)(Vector4)val;
                break;
            default:
                Debug.LogFormat("Unknown Property : {0} : {1} : {2}", name, val.GetType().Name, val.ToString());
                break;
            }
        }
        if (str != null || vec3 != Vector3.zero) {
            var udh = go.AddComponent<UserDataHolder>();
            if (str != null) {
                udh.StringData = str;
            }
            udh.VectorData = vec3;
        }
    }
}

UserDataHodler.cs:

using UnityEngine;
 
public class UserDataHolder : MonoBehaviour {
    public string StringData;
    public Vector3 VectorData;
}

Note: You will want to open and edit WithMyData.fbx in the archive directly with Maya.

提示:你可以在MAYA里打开并编辑 WithMyData.fbx 

例子:FbxUserDataSample.zip

评论回复