From 1d498090e391c1495b8de8f8b11e8bda142bd710 Mon Sep 17 00:00:00 2001 From: Soraphis Date: Mon, 24 Oct 2022 00:22:30 +0200 Subject: [PATCH] =?UTF-8?q?prevent=20null=20reference=20exceptions=20in=20?= =?UTF-8?q?editor=20when=20using=20not-serializab=E2=80=A6=20(#371)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * prevent null reference exceptions in editor when using not-serializable types * - Draw warning helpbox only once in AtomVariableEditor.cs - Minor clean up * Revert unintended removed line Co-authored-by: Adam Ramberg --- .../Editor/Drawers/AtomBaseReferenceDrawer.cs | 29 ++++++++++++------- .../Core/Editor/Drawers/VariableDrawer.cs | 12 ++++++-- .../Editors/Variables/AtomVariableEditor.cs | 27 +++++++++++++---- 3 files changed, 50 insertions(+), 18 deletions(-) diff --git a/Packages/Core/Editor/Drawers/AtomBaseReferenceDrawer.cs b/Packages/Core/Editor/Drawers/AtomBaseReferenceDrawer.cs index cdfcfdac..f1b82f5a 100644 --- a/Packages/Core/Editor/Drawers/AtomBaseReferenceDrawer.cs +++ b/Packages/Core/Editor/Drawers/AtomBaseReferenceDrawer.cs @@ -35,7 +35,10 @@ namespace UnityAtoms.Editor } } - return EditorGUI.GetPropertyHeight(property.FindPropertyRelative(usageData.PropertyName), label); + var innerProperty = property.FindPropertyRelative(usageData.PropertyName); + return innerProperty == null ? + EditorGUIUtility.singleLineHeight : + EditorGUI.GetPropertyHeight(innerProperty, label); } public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) @@ -71,20 +74,26 @@ namespace UnityAtoms.Editor var usageTypePropertyName = GetUsages(property)[newUsageValue].PropertyName; var usageTypeProperty = property.FindPropertyRelative(usageTypePropertyName); - var expanded = usageTypeProperty.isExpanded; - usageTypeProperty.isExpanded = true; - var valueFieldHeight = EditorGUI.GetPropertyHeight(usageTypeProperty, label); - usageTypeProperty.isExpanded = expanded; - - if (usageTypePropertyName == "_value" && valueFieldHeight > EditorGUIUtility.singleLineHeight + 2) + if (usageTypeProperty == null) { - EditorGUI.PropertyField(originalPosition, usageTypeProperty, GUIContent.none, true); + EditorGUI.LabelField(position, "[Non serialized value]"); } else { - EditorGUI.PropertyField(position, usageTypeProperty, GUIContent.none); - } + var expanded = usageTypeProperty.isExpanded; + usageTypeProperty.isExpanded = true; + var valueFieldHeight = EditorGUI.GetPropertyHeight(usageTypeProperty, label); + usageTypeProperty.isExpanded = expanded; + if (usageTypePropertyName == "_value" && (valueFieldHeight > EditorGUIUtility.singleLineHeight + 2)) + { + EditorGUI.PropertyField(originalPosition, usageTypeProperty, GUIContent.none, true); + } + else + { + EditorGUI.PropertyField(position, usageTypeProperty, GUIContent.none); + } + } if (EditorGUI.EndChangeCheck()) property.serializedObject.ApplyModifiedProperties(); diff --git a/Packages/Core/Editor/Drawers/VariableDrawer.cs b/Packages/Core/Editor/Drawers/VariableDrawer.cs index ae0259da..e11ddf12 100644 --- a/Packages/Core/Editor/Drawers/VariableDrawer.cs +++ b/Packages/Core/Editor/Drawers/VariableDrawer.cs @@ -19,16 +19,22 @@ namespace UnityAtoms.Editor var inner = new SerializedObject(property.objectReferenceValue); var valueProp = inner.FindProperty("_value"); - var width = GetPreviewSpace(valueProp.type); Rect previewRect = new Rect(position); - previewRect.width = GetPreviewSpace(valueProp.type); + previewRect.width = GetPreviewSpace(valueProp?.type); position.xMin = previewRect.xMax; int indent = EditorGUI.indentLevel; EditorGUI.indentLevel = 0; EditorGUI.BeginDisabledGroup(true); - EditorGUI.PropertyField(previewRect, valueProp, GUIContent.none, false); + if (valueProp != null) + { + EditorGUI.PropertyField(previewRect, valueProp, GUIContent.none, false); + } + else + { + EditorGUI.LabelField(previewRect, "[Non serialized value]"); + } EditorGUI.EndDisabledGroup(); position.x = position.x + 6f; diff --git a/Packages/Core/Editor/Editors/Variables/AtomVariableEditor.cs b/Packages/Core/Editor/Editors/Variables/AtomVariableEditor.cs index 33081176..ea03c53d 100644 --- a/Packages/Core/Editor/Editors/Variables/AtomVariableEditor.cs +++ b/Packages/Core/Editor/Editors/Variables/AtomVariableEditor.cs @@ -13,10 +13,27 @@ namespace UnityAtoms.Editor { private bool _lockedInitialValue = true; private bool _onEnableTriggerSectionVisible = true; + + private void DrawPotentiallyUnserializablePropertyField(SerializedProperty property, bool drawWarningWhenUnserializable = false) + { + if (property == null) + { + if (drawWarningWhenUnserializable) + { + EditorGUILayout.HelpBox("Can't display values because the type is not serializable! You can still use this type, but won't be able to show values in the Editor.", MessageType.Warning); + } + } + else + { + EditorGUILayout.PropertyField(property, true); + } + } + public override void OnInspectorGUI() { serializedObject.Update(); + EditorGUILayout.PropertyField(serializedObject.FindProperty("_developerDescription")); EditorGUILayout.Space(); @@ -24,7 +41,7 @@ namespace UnityAtoms.Editor EditorGUILayout.BeginHorizontal(); EditorGUI.BeginDisabledGroup(_lockedInitialValue && EditorApplication.isPlayingOrWillChangePlaymode); - EditorGUILayout.PropertyField(serializedObject.FindProperty("_initialValue"), true); + DrawPotentiallyUnserializablePropertyField(serializedObject.FindProperty("_initialValue"), drawWarningWhenUnserializable: true); EditorGUI.EndDisabledGroup(); if (EditorApplication.isPlaying) { @@ -36,7 +53,7 @@ namespace UnityAtoms.Editor using (new EditorGUI.DisabledGroupScope(!EditorApplication.isPlaying)) { EditorGUI.BeginChangeCheck(); - EditorGUILayout.PropertyField(serializedObject.FindProperty("_value"), true); + DrawPotentiallyUnserializablePropertyField(serializedObject.FindProperty("_value")); if (EditorGUI.EndChangeCheck() && target is AtomBaseVariable atomTarget) { try @@ -48,8 +65,8 @@ namespace UnityAtoms.Editor { atomTarget.BaseValue = (double)(float)value; } - //Ulong is deserialized to System32 Int. - else if(typeof(T) == typeof(ulong)) + //Ulong is deserialized to System32 Int. + else if (typeof(T) == typeof(ulong)) { atomTarget.BaseValue = (ulong)(int)value; } @@ -71,7 +88,7 @@ namespace UnityAtoms.Editor EditorGUI.BeginDisabledGroup(true); - EditorGUILayout.PropertyField(serializedObject.FindProperty("_oldValue"), true); + DrawPotentiallyUnserializablePropertyField(serializedObject.FindProperty("_oldValue")); EditorGUI.EndDisabledGroup(); const int raiseButtonWidth = 52;