Skip to content

Commit dd0a090

Browse files
committed
prevent errors when converting UI and add warning
1 parent 4dfc2c3 commit dd0a090

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,20 @@ protected override void OnEnable()
103103
m_prefabExtLabelWidth = FbxExtLabelStyle.CalcSize(new GUIContent(".prefab")).x;
104104
}
105105

106+
protected bool ExportSetContainsRectTransform(out List<string> roots)
107+
{
108+
roots = new List<string>();
109+
foreach (var obj in GetToExport())
110+
{
111+
var go = ModelExporter.GetGameObject(obj);
112+
if (go.GetComponentInChildren<RectTransform>())
113+
{
114+
roots.Add(go.name);
115+
}
116+
}
117+
return roots.Count > 0;
118+
}
119+
106120
protected bool ExportSetContainsAnimation()
107121
{
108122
foreach (var obj in GetToExport())
@@ -143,6 +157,21 @@ protected override bool Export()
143157
return false;
144158
}
145159

160+
List<string> hierarchiesWithUI;
161+
if (ExportSetContainsRectTransform(out hierarchiesWithUI))
162+
{
163+
// Warn that UI elements will break if converted
164+
string warning = string.Format("UI elements with RectTransform component present in the following hierarchies:\n\n{0}\n\nIf converted, RectTransform and other UI components may be lost",
165+
string.Join("\n", hierarchiesWithUI));
166+
bool result = UnityEditor.EditorUtility.DisplayDialog(
167+
string.Format("{0} Warning", ModelExporter.PACKAGE_UI_NAME), warning, "Continue", "Cancel");
168+
169+
if (!result)
170+
{
171+
return false;
172+
}
173+
}
174+
146175
if (SettingsObject.UseMayaCompatibleNames && SettingsObject.AllowSceneModification)
147176
{
148177
string warning = "Names of objects in the hierarchy may change with the Compatible Naming option turned on";

0 commit comments

Comments
 (0)