Advertisement
duck

Unity 3D asset post process split fbx into separate prefabs

Nov 13th, 2012
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections;
  4.  
  5. public class ModelProcessor : AssetPostprocessor {
  6.    
  7.    
  8.     void OnPreprocessModel()
  9.     {
  10.         ModelImporter modelImporter = (ModelImporter) assetImporter;
  11.         modelImporter.globalScale = 1;
  12.     }
  13.    
  14.     void OnPostprocessModel( GameObject g )
  15.     {
  16.         Process(g.transform);
  17.     }
  18.    
  19.     void Process (Transform t)
  20.     {
  21.  
  22.         if (t.name.ToLower().Contains(" proxy box"))
  23.         {
  24.             t.gameObject.AddComponent<BoxCollider>();
  25.         }
  26.        
  27.         if (t.name.ToLower().Contains(" proxy mesh"))
  28.         {
  29.             MeshCollider mc = t.gameObject.AddComponent<MeshCollider>();
  30.             mc.convex = true;      
  31.         }
  32.        
  33.         if (t.name.ToLower().Contains(" proxy"))
  34.         {
  35.             t.renderer.enabled = false;
  36.             Rigidbody rb = t.gameObject.AddComponent<Rigidbody>();
  37.             rb.isKinematic = true;
  38.         }
  39.        
  40.         // recurse
  41.         foreach (Transform child in t)
  42.         {
  43.             Process (child);   
  44.         }
  45.     }
  46.    
  47.    
  48.     void OnPostprocessGameObjectWithUserProperties (GameObject go, string[] propNames, System.Object[] values) {
  49.        
  50.         for (int i = 0; i < propNames.Length; i++) {
  51.            
  52.             string propName = propNames[i];
  53.             System.Object v = values[i];
  54.  
  55.             if (propName == "UDP3DSMAX")
  56.             {
  57.                 // max exports all props as single string. we must split by return char
  58.                 string[] lines = ((string)v).Split('\n');
  59.                
  60.                 foreach (string line in lines)
  61.                 {
  62.                     string prop = line.Trim();
  63.                     Debug.Log (prop);
  64.                    
  65.                     if (prop == "splitfbx")
  66.                     {
  67.  
  68.                         string path = System.IO.Path.GetDirectoryName(assetPath);
  69.                         string assetFileName = System.IO.Path.GetFileNameWithoutExtension(assetPath);
  70.                         System.IO.Directory.CreateDirectory(path+"/"+assetFileName);
  71.                         string prefabPath = path+"/"+assetFileName+"/"+go.name+".prefab";
  72.                         Object targetPrefab = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(Object));
  73.                    
  74.                         if (targetPrefab != null) {
  75.                             PrefabUtility.ReplacePrefab(go, targetPrefab, ReplacePrefabOptions.ReplaceNameBased);
  76.                         } else {
  77.                             PrefabUtility.CreatePrefab( prefabPath, go );
  78.                         }
  79.                     }
  80.  
  81.                    
  82.                     if (prop == "boxcollider")
  83.                     {
  84.                         go.AddComponent<BoxCollider>();
  85.                     }
  86.            
  87.                     if (prop == "spherecollider")
  88.                     {
  89.                         go.AddComponent<SphereCollider>();
  90.                     }
  91.            
  92.                     if (prop == "meshcollider")
  93.                     {
  94.                         go.AddComponent<MeshCollider>();
  95.                     }
  96.                        
  97.                     if (prop == "proxy")
  98.                     {
  99.                         go.renderer.enabled = false;
  100.                         if (go.GetComponent<Collider>() == null)
  101.                         {
  102.                             go.AddComponent<MeshCollider>();
  103.                         }
  104.                     }
  105.                        
  106.                     if (prop == "movable")
  107.                     {
  108.                         go.AddComponent<Rigidbody>();
  109.                         MeshCollider mc = go.GetComponent<MeshCollider>();
  110.                        
  111.                         if (mc != null)
  112.                         {
  113.                             mc.convex = true;  
  114.                         }
  115.                     }
  116.                    
  117.                 }
  118.                    
  119.             }
  120.            
  121.            
  122.         }
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement