@@ -32,7 +32,20 @@ protected void SetGameObjectsToConvert(IEnumerable<GameObject> toConvert){
32
32
m_toConvert = toConvert . OrderBy ( go => go . name ) . ToArray ( ) ;
33
33
34
34
if ( m_toConvert . Length == 1 ) {
35
- m_prefabFileName = m_toConvert [ 0 ] . name ;
35
+ var go = m_toConvert [ 0 ] ;
36
+ // check if the GameObject is a model instance, use as default filename and path if it is
37
+ PrefabType unityPrefabType = PrefabUtility . GetPrefabType ( go ) ;
38
+ if ( unityPrefabType == PrefabType . ModelPrefabInstance && go . Equals ( PrefabUtility . FindPrefabRoot ( go ) ) ) {
39
+ var mainAsset = PrefabUtility . GetPrefabParent ( go ) as GameObject ;
40
+ var mainAssetRelPath = AssetDatabase . GetAssetPath ( mainAsset ) ;
41
+ // remove Assets/ from beginning of path
42
+ mainAssetRelPath = mainAssetRelPath . Substring ( "Assets" . Length ) ;
43
+
44
+ m_prefabFileName = System . IO . Path . GetFileNameWithoutExtension ( mainAssetRelPath ) ;
45
+ ExportSettings . AddFbxSavePath ( System . IO . Path . GetDirectoryName ( mainAssetRelPath ) ) ;
46
+ } else {
47
+ m_prefabFileName = go . name ;
48
+ }
36
49
} else if ( m_toConvert . Length > 1 ) {
37
50
m_prefabFileName = "(automatic)" ;
38
51
}
@@ -56,19 +69,51 @@ protected override void Export ()
56
69
var prefabDirPath = ExportSettings . GetPrefabAbsoluteSavePath ( ) ;
57
70
var prefabPath = System . IO . Path . Combine ( prefabDirPath , m_prefabFileName + ".prefab" ) ;
58
71
59
- // check if file already exists, give a warning if it does
60
- if ( ! OverwriteExistingFile ( fbxPath ) || ! OverwriteExistingFile ( prefabPath ) ) {
61
- return ;
62
- }
63
-
64
72
if ( m_toConvert == null ) {
65
73
Debug . LogError ( "FbxExporter: missing object for conversion" ) ;
66
74
return ;
67
75
}
68
76
69
77
if ( m_toConvert . Length == 1 ) {
78
+ var go = m_toConvert [ 0 ] ;
79
+
80
+ if ( ! OverwriteExistingFile ( prefabPath ) ) {
81
+ return ;
82
+ }
83
+
84
+ // Only create the prefab (no FBX export) if we have selected the root of a model prefab instance.
85
+ // Children of model prefab instances will also have "model prefab instance"
86
+ // as their prefab type, so it is important that it is the root that is selected.
87
+ //
88
+ // e.g. If I have the following hierarchy:
89
+ // Cube
90
+ // -- Sphere
91
+ //
92
+ // Both the Cube and Sphere will have ModelPrefabInstance as their prefab type.
93
+ // However, when selecting the Sphere to convert, we don't want to connect it to the
94
+ // existing FBX but create a new FBX containing just the sphere.
95
+ PrefabType unityPrefabType = PrefabUtility . GetPrefabType ( go ) ;
96
+ if ( unityPrefabType == PrefabType . ModelPrefabInstance && go . Equals ( PrefabUtility . FindPrefabRoot ( go ) ) ) {
97
+ // don't re-export fbx
98
+ // create prefab out of model instance in scene, link to existing fbx
99
+ var mainAsset = PrefabUtility . GetPrefabParent ( go ) as GameObject ;
100
+ var mainAssetRelPath = AssetDatabase . GetAssetPath ( mainAsset ) ;
101
+ var mainAssetAbsPath = System . IO . Directory . GetParent ( Application . dataPath ) + "/" + mainAssetRelPath ;
102
+ var relPrefabPath = ExportSettings . GetProjectRelativePath ( prefabPath ) ;
103
+
104
+ if ( string . Equals ( System . IO . Path . GetFullPath ( fbxPath ) , System . IO . Path . GetFullPath ( mainAssetAbsPath ) ) ) {
105
+ ConvertToModel . SetupFbxPrefab ( go , mainAsset , relPrefabPath , mainAssetAbsPath ) ;
106
+ return ;
107
+ }
108
+ }
109
+
110
+ // check if file already exists, give a warning if it does
111
+ if ( ! OverwriteExistingFile ( fbxPath ) ) {
112
+ return ;
113
+ }
114
+
70
115
ConvertToModel . Convert (
71
- m_toConvert [ 0 ] , fbxFullPath : fbxPath , prefabFullPath : prefabPath , exportOptions : ExportSettings . instance . convertToPrefabSettings . info
116
+ go , fbxFullPath : fbxPath , prefabFullPath : prefabPath , exportOptions : ExportSettings . instance . convertToPrefabSettings . info
72
117
) ;
73
118
return ;
74
119
}
0 commit comments