Skip to content

Commit 611b232

Browse files
authored
Merge pull request #473 from Unity-Technologies/UT-2049-fix-error-converting-UI
UT-2049 prevent errors when converting UI and add warning
2 parents 4dfc2c3 + 68858dd commit 611b232

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

com.unity.formats.fbx/Editor/ConvertToNestedPrefab.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -840,9 +840,8 @@ internal static void CopySerializedProperty(SerializedObject serializedObject, S
840840
}
841841

842842
/// <summary>
843-
/// Copy components on the 'from' object which is the FBX,
844-
/// over to the 'to' object which is the object in the
845-
/// scene we exported.
843+
/// Copy components on the 'from' object which is the object being converted,
844+
/// over to the 'to' object which is the FBX.
846845
///
847846
/// Copy over everything except meshes and materials, since these
848847
/// are already in the FBX.
@@ -872,7 +871,10 @@ internal static void CopyComponents(GameObject to, GameObject from, GameObject r
872871
}
873872

874873
// ignore FbxPrefab (when converting LinkedPrefabs)
875-
if (fromComponent is UnityEngine.Formats.Fbx.Exporter.FbxPrefab)
874+
// Also ignore RectTransform, since it is not currently possible to switch transforms
875+
// in a prefab.
876+
if (fromComponent is UnityEngine.Formats.Fbx.Exporter.FbxPrefab ||
877+
fromComponent is RectTransform)
876878
{
877879
continue;
878880
}
@@ -920,6 +922,13 @@ internal static void CopyComponents(GameObject to, GameObject from, GameObject r
920922
toComponent = to.AddComponent(fromComponent.GetType());
921923
}
922924

925+
if (!toComponent)
926+
{
927+
// Failed to add component
928+
Debug.LogWarningFormat("{0}: Failed to add component of type {1} to converted object", ModelExporter.PACKAGE_UI_NAME, fromComponent.GetType().Name);
929+
continue;
930+
}
931+
923932
FixSceneReferences(fromComponent, toComponent, root);
924933

925934
// Do not try to copy materials for ParticleSystemRenderer, since it is not in the

com.unity.formats.fbx/Editor/ConvertToPrefabEditorWindow.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,26 @@ protected override void OnEnable()
103103
m_prefabExtLabelWidth = FbxExtLabelStyle.CalcSize(new GUIContent(".prefab")).x;
104104
}
105105

106+
/// <summary>
107+
/// Get a list of all the export set objects that contain
108+
/// RectTransforms or have children with RectTransforms.
109+
/// </summary>
110+
/// <param name="uiObjectNames">names of objects in set which contain RectTransforms</param>
111+
/// <returns>Whethere there are any UI elements in the export set</returns>
112+
protected bool GetUIElementsInExportSet(out List<string> uiObjectNames)
113+
{
114+
uiObjectNames = new List<string>();
115+
foreach (var obj in GetToExport())
116+
{
117+
var go = ModelExporter.GetGameObject(obj);
118+
if (go.GetComponentInChildren<RectTransform>())
119+
{
120+
uiObjectNames.Add(go.name);
121+
}
122+
}
123+
return uiObjectNames.Count > 0;
124+
}
125+
106126
protected bool ExportSetContainsAnimation()
107127
{
108128
foreach (var obj in GetToExport())
@@ -143,6 +163,21 @@ protected override bool Export()
143163
return false;
144164
}
145165

166+
List<string> hierarchiesWithUI;
167+
if (GetUIElementsInExportSet(out hierarchiesWithUI))
168+
{
169+
// Warn that UI elements will break if converted
170+
string warning = string.Format("RectTransform and other UI components will be lost if the following GameObject hierarchies are converted:\n\n{0}\n",
171+
string.Join("\n", hierarchiesWithUI));
172+
bool result = UnityEditor.EditorUtility.DisplayDialog(
173+
string.Format("{0} Warning", ModelExporter.PACKAGE_UI_NAME), warning, "Convert and lose UI", "Cancel");
174+
175+
if (!result)
176+
{
177+
return false;
178+
}
179+
}
180+
146181
if (SettingsObject.UseMayaCompatibleNames && SettingsObject.AllowSceneModification)
147182
{
148183
string warning = "Names of objects in the hierarchy may change with the Compatible Naming option turned on";

0 commit comments

Comments
 (0)