prevent null reference exceptions in editor when using not-serializab… (#371)

* 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 <adam@mambojambostudios.com>
This commit is contained in:
Soraphis 2022-10-24 00:22:30 +02:00 committed by GitHub
parent 9cf1b60a06
commit 1d498090e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 18 deletions

View File

@ -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();

View File

@ -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;

View File

@ -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;