@@ -45,12 +45,23 @@ protected void SetGameObjectsToConvert(IEnumerable<GameObject> toConvert){
45
45
46
46
TransferAnimationSource = null ;
47
47
TransferAnimationDest = null ;
48
-
49
48
if ( ToExport . Length == 1 ) {
50
- m_prefabFileName = ToExport [ 0 ] . name ;
49
+ var go = ModelExporter . GetGameObject ( ToExport [ 0 ] ) ;
50
+ // check if the GameObject is a model instance, use as default filename and path if it is
51
+ if ( ConvertToModel . IsModelInstance ( go ) ) {
52
+ var mainAsset = PrefabUtility . GetPrefabParent ( go ) as GameObject ;
53
+ var mainAssetRelPath = AssetDatabase . GetAssetPath ( mainAsset ) ;
54
+ // remove Assets/ from beginning of path
55
+ mainAssetRelPath = mainAssetRelPath . Substring ( "Assets" . Length ) ;
56
+
57
+ m_prefabFileName = System . IO . Path . GetFileNameWithoutExtension ( mainAssetRelPath ) ;
58
+ ExportSettings . AddFbxSavePath ( System . IO . Path . GetDirectoryName ( mainAssetRelPath ) ) ;
59
+ }
60
+ else {
61
+ m_prefabFileName = ToExport [ 0 ] . name ;
62
+ }
51
63
52
64
// if only one object selected, set transfer source/dest to this object
53
- var go = ModelExporter . GetGameObject ( ToExport [ 0 ] ) ;
54
65
if ( go ) {
55
66
TransferAnimationSource = go . transform ;
56
67
TransferAnimationDest = go . transform ;
@@ -79,18 +90,38 @@ protected override void Export ()
79
90
var prefabDirPath = ExportSettings . GetPrefabAbsoluteSavePath ( ) ;
80
91
var prefabPath = System . IO . Path . Combine ( prefabDirPath , m_prefabFileName + ".prefab" ) ;
81
92
82
- // check if file already exists, give a warning if it does
83
- if ( ! OverwriteExistingFile ( fbxPath ) || ! OverwriteExistingFile ( prefabPath ) ) {
84
- return ;
85
- }
86
-
87
93
if ( ToExport == null ) {
88
94
Debug . LogError ( "FbxExporter: missing object for conversion" ) ;
89
95
return ;
90
96
}
91
97
92
98
if ( ToExport . Length == 1 ) {
93
99
var go = ModelExporter . GetGameObject ( ToExport [ 0 ] ) ;
100
+
101
+ if ( ! OverwriteExistingFile ( prefabPath ) ) {
102
+ return ;
103
+ }
104
+
105
+ // Only create the prefab (no FBX export) if we have selected the root of a model prefab instance.
106
+ if ( ConvertToModel . IsModelInstance ( go ) ) {
107
+ // don't re-export fbx
108
+ // create prefab out of model instance in scene, link to existing fbx
109
+ var mainAsset = PrefabUtility . GetPrefabParent ( go ) as GameObject ;
110
+ var mainAssetRelPath = AssetDatabase . GetAssetPath ( mainAsset ) ;
111
+ var mainAssetAbsPath = System . IO . Directory . GetParent ( Application . dataPath ) + "/" + mainAssetRelPath ;
112
+ var relPrefabPath = ExportSettings . GetProjectRelativePath ( prefabPath ) ;
113
+
114
+ if ( string . Equals ( System . IO . Path . GetFullPath ( fbxPath ) , System . IO . Path . GetFullPath ( mainAssetAbsPath ) ) ) {
115
+ ConvertToModel . SetupFbxPrefab ( go , mainAsset , relPrefabPath , mainAssetAbsPath ) ;
116
+ return ;
117
+ }
118
+ }
119
+
120
+ // check if file already exists, give a warning if it does
121
+ if ( ! OverwriteExistingFile ( fbxPath ) ) {
122
+ return ;
123
+ }
124
+
94
125
ConvertToModel . Convert (
95
126
go , fbxFullPath : fbxPath , prefabFullPath : prefabPath , exportOptions : ExportSettings . instance . convertToPrefabSettings . info
96
127
) ;
0 commit comments