@@ -52,7 +52,9 @@ static void ImportURDF(MenuCommand menuCommand) {
5252 }
5353
5454 ZOSimDocumentRoot documentRoot = ZOImportURDF . Import ( filePath ) ;
55-
55+ GameObject prefabGo = MakeGameObjectPrefab ( documentRoot . gameObject , saveToDirectory ) ;
56+ Object . DestroyImmediate ( documentRoot . gameObject ) ;
57+ PrefabUtility . InstantiatePrefab ( prefabGo ) ;
5658
5759 }
5860
@@ -61,30 +63,58 @@ static void ImportOBJ(MenuCommand menuCommand) {
6163 string filePath = EditorUtility . OpenFilePanel ( "Import OBJ" , "." , "obj" ) ;
6264 string saveToDirectory = EditorUtility . OpenFolderPanel ( "Save to folder..." , Path . Combine ( Path . GetDirectoryName ( Application . dataPath ) , "Assets" ) , "" ) ;
6365
64- if ( filePath . Length == 0 ) {
66+ if ( string . IsNullOrEmpty ( filePath ) == true ) {
6567 return ;
6668 }
6769
6870 GameObject go = ZOImportOBJ . Import ( filePath ) ;
71+ GameObject prefabGo = MakeGameObjectPrefab ( go , saveToDirectory ) ;
72+ Object . DestroyImmediate ( go ) ;
73+ PrefabUtility . InstantiatePrefab ( prefabGo ) ;
74+
75+
76+ }
77+
78+ static GameObject MakeGameObjectPrefab ( GameObject gameObject , string saveToDirectory ) {
79+
80+ // save any meshes and materials
81+ MeshFilter [ ] meshFilters = gameObject . GetComponentsInChildren < MeshFilter > ( ) ;
82+ MeshRenderer [ ] meshRenderers = gameObject . GetComponentsInChildren < MeshRenderer > ( ) ;
83+
84+ foreach ( MeshRenderer meshRenderer in meshRenderers ) {
85+ SaveMaterial ( meshRenderer . sharedMaterial , saveToDirectory ) ;
86+ }
6987
70- // build a prefab from the GameObject
71- MeshFilter [ ] meshFilters = go . GetComponentsInChildren < MeshFilter > ( ) ;
7288
7389 foreach ( MeshFilter meshFilter in meshFilters ) {
74- string meshAssetPath = Path . Combine ( saveToDirectory , $ "{ meshFilter . name } .asset") ;
75- meshAssetPath = meshAssetPath . Substring ( Application . dataPath . Length ) ;
76- Debug . Log ( "INFO: saving to relative path: " + meshAssetPath ) ;
77- Mesh msh = new Mesh ( ) ;
78- msh . vertices = meshFilter . sharedMesh . vertices ;
79- msh . triangles = meshFilter . sharedMesh . triangles ;
80- msh . uv = meshFilter . sharedMesh . uv ;
81- msh . uv2 = meshFilter . sharedMesh . uv2 ;
82- msh . RecalculateNormals ( ) ;
83- msh . RecalculateBounds ( ) ;
84-
85- AssetDatabase . CreateAsset ( msh , "Assets/" + meshAssetPath ) ;
90+ SaveMesh ( meshFilter . sharedMesh , saveToDirectory ) ;
8691 }
8792
93+ string path = Path . Combine ( saveToDirectory , gameObject . name + ".prefab" ) ;
94+ path = FileUtil . GetProjectRelativePath ( path ) ;
95+
96+ return PrefabUtility . SaveAsPrefabAsset ( gameObject , path ) ;
97+
98+ }
99+
100+
101+ public static void SaveMaterial ( Material material , string saveToDirectory ) {
102+ string path = Path . Combine ( saveToDirectory , material . name + ".mat" ) ;
103+ path = FileUtil . GetProjectRelativePath ( path ) ;
104+ path = AssetDatabase . GenerateUniqueAssetPath ( path ) ;
105+ Debug . Log ( $ "INFO: Saving material at path: { path } ") ;
106+ AssetDatabase . CreateAsset ( material , path ) ;
107+ AssetDatabase . SaveAssets ( ) ;
108+ }
109+
110+ public static void SaveMesh ( Mesh mesh , string saveToDirectory ) {
111+ string path = Path . Combine ( saveToDirectory , mesh . name + ".asset" ) ;
112+ path = FileUtil . GetProjectRelativePath ( path ) ;
113+ Debug . Log ( $ "INFO: Saving mesh at path: { path } ") ;
114+ path = AssetDatabase . GenerateUniqueAssetPath ( path ) ;
115+ AssetDatabase . CreateAsset ( mesh , path ) ;
116+ AssetDatabase . SaveAssets ( ) ;
117+
88118 }
89119
90120
0 commit comments