Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Created February 27, 2023 07:02
Show Gist options
  • Save unitycoder/60564cb8cf658aea0c168ac7602eb0a4 to your computer and use it in GitHub Desktop.
Save unitycoder/60564cb8cf658aea0c168ac7602eb0a4 to your computer and use it in GitHub Desktop.
Read FBX Custom Properties (Unity Editor Script AssetPostprocessor)
// https://forum.unity.com/threads/how-to-import-user-defined-attributes-from-fbx-files.409877/
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;
}
}
}
using UnityEngine;
public class UserDataHolder : MonoBehaviour {
public string StringData;
public Vector3 VectorData;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment