Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Created March 14, 2021 16:47
Show Gist options
  • Select an option

  • Save unitycoder/06178c8dba730db057fce3c157ca19cd to your computer and use it in GitHub Desktop.

Select an option

Save unitycoder/06178c8dba730db057fce3c157ca19cd to your computer and use it in GitHub Desktop.
import user defined attributes from FBX files
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;
}
@unitycoder

Copy link
Copy Markdown
Author

using dummy transform position for animated alpha value:

public class MaterialDriver : MonoBehaviour
 {
     Transform MaterialDriver;
 
     void Update()
     {
         // Get the current color
         Color materialColor = renderer.material.color;
         // Set the alpha of the material from the local (animated) x position of the transform
         materialColor.a = MaterialDriver.localPosition.x;
         // Set the new color
         renderer.material.color = materialColor;        
     }
 }

http://answers.unity.com/answers/653494/view.html

@unitycoder

Copy link
Copy Markdown
Author
  • In Maya (or Max...), add your custom attribute (for us, using PascalCase/UpperCamelCase only)
  • In Unity, check the box for "Animated Custom Properties" in the FBX import settings
  • On the animator you're assigning this clip to in the scene hierarchy, add a parameter that exactly matches -- including case -- the original name given to the custom attribute in your modeling software

https://forum.unity.com/threads/new-fbx-exporter-in-2017-2.499889/page-7#post-3596232

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment