duck

Unity 3D asset post process split fbx into separate prefabs

Nov 12th, 2012
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.33 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections;
  4.  
  5. public class AutoSplitFBX : AssetPostprocessor {
  6.  
  7.     void OnPostprocessGameObjectWithUserProperties (GameObject go, string[] propNames, System.Object[] values) {
  8.        
  9.         for (int i = 0; i < propNames.Length; i++) {
  10.            
  11.             string propName = propNames[i];
  12.             System.Object v = values[i];
  13.  
  14.             if (propName == "UDP3DSMAX")
  15.             {
  16.                 // max exports all props as single string. we must split by return char
  17.                 string[] lines = ((string)v).Split('\n');
  18.                
  19.                 foreach (string line in lines)
  20.                 {
  21.                     string prop = line.Trim();
  22.                     Debug.Log (prop);
  23.                    
  24.                     if (prop == "splitfbx")
  25.                     {
  26.  
  27.                         string path = System.IO.Path.GetDirectoryName(assetPath);
  28.                         string assetFileName = System.IO.Path.GetFileNameWithoutExtension(assetPath);
  29.                         System.IO.Directory.CreateDirectory(path+"/"+assetFileName);
  30.                         string prefabPath = path+"/"+assetFileName+"/"+go.name+".prefab";
  31.                         Object targetPrefab = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(Object));
  32.                        
  33.                         if (targetPrefab != null) {
  34.                             PrefabUtility.ReplacePrefab(go, targetPrefab, ReplacePrefabOptions.ReplaceNameBased);
  35.                         } else {
  36.                             PrefabUtility.CreatePrefab( prefabPath, go );
  37.                         }
  38.                     }
  39.                 }
  40.             }
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment