Skip to content

Commit 658635d

Browse files
committed
Updated with changes asked in pull request
1 parent fb11c3b commit 658635d

File tree

2 files changed

+57
-29
lines changed

2 files changed

+57
-29
lines changed

Assets/FbxExporters/Editor/FbxPrefabAutoUpdater.cs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ namespace FbxExporters
2626
/// </summary>
2727
public /*static*/ class FbxPrefabAutoUpdater : UnityEditor.AssetPostprocessor
2828
{
29-
#if UNITY_EDITOR
29+
#if UNITY_EDITOR
3030
public const string FBX_PREFAB_FILE = "/FbxPrefab.cs";
31-
#else
31+
#else
3232
public const string FBX_PREFAB_FILE = "/UnityFbxPrefab.dll";
33-
#endif
33+
#endif
3434

35-
static string[] importedAssets;
35+
const string MenuItemName = "GameObject/Update from Fbx";
3636

3737
public static string FindFbxPrefabAssetPath()
3838
{
@@ -57,8 +57,6 @@ public static bool IsPrefabAsset(string assetPath) {
5757
return assetPath.EndsWith(".prefab");
5858
}
5959

60-
const string MenuItemName = "GameObject/Update from Fbx";
61-
6260
/// <summary>
6361
/// Return false if the prefab definitely does not have an
6462
/// FbxPrefab component that points to one of the Fbx assets
@@ -192,45 +190,47 @@ static void OnContextItem(MenuCommand command)
192190
return;
193191
}
194192

195-
//Selection.objects = UpdateLinkedPrefab(selection);
196-
UpdateLinkedPrefab(selection);
193+
foreach (GameObject selectedObject in selection)
194+
{
195+
UpdateLinkedPrefab(selectedObject);
196+
}
197197
}
198198

199199
/// <summary>
200200
// Validate the menu item defined by the function above.
201201
/// </summary>
202202
[MenuItem(MenuItemName, true,31)]
203-
static bool OnValidateMenuItem()
203+
public static bool OnValidateMenuItem()
204204
{
205205
GameObject[] selection = Selection.GetFiltered<GameObject>(SelectionMode.Editable | SelectionMode.TopLevel);
206-
bool allObjectsPrefab = false;
207206
foreach (GameObject selectedObject in selection)
208207
{
209-
if (selectedObject.GetComponentInChildren<FbxPrefab>())
208+
GameObject prefab = UnityEditor.PrefabUtility.GetPrefabParent(selectedObject) as GameObject;
209+
if (!prefab)
210+
{
211+
return false;
212+
}
213+
if (prefab.GetComponentInChildren<FbxPrefab>())
210214
{
211-
allObjectsPrefab = true;
212-
break;
215+
return true;
213216
}
214217
}
215218

216-
var isPrefab = PrefabUtility.GetPrefabParent(Selection.activeGameObject) != null;
217-
return isPrefab && allObjectsPrefab;
219+
return false;
218220
}
219221

220-
public static void UpdateLinkedPrefab(GameObject[] selection)
222+
public static void UpdateLinkedPrefab(GameObject prefabInstance)
221223
{
222-
var fbxPrefabScriptPath = FindFbxPrefabAssetPath();
223-
foreach (GameObject selectedObject in selection)
224+
GameObject prefab = UnityEditor.PrefabUtility.GetPrefabParent(prefabInstance) as GameObject;
225+
if (!prefab)
224226
{
225-
GameObject prefabInstance = selectedObject;
226-
227-
GameObject prefab = UnityEditor.PrefabUtility.GetPrefabParent(prefabInstance) as GameObject;
227+
return;
228+
}
228229

229-
foreach (var fbxPrefabComponent in prefab.GetComponentsInChildren<FbxPrefab>())
230-
{
231-
var fbxPrefabUtility = new FbxPrefabUtility(fbxPrefabComponent);
232-
fbxPrefabUtility.SyncPrefab();
233-
}
230+
foreach (var fbxPrefabComponent in prefab.GetComponentsInChildren<FbxPrefab>())
231+
{
232+
var fbxPrefabUtility = new FbxPrefabUtility(fbxPrefabComponent);
233+
fbxPrefabUtility.SyncPrefab();
234234
}
235235
}
236236

Assets/FbxExporters/Editor/UnitTests/FbxPrefabAutoUpdaterTest.cs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,15 +266,43 @@ public void RemappingTest()
266266
Assert.IsTrue(cubePrefabInstance.transform.GetChild(0).name == "Sphere");
267267
Assert.IsTrue(cubePrefabInstance.transform.GetChild(0).GetComponent<MeshFilter>().sharedMesh != null);
268268

269-
// Testing Manual update
269+
// Testing Manual update on true prefab
270270
GameObject[] selection = new GameObject[] { cubePrefabInstance };
271-
FbxPrefabAutoUpdater.UpdateLinkedPrefab(selection);
272-
271+
FbxPrefabAutoUpdater.UpdateLinkedPrefab(selection[0]);
273272
Assert.IsTrue(cubePrefabInstance != null);
274273
Assert.IsTrue(cubePrefabInstance.GetComponent<MeshFilter>().sharedMesh != null);
275274
Assert.IsTrue(cubePrefabInstance.transform.GetChild(0).name == "SphereFBX");
276275
Assert.IsTrue(cubePrefabInstance.transform.GetChild(0).GetComponent<MeshFilter>().sharedMesh != null);
277276

277+
// Testing Manual update on some random object that isn't a prefab at all
278+
GameObject quad = GameObject.CreatePrimitive(PrimitiveType.Quad);
279+
FbxPrefabAutoUpdater.UpdateLinkedPrefab(quad);
280+
Assert.IsTrue(quad != null);
281+
Assert.IsTrue(quad.GetComponent<MeshFilter>().sharedMesh != null);
282+
283+
// Testing Manual update on some random prefab that doesn't have an FbxPrefab in it.
284+
GameObject capsule = GameObject.CreatePrimitive(PrimitiveType.Capsule);
285+
// Convert to linked prefab instance (auto-updating prefab)
286+
string prefabPath = GetRandomPrefabAssetPath();
287+
GameObject capsuleInstance = PrefabUtility.CreatePrefab(prefabPath, capsule);
288+
FbxPrefabAutoUpdater.UpdateLinkedPrefab(capsuleInstance);
289+
Assert.IsTrue(capsuleInstance != null);
290+
Assert.IsTrue(capsuleInstance.GetComponent<MeshFilter>().sharedMesh != null);
291+
292+
// Check the menu returns true because sphere3 is a linked prefab
293+
GameObject cylinder3 = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
294+
GameObject sphere3 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
295+
sphere3.transform.SetParent(cylinder3.transform);
296+
GameObject sphere3Instance = ConvertToModel.Convert(sphere3, fbxFullPath: filePath);
297+
Selection.objects = new GameObject[] { sphere3Instance };
298+
Assert.IsTrue(FbxPrefabAutoUpdater.OnValidateMenuItem());
299+
300+
// Check the contextual menu returns false because there is no linked prefab
301+
GameObject cylinder4 = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
302+
GameObject sphere4 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
303+
sphere4.transform.SetParent(cylinder4.transform);
304+
Selection.objects = new GameObject[] { cylinder4, sphere4 };
305+
Assert.IsFalse(FbxPrefabAutoUpdater.OnValidateMenuItem());
278306
}
279307

280308

0 commit comments

Comments
 (0)