duck

Unity 3D asset post process split fbx into separate prefabs

Nov 13th, 2012
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.62 KB | None | 0 0
  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);
  17.     }
  18.    
  19.     void Process (GameObject g)
  20.     {
  21.         if (g.name.Contains("Proxy Mesh"))
  22.         {
  23.             g.renderer.enabled = false;
  24.             g.AddComponent<MeshCollider>();
  25.         }
  26.         if (g.name.Contains("Proxy Box"))
  27.         {
  28.             g.renderer.enabled = false;
  29.             g.AddComponent<BoxCollider>();
  30.         }
  31.        
  32.         foreach(Transform child in g.transform)
  33.         {
  34.             Process(child.gameObject); 
  35.         }
  36.        
  37.     }  
  38.    
  39.    
  40.     void OnPostprocessGameObjectWithUserProperties (GameObject go, string[] propNames, System.Object[] values) {
  41.        
  42.         for (int i = 0; i < propNames.Length; i++) {
  43.            
  44.             string propName = propNames[i];
  45.             System.Object v = values[i];
  46.  
  47.             if (propName == "UDP3DSMAX")
  48.             {
  49.                 // max exports all props as single string. we must split by return char
  50.                 string[] lines = ((string)v).Split('\n');
  51.                
  52.                 foreach (string line in lines)
  53.                 {
  54.                     string prop = line.Trim();
  55.                     Debug.Log (prop);
  56.                    
  57.                     if (prop == "splitfbx")
  58.                     {
  59.  
  60.                         string path = System.IO.Path.GetDirectoryName(assetPath);
  61.                         string assetFileName = System.IO.Path.GetFileNameWithoutExtension(assetPath);
  62.                         System.IO.Directory.CreateDirectory(path+"/"+assetFileName);
  63.                         string prefabPath = path+"/"+assetFileName+"/"+go.name+".prefab";
  64.                         Object targetPrefab = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(Object));
  65.                    
  66.                         if (targetPrefab != null) {
  67.                             PrefabUtility.ReplacePrefab(go, targetPrefab, ReplacePrefabOptions.ReplaceNameBased);
  68.                         } else {
  69.                             PrefabUtility.CreatePrefab( prefabPath, go );
  70.                         }
  71.                     }
  72.  
  73.                    
  74.                     if (prop == "boxcollider")
  75.                     {
  76.                         go.AddComponent<BoxCollider>();
  77.                     }
  78.            
  79.                     if (prop == "spherecollider")
  80.                     {
  81.                         go.AddComponent<SphereCollider>();
  82.                     }
  83.            
  84.                     if (prop == "meshcollider")
  85.                     {
  86.                         go.AddComponent<MeshCollider>();
  87.                     }
  88.                        
  89.                     if (prop == "proxy")
  90.                     {
  91.                         go.renderer.enabled = false;
  92.                         if (go.GetComponent<Collider>() == null)
  93.                         {
  94.                             go.AddComponent<MeshCollider>();
  95.                         }
  96.                     }
  97.                        
  98.                     if (prop == "movable")
  99.                     {
  100.                         go.AddComponent<Rigidbody>();
  101.                         MeshCollider mc = go.GetComponent<MeshCollider>();
  102.                        
  103.                         if (mc != null)
  104.                         {
  105.                             mc.convex = true;  
  106.                         }
  107.                             }
  108.                    
  109.                 }
  110.                    
  111.             }
  112.            
  113.            
  114.         }
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment