1
+ using UnityEngine ;
2
+ using UnityEditor ;
3
+ using System . Collections . Generic ;
4
+ using FbxExporters ;
5
+ using System . Linq ;
6
+
7
+ public class ManualUpdateEditorWindow : EditorWindow
8
+ {
9
+ int [ ] selectedNodesToDestroy ;
10
+ int [ ] selectedNodesToRename ;
11
+
12
+ FbxPrefabAutoUpdater . FbxPrefabUtility m_fbxPrefabUtility ;
13
+ FbxPrefab m_fbxPrefab ;
14
+ GUIContent [ ] options ;
15
+ List < string > m_nodesToCreate ;
16
+ List < string > m_nodesToDestroy ;
17
+ List < string > m_nodesToRename ;
18
+
19
+ List < string > m_nodeNameToSuggest ;
20
+
21
+ public void Init ( FbxPrefabAutoUpdater . FbxPrefabUtility fbxPrefabUtility , FbxPrefab fbxPrefab )
22
+ {
23
+ FbxPrefabAutoUpdater . FbxPrefabUtility . UpdateList updates = new FbxPrefabAutoUpdater . FbxPrefabUtility . UpdateList ( new FbxPrefabAutoUpdater . FbxPrefabUtility . FbxRepresentation ( fbxPrefab . FbxHistory ) , fbxPrefab . FbxModel . transform , fbxPrefab ) ;
24
+
25
+ m_fbxPrefabUtility = fbxPrefabUtility ;
26
+ m_fbxPrefab = fbxPrefab ;
27
+ // Convert Hashset into List
28
+ m_nodesToCreate = updates . GetNodesToCreate ( ) . ToList ( ) ;
29
+ m_nodesToDestroy = updates . GetNodesToDestroy ( ) . ToList ( ) ;
30
+ m_nodesToRename = updates . GetNodesToRename ( ) . ToList ( ) ;
31
+ // Create the dropdown list
32
+ m_nodeNameToSuggest = new List < string > ( ) ;
33
+ m_nodeNameToSuggest . AddRange ( m_nodesToCreate ) ;
34
+ m_nodeNameToSuggest . AddRange ( m_nodesToRename ) ;
35
+
36
+ // Add extra 1 for the [Delete] option
37
+ selectedNodesToDestroy = new int [ m_nodeNameToSuggest . Count + 1 ] ;
38
+ selectedNodesToRename = new int [ m_nodeNameToSuggest . Count + 1 ] ;
39
+
40
+ // Default option for nodes to rename. Shows the current name mapping
41
+ for ( int i = 0 ; i < m_nodesToRename . Count ; i ++ )
42
+ {
43
+ for ( int j = 0 ; j < m_nodeNameToSuggest . Count ; j ++ )
44
+ {
45
+ if ( m_nodeNameToSuggest [ j ] == m_nodesToRename [ i ] )
46
+ {
47
+ // Add extra 1 for the [Delete] option
48
+ selectedNodesToRename [ i ] = j + 1 ;
49
+ }
50
+ }
51
+ }
52
+ }
53
+
54
+ void OnGUI ( )
55
+ {
56
+ if ( m_nodesToDestroy . Count == 0 && m_nodesToRename . Count == 0 )
57
+ {
58
+ m_fbxPrefabUtility . SyncPrefab ( ) ;
59
+ Close ( ) ;
60
+ }
61
+
62
+ //Titles of the columns
63
+ GUILayout . BeginHorizontal ( ) ;
64
+ GUILayout . Label ( "Unity Names" , EditorStyles . boldLabel ) ;
65
+ GUILayout . Label ( "FBX Names" , EditorStyles . boldLabel ) ;
66
+ GUILayout . EndHorizontal ( ) ;
67
+
68
+ // List of nodes that will be destroyed on the Unity object, unless the user wants to map them
69
+ for ( int i = 0 ; i < m_nodesToDestroy . Count ; i ++ )
70
+ {
71
+ GUILayout . BeginHorizontal ( ) ;
72
+ EditorGUILayout . LabelField ( m_nodesToDestroy [ i ] ) ;
73
+
74
+ List < GUIContent > listFbxNames = new List < GUIContent > ( ) ;
75
+ listFbxNames . Add ( new GUIContent ( "[Delete]" ) ) ;
76
+
77
+ for ( int j = 0 ; j < m_nodeNameToSuggest . Count ; j ++ )
78
+ {
79
+ listFbxNames . Add ( new GUIContent ( m_fbxPrefabUtility . GetFBXObjectName ( m_nodeNameToSuggest [ j ] ) ) ) ;
80
+ }
81
+
82
+ options = listFbxNames . ToArray ( ) ;
83
+ selectedNodesToDestroy [ i ] = EditorGUILayout . Popup ( selectedNodesToDestroy [ i ] , options ) ;
84
+
85
+ GUILayout . EndHorizontal ( ) ;
86
+ }
87
+
88
+ for ( int i = 0 ; i < m_nodesToRename . Count ; i ++ )
89
+ {
90
+ GUILayout . BeginHorizontal ( ) ;
91
+ EditorGUILayout . LabelField ( m_fbxPrefabUtility . GetUnityObjectName ( m_nodesToRename [ i ] ) ) ;
92
+
93
+ List < GUIContent > listFbxNames = new List < GUIContent > ( ) ;
94
+ listFbxNames . Add ( new GUIContent ( "[Delete]" ) ) ;
95
+
96
+ for ( int j = 0 ; j < m_nodeNameToSuggest . Count ; j ++ )
97
+ {
98
+ listFbxNames . Add ( new GUIContent ( m_fbxPrefabUtility . GetFBXObjectName ( m_nodeNameToSuggest [ j ] ) ) ) ;
99
+ }
100
+
101
+ options = listFbxNames . ToArray ( ) ;
102
+
103
+ selectedNodesToRename [ i ] = EditorGUILayout . Popup ( selectedNodesToRename [ i ] , options ) ;
104
+
105
+ GUILayout . EndHorizontal ( ) ;
106
+ }
107
+
108
+ GUILayout . BeginHorizontal ( ) ;
109
+
110
+ if ( GUILayout . Button ( "Apply Changes" ) )
111
+ {
112
+ ApplyChanges ( ) ;
113
+ //Close editor window
114
+ Close ( ) ;
115
+ }
116
+
117
+ if ( GUILayout . Button ( "Cancel" ) )
118
+ {
119
+ //Close editor window
120
+ Close ( ) ;
121
+ }
122
+ GUILayout . EndHorizontal ( ) ;
123
+ }
124
+
125
+ void ApplyChanges ( )
126
+ {
127
+ // Nodes to Destroy have Unity names
128
+ for ( int i = 0 ; i < m_nodesToDestroy . Count ; i ++ )
129
+ {
130
+ // != [Delete]
131
+ if ( selectedNodesToDestroy [ i ] != 0 )
132
+ {
133
+ FbxPrefab . StringPair stringpair = new FbxPrefab . StringPair ( ) ;
134
+ stringpair . FBXObjectName = options [ selectedNodesToDestroy [ i ] ] . text ;
135
+ stringpair . UnityObjectName = m_nodesToDestroy [ i ] ;
136
+
137
+ m_fbxPrefab . NameMapping . Add ( stringpair ) ;
138
+ Debug . Log ( "Mapped Unity: " + stringpair . UnityObjectName + " to FBX: " + stringpair . FBXObjectName ) ;
139
+ }
140
+ }
141
+
142
+ // Nodes to Rename have FBX names
143
+ for ( int i = 0 ; i < m_nodesToRename . Count ; i ++ )
144
+ {
145
+ string currentUnityNodeName = m_fbxPrefabUtility . GetUnityObjectName ( m_nodesToRename [ i ] ) ;
146
+ // == [Delete]
147
+ if ( selectedNodesToRename [ i ] == 0 )
148
+ {
149
+ // Remove previous mapping
150
+ m_fbxPrefabUtility . RemoveMappingUnityObjectName ( currentUnityNodeName ) ;
151
+ }
152
+ else
153
+ {
154
+ if ( currentUnityNodeName != m_fbxPrefabUtility . GetUnityObjectName ( options [ selectedNodesToRename [ i ] ] . text ) )
155
+ {
156
+ m_fbxPrefabUtility . RemoveMappingUnityObjectName ( currentUnityNodeName ) ;
157
+ FbxPrefab . StringPair stringpair = new FbxPrefab . StringPair ( ) ;
158
+ stringpair . FBXObjectName = options [ selectedNodesToRename [ i ] ] . text ;
159
+ stringpair . UnityObjectName = currentUnityNodeName ;
160
+ m_fbxPrefab . NameMapping . Add ( stringpair ) ;
161
+
162
+ Debug . Log ( "Mapped Unity: " + stringpair . UnityObjectName + " to FBX: " + stringpair . FBXObjectName ) ;
163
+ }
164
+ else
165
+ {
166
+ Debug . Log ( "ALREADY Mapped Unity: " + currentUnityNodeName + " to FBX: " + options [ selectedNodesToRename [ i ] ] . text ) ;
167
+ }
168
+ }
169
+ }
170
+
171
+ m_fbxPrefabUtility . SyncPrefab ( ) ;
172
+ }
173
+ }
0 commit comments