@@ -165,26 +165,38 @@ internal enum FbxNodeRelationType
165
165
Dictionary < FbxNode , Dictionary < FbxConstraint , System . Type > > MapConstrainedObjectToConstraints = new Dictionary < FbxNode , Dictionary < FbxConstraint , System . Type > > ( ) ;
166
166
167
167
/// <summary>
168
- /// Map Unity material name to FBX material object
168
+ /// Map Unity material ID to FBX material object
169
169
/// </summary>
170
- Dictionary < string , FbxSurfaceMaterial > MaterialMap = new Dictionary < string , FbxSurfaceMaterial > ( ) ;
170
+ Dictionary < int , FbxSurfaceMaterial > MaterialMap = new Dictionary < int , FbxSurfaceMaterial > ( ) ;
171
171
172
172
/// <summary>
173
173
/// Map texture filename name to FBX texture object
174
174
/// </summary>
175
175
Dictionary < string , FbxTexture > TextureMap = new Dictionary < string , FbxTexture > ( ) ;
176
176
177
177
/// <summary>
178
- /// Map the name of a prefab to an FbxMesh (for preserving instances)
178
+ /// Map the ID of a prefab to an FbxMesh (for preserving instances)
179
179
/// </summary>
180
- Dictionary < string , FbxMesh > SharedMeshes = new Dictionary < string , FbxMesh > ( ) ;
180
+ Dictionary < int , FbxMesh > SharedMeshes = new Dictionary < int , FbxMesh > ( ) ;
181
181
182
182
/// <summary>
183
183
/// Map for the Name of an Object to number of objects with this name.
184
184
/// Used for enforcing unique names on export.
185
185
/// </summary>
186
186
Dictionary < string , int > NameToIndexMap = new Dictionary < string , int > ( ) ;
187
187
188
+ /// <summary>
189
+ /// Map for the Material Name to number of materials with this name.
190
+ /// Used for enforcing unique names on export.
191
+ /// </summary>
192
+ Dictionary < string , int > MaterialNameToIndexMap = new Dictionary < string , int > ( ) ;
193
+
194
+ /// <summary>
195
+ /// Map for the Texture Name to number of textures with this name.
196
+ /// Used for enforcing unique names on export.
197
+ /// </summary>
198
+ Dictionary < string , int > TextureNameToIndexMap = new Dictionary < string , int > ( ) ;
199
+
188
200
/// <summary>
189
201
/// Format for creating unique names
190
202
/// </summary>
@@ -620,7 +632,8 @@ internal bool ExportTexture (Material unityMaterial, string unityPropName,
620
632
621
633
// Find or create an fbx texture and link it up to the fbx material.
622
634
if ( ! TextureMap . ContainsKey ( textureSourceFullPath ) ) {
623
- var fbxTexture = FbxFileTexture . Create ( fbxMaterial , fbxPropName + "_Texture" ) ;
635
+ var textureName = GetUniqueTextureName ( fbxPropName + "_Texture" ) ;
636
+ var fbxTexture = FbxFileTexture . Create ( fbxMaterial , textureName ) ;
624
637
fbxTexture . SetFileName ( textureSourceFullPath ) ;
625
638
fbxTexture . SetTextureUse ( FbxTexture . ETextureUse . eStandard ) ;
626
639
fbxTexture . SetMappingType ( FbxTexture . EMappingType . eUV ) ;
@@ -655,20 +668,23 @@ internal bool ExportMaterial (Material unityMaterial, FbxScene fbxScene, FbxNode
655
668
unityMaterial = DefaultMaterial ;
656
669
}
657
670
658
- var unityName = unityMaterial . name ;
659
- if ( MaterialMap . ContainsKey ( unityName ) ) {
660
- fbxNode . AddMaterial ( MaterialMap [ unityName ] ) ;
671
+ var unityID = unityMaterial . GetInstanceID ( ) ;
672
+ if ( MaterialMap . ContainsKey ( unityID ) ) {
673
+ fbxNode . AddMaterial ( MaterialMap [ unityID ] ) ;
661
674
return true ;
662
675
}
663
676
677
+ var unityName = unityMaterial . name ;
664
678
var fbxName = ExportOptions . UseMayaCompatibleNames
665
679
? ConvertToMayaCompatibleName ( unityName ) : unityName ;
666
680
681
+ fbxName = GetUniqueMaterialName ( fbxName ) ;
682
+
667
683
if ( Verbose ) {
668
684
if ( unityName != fbxName ) {
669
- Debug . Log ( string . Format ( "exporting material {0} as {1}" , unityName , fbxName ) ) ;
685
+ Debug . Log ( string . Format ( "exporting material {0} as {1}" , unityID , fbxName ) ) ;
670
686
} else {
671
- Debug . Log ( string . Format ( "exporting material {0}" , unityName ) ) ;
687
+ Debug . Log ( string . Format ( "exporting material {0}" , unityID ) ) ;
672
688
}
673
689
}
674
690
@@ -700,7 +716,7 @@ internal bool ExportMaterial (Material unityMaterial, FbxScene fbxScene, FbxNode
700
716
ExportTexture ( unityMaterial , "_SpecGlosMap" , fbxMaterial , FbxSurfaceMaterial . sSpecular ) ;
701
717
}
702
718
703
- MaterialMap . Add ( unityName , fbxMaterial ) ;
719
+ MaterialMap . Add ( unityID , fbxMaterial ) ;
704
720
fbxNode . AddMaterial ( fbxMaterial ) ;
705
721
return true ;
706
722
}
@@ -1219,7 +1235,7 @@ internal bool ExportTransform (UnityEngine.Transform unityTransform, FbxNode fbx
1219
1235
/// if this game object is a model prefab then export with shared components
1220
1236
/// </summary>
1221
1237
[ SecurityPermission ( SecurityAction . LinkDemand ) ]
1222
- private bool ExportInstance ( GameObject unityGo , FbxNode fbxNode )
1238
+ private bool ExportInstance ( GameObject unityGo , FbxScene fbxScene , FbxNode fbxNode )
1223
1239
{
1224
1240
if ( ! unityGo || fbxNode == null )
1225
1241
{
@@ -1242,10 +1258,10 @@ private bool ExportInstance(GameObject unityGo, FbxNode fbxNode)
1242
1258
1243
1259
FbxMesh fbxMesh = null ;
1244
1260
1245
- if ( ! SharedMeshes . TryGetValue ( unityPrefabParent . name , out fbxMesh ) )
1261
+ if ( ! SharedMeshes . TryGetValue ( unityPrefabParent . GetInstanceID ( ) , out fbxMesh ) )
1246
1262
{
1247
1263
if ( ExportMesh ( unityGo , fbxNode ) && fbxNode . GetMesh ( ) != null ) {
1248
- SharedMeshes [ unityPrefabParent . name ] = fbxNode . GetMesh ( ) ;
1264
+ SharedMeshes [ unityPrefabParent . GetInstanceID ( ) ] = fbxNode . GetMesh ( ) ;
1249
1265
return true ;
1250
1266
}
1251
1267
return false ;
@@ -1259,10 +1275,15 @@ private bool ExportInstance(GameObject unityGo, FbxNode fbxNode)
1259
1275
if ( materials != null )
1260
1276
{
1261
1277
foreach ( var mat in materials ) {
1262
- if ( MaterialMap . TryGetValue ( mat . name , out newMaterial ) )
1278
+ if ( MaterialMap . TryGetValue ( mat . GetInstanceID ( ) , out newMaterial ) )
1263
1279
{
1264
1280
fbxNode . AddMaterial ( newMaterial ) ;
1265
1281
}
1282
+ else
1283
+ {
1284
+ // create new material
1285
+ ExportMaterial ( mat , fbxScene , fbxNode ) ;
1286
+ }
1266
1287
}
1267
1288
}
1268
1289
@@ -2515,18 +2536,58 @@ private void SetDefaultCamera (FbxScene fbxScene)
2515
2536
/// </summary>
2516
2537
/// <returns>Unique name</returns>
2517
2538
/// <param name="name">Name</param>
2518
- private string GetUniqueName ( string name )
2539
+ /// <param name="nameToIndexMap">The dictionary to use to map name to # of occurences</param>
2540
+ private string GetUniqueName ( string name , ref Dictionary < string , int > nameToIndexMap )
2519
2541
{
2520
2542
var uniqueName = name ;
2521
- if ( NameToIndexMap . ContainsKey ( name ) ) {
2522
- uniqueName = string . Format ( UniqueNameFormat , name , NameToIndexMap [ name ] ) ;
2523
- NameToIndexMap [ name ] ++ ;
2524
- } else {
2525
- NameToIndexMap [ name ] = 1 ;
2543
+ if ( nameToIndexMap . ContainsKey ( name ) )
2544
+ {
2545
+ uniqueName = string . Format ( UniqueNameFormat , name , nameToIndexMap [ name ] ) ;
2546
+ nameToIndexMap [ name ] ++ ;
2547
+ }
2548
+ else
2549
+ {
2550
+ nameToIndexMap [ name ] = 1 ;
2526
2551
}
2527
2552
return uniqueName ;
2528
2553
}
2529
2554
2555
+ /// <summary>
2556
+ /// Ensures that the inputted name is unique.
2557
+ /// If a duplicate name is found, then it is incremented.
2558
+ /// e.g. Sphere becomes Sphere_1
2559
+ /// </summary>
2560
+ /// <returns>Unique name</returns>
2561
+ /// <param name="name">Name</param>
2562
+ private string GetUniqueFbxNodeName ( string name )
2563
+ {
2564
+ return GetUniqueName ( name , ref NameToIndexMap ) ;
2565
+ }
2566
+
2567
+ /// <summary>
2568
+ /// Ensures that the inputted material name is unique.
2569
+ /// If a duplicate name is found, then it is incremented.
2570
+ /// e.g. mat becomes mat_1
2571
+ /// </summary>
2572
+ /// <param name="name">Name</param>
2573
+ /// <returns>Unique material name</returns>
2574
+ private string GetUniqueMaterialName ( string name )
2575
+ {
2576
+ return GetUniqueName ( name , ref MaterialNameToIndexMap ) ;
2577
+ }
2578
+
2579
+ /// <summary>
2580
+ /// Ensures that the inputted texture name is unique.
2581
+ /// If a duplicate name is found, then it is incremented.
2582
+ /// e.g. tex becomes tex_1
2583
+ /// </summary>
2584
+ /// <param name="name">Name</param>
2585
+ /// <returns>Unique texture name</returns>
2586
+ private string GetUniqueTextureName ( string name )
2587
+ {
2588
+ return GetUniqueName ( name , ref TextureNameToIndexMap ) ;
2589
+ }
2590
+
2530
2591
/// <summary>
2531
2592
/// Create a fbxNode from unityGo.
2532
2593
/// </summary>
@@ -2546,7 +2607,7 @@ private FbxNode CreateFbxNode(GameObject unityGo, FbxScene fbxScene)
2546
2607
}
2547
2608
}
2548
2609
2549
- FbxNode fbxNode = FbxNode . Create ( fbxScene , GetUniqueName ( fbxName ) ) ;
2610
+ FbxNode fbxNode = FbxNode . Create ( fbxScene , GetUniqueFbxNodeName ( fbxName ) ) ;
2550
2611
2551
2612
// Default inheritance type in FBX is RrSs, which causes scaling issues in Maya as
2552
2613
// both Maya and Unity use RSrs inheritance by default.
@@ -3126,7 +3187,7 @@ private bool ExportComponents(FbxScene fbxScene)
3126
3187
var fbxNode = entry . Value ;
3127
3188
3128
3189
// try export mesh
3129
- bool exportedMesh = ExportInstance ( unityGo , fbxNode ) ;
3190
+ bool exportedMesh = ExportInstance ( unityGo , fbxScene , fbxNode ) ;
3130
3191
3131
3192
if ( ! exportedMesh ) {
3132
3193
exportedMesh = ExportMesh ( unityGo , fbxNode ) ;
0 commit comments