Skip to content

Commit 1d3e9c4

Browse files
committed
Pull request changes
1 parent 1e0242e commit 1d3e9c4

File tree

3 files changed

+60
-20
lines changed

3 files changed

+60
-20
lines changed

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2471,7 +2471,7 @@ static void OnContextItem (MenuCommand command)
24712471
}
24722472

24732473
/// <summary>
2474-
// Validate the menu item defined by the function above.
2474+
/// Validate the menu item defined by the function above.
24752475
/// </summary>
24762476
[MenuItem (MenuItemName, true, 30)]
24772477
public static bool OnValidateMenuItem ()

Assets/FbxExporters/Editor/FbxPrefabAutoUpdater.cs

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Linq;
77
using System;
88
using FbxExporters.Editor;
9+
using UnityEngine.TestTools;
910

1011
namespace FbxExporters
1112
{
@@ -33,6 +34,7 @@ namespace FbxExporters
3334
#endif
3435

3536
const string MenuItemName = "GameObject/Update from FBX";
37+
public static bool runningUnitTest = false;
3638

3739
public static string FindFbxPrefabAssetPath()
3840
{
@@ -163,7 +165,9 @@ static void OnPostprocessAllAssets(string[] imported, string[] deleted, string[]
163165
}
164166
}
165167
}
166-
168+
/// <summary>
169+
/// Add an option "Update from FBX" in the contextual GameObject menu.
170+
/// </summary>
167171
[MenuItem(MenuItemName, false,31)]
168172
static void OnContextItem(MenuCommand command)
169173
{
@@ -191,42 +195,69 @@ static void OnContextItem(MenuCommand command)
191195
}
192196

193197
/// <summary>
194-
// Validate the menu item defined by the function above.
198+
/// Validate the menu item defined by the function above.
195199
/// </summary>
196200
[MenuItem(MenuItemName, true,31)]
197201
public static bool OnValidateMenuItem()
198202
{
199-
GameObject[] selection = Selection.GetFiltered<GameObject>(SelectionMode.Editable | SelectionMode.TopLevel);
203+
GameObject[] selection = Selection.gameObjects;
200204

201205
if (selection == null || selection.Length == 0)
202206
{
203-
UnityEditor.EditorUtility.DisplayDialog(
204-
string.Format("{0} Warning", ModelExporter.PACKAGE_UI_NAME),
205-
"No GameObjects selected for update.",
206-
"Ok");
207+
string errorMessage = "No GameObjects selected for update.";
208+
if (runningUnitTest)
209+
{
210+
Debug.LogError(errorMessage);
211+
LogAssert.Expect(LogType.Error, errorMessage);
212+
}
213+
else
214+
{
215+
DisplayNoSelectionDialog(errorMessage);
216+
}
207217
return false;
208218
}
209219

220+
bool containsLinkedPrefab = false;
210221
foreach (GameObject selectedObject in selection)
211222
{
212223
GameObject prefab = UnityEditor.PrefabUtility.GetPrefabParent(selectedObject) as GameObject;
213-
if (!prefab)
224+
if (prefab && prefab.GetComponentInChildren<FbxPrefab>())
225+
{
226+
containsLinkedPrefab = true;
227+
break;
228+
}
229+
}
230+
231+
if (!containsLinkedPrefab)
232+
{
233+
string errorMessage = "No linked prefabs selected.";
234+
if (runningUnitTest)
214235
{
215-
UnityEditor.EditorUtility.DisplayDialog(
216-
string.Format("{0} Warning", ModelExporter.PACKAGE_UI_NAME),
217-
"These GameObjects aren't prefab.",
218-
"Ok");
219-
return false;
236+
Debug.LogError(errorMessage);
237+
LogAssert.Expect(LogType.Error, errorMessage);
220238
}
221-
if (prefab.GetComponentInChildren<FbxPrefab>())
239+
else
222240
{
223-
return true;
241+
DisplayNoSelectionDialog(errorMessage);
224242
}
225243
}
226244

227-
return false;
245+
246+
return containsLinkedPrefab;
228247
}
229248

249+
250+
static void DisplayNoSelectionDialog(string message)
251+
{
252+
UnityEditor.EditorUtility.DisplayDialog(
253+
string.Format("{0} Warning", ModelExporter.PACKAGE_UI_NAME),
254+
message,
255+
"Ok");
256+
}
257+
258+
/// <summary>
259+
/// Launch the manual update of the linked prefab specified
260+
/// </summary>
230261
public static void UpdateLinkedPrefab(GameObject prefabInstance)
231262
{
232263
GameObject prefab = UnityEditor.PrefabUtility.GetPrefabParent(prefabInstance) as GameObject;

Assets/FbxExporters/Editor/UnitTests/FbxPrefabAutoUpdaterTest.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,10 @@ public class FbxPrefabAutoUpdaterToggleTest : ExporterTestBase
215215
[SetUp]
216216
public void Init()
217217
{
218+
// Save the initial setting for the auto updater toggle and disable it for the unit test
218219
isAutoUpdaterOn = FbxExporters.EditorTools.ExportSettings.instance.autoUpdaterEnabled;
219220
FbxExporters.EditorTools.ExportSettings.instance.autoUpdaterEnabled = false;
221+
FbxPrefabAutoUpdater.runningUnitTest = true;
220222
}
221223

222224
[Test]
@@ -274,13 +276,13 @@ public void RemappingTest()
274276
Assert.IsTrue(cubePrefabInstance.transform.GetChild(0).name == "SphereFBX");
275277
Assert.IsTrue(cubePrefabInstance.transform.GetChild(0).GetComponent<MeshFilter>().sharedMesh != null);
276278

277-
// Testing Manual update on some random object that isn't a prefab at all
279+
// Testing Manual update on some random object that isn't a prefab at all, the gameobject shouldn't be modified. No error is returned
278280
GameObject quad = GameObject.CreatePrimitive(PrimitiveType.Quad);
279281
FbxPrefabAutoUpdater.UpdateLinkedPrefab(quad);
280282
Assert.IsTrue(quad != null);
281283
Assert.IsTrue(quad.GetComponent<MeshFilter>().sharedMesh != null);
282284

283-
// Testing Manual update on some random prefab that doesn't have an FbxPrefab in it.
285+
// Testing Manual update on some random prefab that doesn't have an FbxPrefab in it, the prefab shouldn't be modified. No error is returned
284286
GameObject capsule = GameObject.CreatePrimitive(PrimitiveType.Capsule);
285287
// Convert to linked prefab instance (auto-updating prefab)
286288
string prefabPath = GetRandomPrefabAssetPath();
@@ -297,19 +299,26 @@ public void RemappingTest()
297299
Selection.objects = new GameObject[] { sphere3Instance };
298300
Assert.IsTrue(FbxPrefabAutoUpdater.OnValidateMenuItem());
299301

300-
// Check the contextual menu returns false because there is no linked prefab
302+
// Check the contextual menu returns false because there is no linked prefab in the selection, an error is returned
301303
GameObject cylinder4 = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
302304
GameObject sphere4 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
303305
sphere4.transform.SetParent(cylinder4.transform);
304306
Selection.objects = new GameObject[] { cylinder4, sphere4 };
305307
Assert.IsFalse(FbxPrefabAutoUpdater.OnValidateMenuItem());
308+
309+
// Assert is true because sphere 3 instance is a linked prefab and all the selection is taken into account
310+
sphere3Instance.transform.SetParent(quad.transform);
311+
Selection.objects = new GameObject[] { quad, sphere3Instance };
312+
Assert.IsTrue(FbxPrefabAutoUpdater.OnValidateMenuItem());
306313
}
307314

308315

309316
[TearDown]
310317
public void stopTest()
311318
{
319+
// Put back the initial setting for the auto-updater toggle
312320
FbxExporters.EditorTools.ExportSettings.instance.autoUpdaterEnabled = isAutoUpdaterOn;
321+
FbxPrefabAutoUpdater.runningUnitTest = false;
313322
}
314323
}
315324
}

0 commit comments

Comments
 (0)