Question by
Yanfundi · May 08 at 08:06 PM ·
custom editorproperty drawer
Property Drawer SerializedProperty Null inside Array
I'm new to this PropertyDrawer
stuff. I have created PropertyDrawer
for my class AudioData
and it works fine. But when I create variable that is an array/list of AudioData
and click the plus button, it throws a NullException
to my property.FindPropertyRelative
.
How do I fix this? Thanks.
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
[CustomPropertyDrawer(typeof(AudioData), false)]
public class AudioDataPropertyDrawer : PropertyDrawer
{
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return property.isExpanded ? (EditorGUIUtility.singleLineHeight * 5) : EditorGUIUtility.singleLineHeight;
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position, label, property);
if (property != null)
{
string foldOutName = string.IsNullOrEmpty(property.FindPropertyRelative("AudioName").stringValue) ? "Audio Data" : property.FindPropertyRelative("AudioName").stringValue;
Rect foldOutRect = new Rect(position.x, position.y, position.width, 16);
property.isExpanded = EditorGUI.Foldout(foldOutRect, property.isExpanded, foldOutName, true);
EditorGUI.indentLevel++;
if (property.isExpanded)
{
Rect nameRect = new Rect(position.x, position.y + 18, position.width, 16);
EditorGUI.PropertyField(nameRect, property.FindPropertyRelative("AudioName"));
Rect clipRect = new Rect(position.x, position.y + 36, position.width, 16);
EditorGUI.PropertyField(clipRect, property.FindPropertyRelative("SoundClip"));
Rect isRandVolRect = new Rect(position.x, position.y + 54, position.width, 16);
EditorGUI.PropertyField(isRandVolRect, property.FindPropertyRelative("IsRandomizeVolume"));
Rect volumeRect = new Rect(position.x, position.y + 72, position.width, 16);
if (property.FindPropertyRelative("IsRandomizeVolume").boolValue)
EditorGUI.PropertyField(volumeRect, property.FindPropertyRelative("VolumeRange"));
else
{
Vector2 volX = property.FindPropertyRelative("VolumeRange").vector2Value;
volX.x = EditorGUI.Slider(volumeRect, "Volume", property.FindPropertyRelative("VolumeRange").vector2Value.x, 0, 1);
property.FindPropertyRelative("VolumeRange").vector2Value = volX;
}
}
EditorGUI.indentLevel--;
}
EditorGUI.EndProperty();
}
}
#endif
public enum SoundTypeEnum
{
SFX,
BGM,
}
[System.Serializable]
public class AudioData
{
public string AudioName = "";
public AudioClip SoundClip = null;
public bool IsRandomizeVolume = false;
public Vector2 VolumeRange = Vector2.one;
public virtual AudioClip GetSoundClip(object[] obj = null)
{
return SoundClip;
}
public virtual float GetVolume(object[] obj = null)
{
return IsRandomizeVolume ? Random.Range(VolumeRange.x, VolumeRange.y) : VolumeRange.x;
}
public AudioData()
{
AudioName = "";
SoundClip = null;
IsRandomizeVolume = false;
VolumeRange = Vector2.one;
}
public AudioData(string name = "", AudioClip clip = null, bool isRandomizeVol = false, Vector2? vol = null)
{
AudioName = name;
SoundClip = clip;
IsRandomizeVolume = isRandomizeVol;
VolumeRange = vol ?? Vector2.one;
}
public AudioData(AudioClip clip = null, bool isRandomizeVol = false, Vector2? vol = null)
{
AudioName = clip.name;
SoundClip = clip;
IsRandomizeVolume = isRandomizeVol;
VolumeRange = vol ?? Vector2.one;
}
}
This is what happened in the Editor (Attached GIF):
The error:
NullReferenceException: Object reference not set to an instance of an object
AudioDataPropertyDrawer.OnGUI (UnityEngine.Rect position, UnityEditor.SerializedProperty property, UnityEngine.GUIContent label) (at Assets/_Project/Scripts/Managers/Audio Manager/AudioData.cs:19)
UnityEditor.PropertyDrawer.OnGUISafe (UnityEngine.Rect position, UnityEditor.SerializedProperty property, UnityEngine.GUIContent label) (at <d0e4b8c1204147f3935b6e9c8b8565ad>:0)
UnityEditor.PropertyHandler.OnGUI (UnityEngine.Rect position, UnityEditor.SerializedProperty property, UnityEngine.GUIContent label, System.Boolean includeChildren, UnityEngine.Rect visibleArea) (at <d0e4b8c1204147f3935b6e9c8b8565ad>:0)
UnityEditor.PropertyHandler.OnGUI (UnityEngine.Rect position, UnityEditor.SerializedProperty property, UnityEngine.GUIContent label, System.Boolean includeChildren) (at <d0e4b8c1204147f3935b6e9c8b8565ad>:0)
UnityEditorInternal.ReorderableList+Defaults.DrawElement (UnityEngine.Rect rect, UnityEditor.SerializedProperty element, System.Object listItem, System.Boolean selected, System.Boolean focused, System.Boolean draggable, System.Boolean editable) (at <d0e4b8c1204147f3935b6e9c8b8565ad>:0)
UnityEditorInternal.ReorderableList.DoListElements (UnityEngine.Rect listRect, UnityEngine.Rect visibleRect) (at <d0e4b8c1204147f3935b6e9c8b8565ad>:0)
UnityEditorInternal.ReorderableList.DoList (UnityEngine.Rect rect, UnityEngine.Rect visibleRect) (at <d0e4b8c1204147f3935b6e9c8b8565ad>:0)
UnityEditorInternal.ReorderableListWrapper.Draw (UnityEngine.GUIContent label, UnityEngine.Rect r, UnityEngine.Rect visibleArea, System.Boolean includeChildren) (at <d0e4b8c1204147f3935b6e9c8b8565ad>:0)
UnityEditor.PropertyHandler.OnGUI (UnityEngine.Rect position, UnityEditor.SerializedProperty property, UnityEngine.GUIContent label, System.Boolean includeChildren, UnityEngine.Rect visibleArea) (at <d0e4b8c1204147f3935b6e9c8b8565ad>:0)
UnityEditor.PropertyHandler.OnGUI (UnityEngine.Rect position, UnityEditor.SerializedProperty property, UnityEngine.GUIContent label, System.Boolean includeChildren) (at <d0e4b8c1204147f3935b6e9c8b8565ad>:0)
UnityEditor.PropertyHandler.OnGUILayout (UnityEditor.SerializedProperty property, UnityEngine.GUIContent label, System.Boolean includeChildren, UnityEngine.GUILayoutOption[] options) (at <d0e4b8c1204147f3935b6e9c8b8565ad>:0)
UnityEditor.EditorGUILayout.PropertyField (UnityEditor.SerializedProperty property, UnityEngine.GUIContent label, System.Boolean includeChildren, UnityEngine.GUILayoutOption[] options) (at <d0e4b8c1204147f3935b6e9c8b8565ad>:0)
UnityEditor.EditorGUILayout.PropertyField (UnityEditor.SerializedProperty property, System.Boolean includeChildren, UnityEngine.GUILayoutOption[] options) (at <d0e4b8c1204147f3935b6e9c8b8565ad>:0)
UnityEditor.Editor.DoDrawDefaultInspector (UnityEditor.SerializedObject obj) (at <d0e4b8c1204147f3935b6e9c8b8565ad>:0)
UnityEditor.Editor.DoDrawDefaultInspector () (at <d0e4b8c1204147f3935b6e9c8b8565ad>:0)
UnityEditor.Editor.DrawDefaultInspector () (at <d0e4b8c1204147f3935b6e9c8b8565ad>:0)
UnityEditor.Editor.OnInspectorGUI () (at <d0e4b8c1204147f3935b6e9c8b8565ad>:0)
UnityEditor.UIElements.InspectorElement+<>c__DisplayClass59_0.<CreateIMGUIInspectorFromEditor>b__0 () (at <17cba49fae414354b79f447a7073fc5b>:0)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&)
audiodata-propertydrawer.gif
(64.9 kB)
Comment