duck

Unity 3D Auto Set Up OffMeshLink in AssetPostProcessor

Feb 15th, 2013
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.59 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections;
  4.  
  5. public class ModelImportProcessor : AssetPostprocessor
  6. {
  7.     // requires a CustomEditor of OffmeshLinkInspector!
  8.     void OnPostprocessModel (GameObject g)
  9.     {
  10.         Apply (g.transform);
  11.     }
  12.    
  13.     void Apply (Transform t)
  14.     {
  15.  
  16.         if (t.name.ToLower ().Contains ("ladder")) {
  17.             var link = t.gameObject.AddComponent<OffMeshLink> ();
  18.  
  19.             // find top & bottom of ladder
  20.            
  21.             SerializedObject linkObj = new SerializedObject (link);
  22.             var start = linkObj.FindProperty ("m_Start");
  23.             var end = linkObj.FindProperty ("m_End");
  24.             var layer = linkObj.FindProperty ("m_NavMeshLayer");
  25.            
  26.             Debug.Log ("ladder found");
  27.            
  28.             foreach (Transform child in t) {
  29.                 Debug.Log ("child: " + child.name);
  30.                 if (child.name.ToLower ().StartsWith ("top")) {
  31.                     start.objectReferenceValue = child;
  32.                     Debug.Log ("set start");
  33.                 }
  34.                 if (child.name.ToLower ().StartsWith ("bottom")) {
  35.                     end.objectReferenceValue = child;
  36.                     Debug.Log ("set end");
  37.                 }
  38.                
  39.             }
  40.            
  41.             layer.intValue = NavMesh.GetNavMeshLayerFromName ("Ladder");
  42.    
  43.            
  44.             linkObj.ApplyModifiedProperties ();
  45.            
  46.         }
  47.        
  48.         // Recurse
  49.         foreach (Transform child in t) {
  50.             Apply (child);
  51.         }
  52.     }
  53.    
  54. }
Advertisement
Add Comment
Please, Sign In to add comment