//static private readonly NLog.Logger LOGGER = LogManager.GetCurrentClassLogger(); /// <summary> /// Handles post process on all assets within Unity Editor. Finds all meshes and creates /// a Unity prefab allowing easy instation at runtime. For more information about this Unity /// Message Handle: http://docs.unity3d.com/ScriptReference/AssetPostprocessor.OnPostprocessAllAssets.html /// </summary> /// <param name="importedAssets">List of paths to assets that have been imported</param> /// <param name="deletedAssets">List of paths to assets that have been deleted</param> /// <param name="movedAssets">List of paths to assets that have been moved</param> /// <param name="movedFromAssetPaths">List of paths to assets that have been moved from paths</param> static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths) { if (importedAssets != null && importedAssets.Length > 0) { FileType type = FileType.UNKNOWN; UrdfDb db = new UrdfDb(); foreach (string assetPath in importedAssets) { string filename = FileManagerImpl.GetFileName(assetPath, false); type = FileManagerImpl.GetFileType(assetPath); UnityEngine.Object asset = AssetDatabase.LoadMainAssetAtPath(assetPath); if (asset == null) { continue; } // Creating prefab here will cause this function to be invoked again causing an infinite loop of // assets being generated. Proper Check of the Object is of type GameObject, isn't part of a prefab // and that the asset is a proper model that can become a prefab. if (asset.GetType() == typeof(GameObject) && PrefabUtility.GetPrefabParent(asset) == null && PrefabUtility.GetPrefabType(asset) == PrefabType.ModelPrefab) { // To properly create a prefab of the object we need to instantiate it into // the game world and save that object as a prefab. GameObject go = GameObject.Instantiate <GameObject>((GameObject)asset); go.name = asset.name; // remove the (clone) within the name. MeshRenderer[] children = go.GetComponentsInChildren <MeshRenderer>(); foreach (MeshRenderer child in children) { if (child.transform == go.transform) { continue; } if (child.gameObject.GetComponent <Renderer>()) { child.gameObject.AddComponent <MeshCollider>(); MeshCollider mc = child.gameObject.GetComponent <MeshCollider>(); mc.sharedMesh = child.gameObject.GetComponent <MeshFilter>().sharedMesh; } } PrefabUtility.CreatePrefab(string.Format(prefabItemPathFormat, go.name), go); UrdfItemModel item = new UrdfItemModel(); item.name = filename; item.prefabFilename = go.name; item.visibility = 1; item.usable = 0; db.AddSensor(item); db.Save(); GameObject.DestroyImmediate(go); } else if (type == FileType.URDF || type == FileType.XACRO) { string prefabName = CreateUrdfRobot(assetPath); if (!String.IsNullOrEmpty(prefabName)) { UrdfItemModel item = new UrdfItemModel(); item.name = filename; item.urdfFilename = assetPath; item.prefabFilename = prefabName; item.visibility = 1; db.AddSensor(item); db.Save(); } } } } }
/// <summary> /// Ensure to save changes to the db when application exits. This is important only /// for DbMaintenance running in the Unity Editor. /// </summary> public void OnApplicationQuit() { urdf.Save(); }