Skip to content

Commit fb11c3b

Browse files
committed
Added unit test for manual update, improved contextual menu condition, added manual update.
1 parent 09b9e39 commit fb11c3b

File tree

2 files changed

+33
-78
lines changed

2 files changed

+33
-78
lines changed

Assets/FbxExporters/Editor/FbxPrefabAutoUpdater.cs

Lines changed: 23 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,6 @@ static void OnPostprocessAllAssets(string[] imported, string[] deleted, string[]
9090
// Do not start if Auto Updater is disabled in FBX Exporter Settings
9191
if (!FbxExporters.EditorTools.ExportSettings.instance.autoUpdaterEnabled)
9292
{
93-
// Store imported assets to reuse them later
94-
importedAssets = imported;
9593
return;
9694
}
9795

@@ -102,7 +100,10 @@ static void OnPostprocessAllAssets(string[] imported, string[] deleted, string[]
102100
HashSet<string> fbxImported = null;
103101
foreach (var fbxModel in imported) {
104102
if (IsFbxAsset(fbxModel)) {
105-
if (fbxImported == null) { fbxImported = new HashSet<string>(); }
103+
if (fbxImported == null)
104+
{
105+
fbxImported = new HashSet<string>();
106+
}
106107
fbxImported.Add(fbxModel);
107108
//Debug.Log("Tracking fbx asset " + fbxModel);
108109
} else {
@@ -165,16 +166,15 @@ static void OnPostprocessAllAssets(string[] imported, string[] deleted, string[]
165166
}
166167
}
167168

168-
[MenuItem(MenuItemName, false, 30)]
169-
public static void OnContextItem(MenuCommand command)
169+
[MenuItem(MenuItemName, false,31)]
170+
static void OnContextItem(MenuCommand command)
170171
{
171172
GameObject[] selection = null;
172173

173174
if (command == null || command.context == null)
174175
{
175176
// We were actually invoked from the top GameObject menu, so use the selection.
176-
//selection = Selection.GetFiltered<GameObject>(SelectionMode.Unfiltered);
177-
selection = Selection.gameObjects;
177+
selection = Selection.GetFiltered<GameObject>(SelectionMode.Editable | SelectionMode.TopLevel);
178178
}
179179
else
180180
{
@@ -192,98 +192,43 @@ public static void OnContextItem(MenuCommand command)
192192
return;
193193
}
194194

195-
196-
// Did we import an fbx file at all?
197-
// Optimize to not allocate in the common case of 'no'
198-
HashSet<string> fbxImported = null;
199-
if (importedAssets != null)
200-
{
201-
foreach (var fbxModel in importedAssets)
202-
{
203-
if (IsFbxAsset(fbxModel))
204-
{
205-
if (fbxImported == null) { fbxImported = new HashSet<string>(); }
206-
fbxImported.Add(fbxModel);
207-
//Debug.Log("Tracking fbx asset " + fbxModel);
208-
}
209-
else
210-
{
211-
//Debug.Log("Not an fbx asset " + fbxModel);
212-
}
213-
}
214-
}
215-
216-
if (fbxImported != null)
217-
{
218-
//Selection.objects = UpdateLinkedPrefab(selection);
219-
UpdateLinkedPrefab(selection, fbxImported);
220-
}
195+
//Selection.objects = UpdateLinkedPrefab(selection);
196+
UpdateLinkedPrefab(selection);
221197
}
222198

223199
/// <summary>
224200
// Validate the menu item defined by the function above.
225201
/// </summary>
226-
[MenuItem(MenuItemName, true, 30)]
227-
public static bool OnValidateMenuItem()
202+
[MenuItem(MenuItemName, true,31)]
203+
static bool OnValidateMenuItem()
228204
{
229-
//GameObject[] selection = Selection.GetFiltered<GameObject>(SelectionMode.Unfiltered);
230-
GameObject[] selection = Selection.gameObjects;
231-
232-
if (selection == null || selection.Length == 0)
233-
{
234-
ModelExporter.DisplayNoSelectionDialog();
235-
return false;
236-
}
237-
238-
bool allObjectsPrefab = true;
239-
// Check if it's a prefab
205+
GameObject[] selection = Selection.GetFiltered<GameObject>(SelectionMode.Editable | SelectionMode.TopLevel);
206+
bool allObjectsPrefab = false;
240207
foreach (GameObject selectedObject in selection)
241208
{
242-
if (selectedObject.GetComponent<FbxPrefab>() != null || PrefabUtility.FindPrefabRoot(selectedObject).GetComponent<FbxPrefab>() != null)
209+
if (selectedObject.GetComponentInChildren<FbxPrefab>())
243210
{
244211
allObjectsPrefab = true;
245-
}
246-
else
247-
{
248-
allObjectsPrefab = false;
249212
break;
250213
}
251214
}
252215

253-
return allObjectsPrefab;
216+
var isPrefab = PrefabUtility.GetPrefabParent(Selection.activeGameObject) != null;
217+
return isPrefab && allObjectsPrefab;
254218
}
255219

256-
static void UpdateLinkedPrefab(GameObject[] selection, HashSet<string> fbxImported)
220+
public static void UpdateLinkedPrefab(GameObject[] selection)
257221
{
258-
// Iterate over all the prefabs that have an FbxPrefab component that
259-
// points to an FBX file that got (re)-imported.
260-
//
261-
// There's no one-line query to get those, so we search for a much
262-
// larger set and whittle it down, hopefully without needing to
263-
// load the asset into memory if it's not necessary.
264-
//
265-
foreach (GameObject prefab in selection)
222+
var fbxPrefabScriptPath = FindFbxPrefabAssetPath();
223+
foreach (GameObject selectedObject in selection)
266224
{
267-
if (!prefab)
268-
{
269-
//Debug.LogWarning("FbxPrefab reimport: failed to update prefab " + prefabPath);
270-
continue;
271-
}
225+
GameObject prefabInstance = selectedObject;
226+
227+
GameObject prefab = UnityEditor.PrefabUtility.GetPrefabParent(prefabInstance) as GameObject;
228+
272229
foreach (var fbxPrefabComponent in prefab.GetComponentsInChildren<FbxPrefab>())
273230
{
274231
var fbxPrefabUtility = new FbxPrefabUtility(fbxPrefabComponent);
275-
if (!fbxPrefabUtility.WantsAutoUpdate())
276-
{
277-
//Debug.Log("Not auto-updating " + prefabPath);
278-
continue;
279-
}
280-
var fbxAssetPath = fbxPrefabUtility.GetFbxAssetPath();
281-
if (!fbxImported.Contains(fbxAssetPath))
282-
{
283-
//Debug.Log("False-positive dependence: " + prefabPath + " via " + fbxAssetPath);
284-
continue;
285-
}
286-
//Debug.Log("Updating " + prefabPath + "...");
287232
fbxPrefabUtility.SyncPrefab();
288233
}
289234
}

Assets/FbxExporters/Editor/UnitTests/FbxPrefabAutoUpdaterTest.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,16 @@ public void RemappingTest()
265265
Assert.IsTrue(cubePrefabInstance.GetComponent<MeshFilter>().sharedMesh != null);
266266
Assert.IsTrue(cubePrefabInstance.transform.GetChild(0).name == "Sphere");
267267
Assert.IsTrue(cubePrefabInstance.transform.GetChild(0).GetComponent<MeshFilter>().sharedMesh != null);
268+
269+
// Testing Manual update
270+
GameObject[] selection = new GameObject[] { cubePrefabInstance };
271+
FbxPrefabAutoUpdater.UpdateLinkedPrefab(selection);
272+
273+
Assert.IsTrue(cubePrefabInstance != null);
274+
Assert.IsTrue(cubePrefabInstance.GetComponent<MeshFilter>().sharedMesh != null);
275+
Assert.IsTrue(cubePrefabInstance.transform.GetChild(0).name == "SphereFBX");
276+
Assert.IsTrue(cubePrefabInstance.transform.GetChild(0).GetComponent<MeshFilter>().sharedMesh != null);
277+
268278
}
269279

270280

0 commit comments

Comments
 (0)