@@ -24,9 +24,6 @@ namespace FbxExporters
24
24
/// </summary>
25
25
public /*static*/ class FbxPrefabAutoUpdater : UnityEditor . AssetPostprocessor
26
26
{
27
- public delegate string getFBXNameFn ( string nodeName ) ;
28
-
29
-
30
27
#if UNITY_EDITOR
31
28
public const string FBX_PREFAB_FILE = "/FbxPrefab.cs" ;
32
29
#else
@@ -162,14 +159,14 @@ public FbxPrefabUtility(FbxPrefab fbxPrefab){
162
159
m_fbxPrefab = fbxPrefab ;
163
160
}
164
161
165
- // Check if the user has specified a new name in the inspector
162
+ /// <summary>
163
+ /// Utility function: check if the user has specified a matching Unity name in the inspector
164
+ /// </summary>
166
165
public string getUnityObjectName ( string fbxObjectName )
167
166
{
168
167
string newNameInUnity = fbxObjectName ;
169
- // UnityEngine.Assertions.Assert.IsTrue(m_fbxPrefab != null);
170
- //UnityEngine.Assertions.Assert.IsTrue(m_fbxPrefab.NameMapping != null);
171
168
if ( fbxObjectName != "" && m_fbxPrefab . NameMapping != null ) {
172
- for ( int i = 0 ; i < m_fbxPrefab . NameMapping . Length ; i ++ )
169
+ for ( int i = 0 ; i < m_fbxPrefab . NameMapping . Count ; i ++ )
173
170
{
174
171
if ( fbxObjectName == m_fbxPrefab . NameMapping [ i ] . FBXObjectName ) {
175
172
newNameInUnity = m_fbxPrefab . NameMapping [ i ] . UnityObjectName ;
@@ -179,13 +176,14 @@ public string getUnityObjectName(string fbxObjectName)
179
176
}
180
177
return newNameInUnity ;
181
178
}
182
-
183
- // Check if the user has specified an old name in the inspector
179
+ /// <summary>
180
+ /// Utility function: check if the user has specified a matching FBX name in the inspector
181
+ /// </summary>
184
182
public string getFBXObjectName ( string unityObjectName )
185
183
{
186
184
string oldNameInFBX = unityObjectName ;
187
185
if ( unityObjectName != "" && m_fbxPrefab . NameMapping != null ) {
188
- for ( int i = 0 ; i < m_fbxPrefab . NameMapping . Length ; i ++ )
186
+ for ( int i = 0 ; i < m_fbxPrefab . NameMapping . Count ; i ++ )
189
187
{
190
188
if ( unityObjectName == m_fbxPrefab . NameMapping [ i ] . UnityObjectName ) {
191
189
oldNameInFBX = m_fbxPrefab . NameMapping [ i ] . FBXObjectName ;
@@ -340,12 +338,12 @@ public class FbxRepresentation
340
338
/// <summary>
341
339
/// Build a hierarchical representation based on a transform.
342
340
/// </summary>
343
- public FbxRepresentation ( Transform xfo , getFBXNameFn getFBXName , bool isRoot = true )
341
+ public FbxRepresentation ( Transform xfo , bool isRoot = true )
344
342
{
345
343
m_children = new Dictionary < string , FbxRepresentation > ( ) ;
346
344
347
345
foreach ( Transform child in xfo ) {
348
- m_children . Add ( child . name , new FbxRepresentation ( child , getFBXName , isRoot : false ) ) ;
346
+ m_children . Add ( child . name , new FbxRepresentation ( child , isRoot : false ) ) ;
349
347
}
350
348
foreach ( var component in xfo . GetComponents < Component > ( ) ) {
351
349
// ignore missing components
@@ -447,7 +445,7 @@ public static string EscapeString(string str) {
447
445
return builder . ToString ( ) ;
448
446
}
449
447
450
- void InitFromJson ( string json , getFBXNameFn getFBXName , ref int index )
448
+ void InitFromJson ( string json , ref int index )
451
449
{
452
450
Consume ( '{' , json , ref index ) ;
453
451
if ( Consume ( '}' , json , ref index , required : false ) ) {
@@ -464,7 +462,7 @@ void InitFromJson(string json, getFBXNameFn getFBXName, ref int index)
464
462
// it's the name of a component, and we store its value as a string.
465
463
bool isChild = ( name . Length > 0 ) && ( name [ 0 ] == '-' ) ;
466
464
if ( isChild ) {
467
- var subrep = new FbxRepresentation ( json , getFBXName , ref index ) ;
465
+ var subrep = new FbxRepresentation ( json , ref index ) ;
468
466
Add ( ref m_children , name . Substring ( 1 ) , subrep ) ;
469
467
//Add(ref m_children, getFBXName(name.Substring(1)), subrep);
470
468
} else {
@@ -476,14 +474,14 @@ void InitFromJson(string json, getFBXNameFn getFBXName, ref int index)
476
474
}
477
475
}
478
476
479
- public FbxRepresentation ( string json , getFBXNameFn fn , ref int index ) {
480
- InitFromJson ( json , fn , ref index ) ;
477
+ public FbxRepresentation ( string json , ref int index ) {
478
+ InitFromJson ( json , ref index ) ;
481
479
}
482
480
483
- public FbxRepresentation ( string json , getFBXNameFn fn ) {
481
+ public FbxRepresentation ( string json ) {
484
482
if ( string . IsNullOrEmpty ( json ) ) { return ; }
485
483
int index = 0 ;
486
- InitFromJson ( json , fn , ref index ) ;
484
+ InitFromJson ( json , ref index ) ;
487
485
}
488
486
489
487
void ToJsonHelper ( System . Text . StringBuilder builder ) {
@@ -589,7 +587,7 @@ public Data(FbxRepresentation fbxrep) {
589
587
InitHelper ( fbxrep , "" ) ;
590
588
}
591
589
592
- public Data ( Transform xfo , getFBXNameFn fn ) : this ( new FbxRepresentation ( xfo , getFBXName : fn ) ) {
590
+ public Data ( Transform xfo ) : this ( new FbxRepresentation ( xfo ) ) {
593
591
}
594
592
595
593
/// <summary>
@@ -797,14 +795,11 @@ void ClassifyReparenting()
797
795
798
796
var prefabParent = m_prefabData . GetParent ( prefabNodeName ) ;
799
797
var oldParent = m_oldFbxData . GetParent ( prefabNodeName ) ;
798
+ // The newFbxData contains the fbx name, so we get the parent from the matching prefabNodeName equivalent
800
799
var newParent = m_newFbxData . GetParent ( m_fbxPrefabUtility . getFBXObjectName ( prefabNodeName ) ) ;
801
800
802
- // If it's a name mapping, don't add a reparenting, we'll rename it later
803
- if ( oldParent == m_fbxPrefabUtility . getUnityObjectName ( newParent ) ) {
804
- continue ;
805
- }
806
-
807
- if ( oldParent != newParent && prefabParent != newParent ) {
801
+ // If it's a name mapping, don't add a reparenting, we'll rename it later in ImplementUpdates()
802
+ if ( oldParent != newParent && prefabParent != newParent && oldParent != m_fbxPrefabUtility . getUnityObjectName ( newParent ) ) {
808
803
// Conflict in this case:
809
804
// if (oldParent != prefabParent && !ShouldDestroy(prefabParent))
810
805
@@ -862,27 +857,28 @@ void ClassifyComponents(Transform newFbx, Transform prefab)
862
857
// about what components might be on it.
863
858
foreach ( var nodeNameInUpdatedPrefab in m_nodesInUpdatedPrefab )
864
859
{
865
- if ( ! m_newFbxData . HasNode ( m_fbxPrefabUtility . getFBXObjectName ( nodeNameInUpdatedPrefab ) ) ) {
860
+ // Get the matching name for the node in the m_newFbxData
861
+ string nodeNameInFBX = m_fbxPrefabUtility . getFBXObjectName ( nodeNameInUpdatedPrefab ) ;
862
+
863
+ // The newFbxData contains the fbx name, so we check if the node is in it with the matching equivalent
864
+ if ( ! m_newFbxData . HasNode ( nodeNameInFBX ) ) {
866
865
// It's not in the FBX, so clearly we're not updating any components.
867
866
// We don't need to check if it's in m_prefab because
868
867
// we're only iterating over those.
869
868
continue ;
870
869
}
871
- // Get the matching name for the node in the m_newFbxData
872
- string nodeNameInFBX = m_fbxPrefabUtility . getFBXObjectName ( nodeNameInUpdatedPrefab ) ;
873
870
874
871
var allTypes = m_oldFbxData . GetComponentTypes ( nodeNameInUpdatedPrefab ) . Union (
875
872
m_newFbxData . GetComponentTypes ( nodeNameInFBX ) ) ;
876
-
873
+
874
+ //Compare oldValues that have prefab unity names with new values that have fbx names.
877
875
foreach ( var typename in allTypes ) {
878
876
var oldValues = m_oldFbxData . GetComponentValues ( nodeNameInUpdatedPrefab , typename ) ;
879
877
var newValues = m_newFbxData . GetComponentValues ( nodeNameInFBX , typename ) ;
880
878
List < string > prefabValues = null ; // get them only if we need them.
881
879
882
880
// If we have multiple identical-type components, match them up by index.
883
881
for ( int i = 0 , n = System . Math . Max ( oldValues . Count , newValues . Count ) ; i < n ; ++ i ) {
884
- Log ( oldValues [ i ] ) ;
885
- Log ( newValues [ i ] ) ;
886
882
if ( /* isNew */ i < newValues . Count ) {
887
883
var newValue = newValues [ i ] ;
888
884
@@ -936,8 +932,8 @@ public UpdateList(
936
932
m_fbxPrefabUtility = new FbxPrefabUtility ( m_fbxPrefab ) ;
937
933
938
934
m_oldFbxData = new Data ( oldFbx ) ;
939
- m_newFbxData = new Data ( newFbx , m_fbxPrefabUtility . getFBXObjectName ) ;
940
- m_prefabData = new Data ( prefab . transform , a => a ) ;
935
+ m_newFbxData = new Data ( newFbx ) ;
936
+ m_prefabData = new Data ( prefab . transform ) ;
941
937
942
938
ClassifyDestroyCreateNodes ( ) ;
943
939
ClassifyReparenting ( ) ;
@@ -1055,7 +1051,6 @@ public HashSet<GameObject> ImplementUpdates(FbxPrefab prefabInstance)
1055
1051
// Rename old nodes (unity names) into new nodes (FBX names).
1056
1052
foreach ( var FBXNodeNameToRename in m_nodesToRename )
1057
1053
{
1058
- string test = prefabNodes [ m_fbxPrefabUtility . getUnityObjectName ( FBXNodeNameToRename ) ] . name ;
1059
1054
prefabNodes [ m_fbxPrefabUtility . getUnityObjectName ( FBXNodeNameToRename ) ] . name = FBXNodeNameToRename ;
1060
1055
Log ( "Renamed {0} into {1}" , m_fbxPrefabUtility . getUnityObjectName ( FBXNodeNameToRename ) , FBXNodeNameToRename ) ;
1061
1056
}
@@ -1166,7 +1161,6 @@ void CompareAndUpdate()
1166
1161
if ( ! m_fbxPrefab . FbxModel ) {
1167
1162
return ;
1168
1163
}
1169
- FbxPrefabUtility m_fbxPrefabUtility = new FbxPrefabUtility ( m_fbxPrefab ) ;
1170
1164
1171
1165
// First write down what we want to do.
1172
1166
var updates = new UpdateList ( GetFbxHistory ( ) , m_fbxPrefab . FbxModel . transform , m_fbxPrefab ) ;
@@ -1197,7 +1191,7 @@ void CompareAndUpdate()
1197
1191
FbxPrefab . CallOnUpdate ( fbxPrefabInstance , updatedObjects ) ;
1198
1192
1199
1193
// Update the representation of the history to match the new fbx.
1200
- var newFbxRep = new FbxRepresentation ( m_fbxPrefab . FbxModel . transform , m_fbxPrefabUtility . getFBXObjectName ) ;
1194
+ var newFbxRep = new FbxRepresentation ( m_fbxPrefab . FbxModel . transform ) ;
1201
1195
var newFbxRepString = newFbxRep . ToJson ( ) ;
1202
1196
fbxPrefabInstance . FbxHistory = newFbxRepString ;
1203
1197
@@ -1230,7 +1224,7 @@ public string GetFbxAssetPath()
1230
1224
/// </summary>
1231
1225
public FbxRepresentation GetFbxHistory ( )
1232
1226
{
1233
- return new FbxRepresentation ( m_fbxPrefab . FbxHistory , getFBXObjectName ) ;
1227
+ return new FbxRepresentation ( m_fbxPrefab . FbxHistory ) ;
1234
1228
}
1235
1229
1236
1230
/// <summary>
@@ -1263,7 +1257,6 @@ public void SetSourceModel(GameObject fbxModel) {
1263
1257
}
1264
1258
1265
1259
m_fbxPrefab . FbxModel = fbxModel ;
1266
- FbxPrefabUtility m_fbxPrefabUtility = new FbxPrefabUtility ( m_fbxPrefab ) ;
1267
1260
1268
1261
// Case 0: fbxModel is null and we have no history
1269
1262
// => not normal data flow, but doing nothing is
@@ -1288,7 +1281,7 @@ public void SetSourceModel(GameObject fbxModel) {
1288
1281
// This is the first time we've seen the FBX file. Assume that
1289
1282
// it's the original FBX. Further assume that the user is happy
1290
1283
// with the prefab as it is now, so don't update it to match the FBX.
1291
- m_fbxPrefab . FbxHistory = new FbxRepresentation ( m_fbxPrefab . FbxModel . transform , m_fbxPrefabUtility . getFBXObjectName ) . ToJson ( ) ;
1284
+ m_fbxPrefab . FbxHistory = new FbxRepresentation ( m_fbxPrefab . FbxModel . transform ) . ToJson ( ) ;
1292
1285
} else {
1293
1286
// Case 3.
1294
1287
// User wants to reconnect or change the connection.
0 commit comments