Custom Editor scripting

Hello dear friends,
i am pretty new to custom editor scripting and i have a simple question which i cant solve. I want to to draw the default UnityEvents property but i dont know how. I can already draw the 2 other fields. On very bottom i have attached an image of how my list looks like right now.

Here are my scripts:

SCRIPT1
[Serializable]
public class ListTemplate
{
    public string templateName;
    public  SOClass so;
    public UnityEvent unityEvent = new UnityEvent(); // i want to draw this into my custom editor
}
------------------------------------------------------------------------------
SCRIPT2
public class EventListener : MonoBehaviour
{
   public List<ListTemplate> list;
}
-------------------------------------------------------------------------------
SCRIPT3
[CustomEditor(typeof(EventListener))]
public class CustomEventListener : Editor
{
    private EventListener targetscript;
    private ReorderableList reorderableList;

    private void OnEnable()
    {
        targetscript = (EventListener)target;
        reorderableList = new ReorderableList(serializedObject, serializedObject.FindProperty("list"), true, true, true, true);

        reorderableList.drawHeaderCallback += DrawHeader;
        reorderableList.drawElementCallback += DrawElement;
        reorderableList.elementHeightCallback += ElementHeightCallback;
        reorderableList.onAddCallback += AddItem;
        reorderableList.onRemoveCallback += RemoveItem;
    }

    private float ElementHeightCallback(int indexer)
    {
float propertyHeight = EditorGUI.GetPropertyHeight(reorderableList.serializedProperty.GetArrayElementAtIndex(indexer), true);
        return propertyHeight + 60;
    }

    public override void OnInspectorGUI()
    {
        reorderableList.DoLayoutList();
    }

    private void DrawHeader(Rect rect)
    {
        GUI.Label(rect, "Eventlist");
    }

    private void DrawElement(Rect rect, int index, bool active, bool focused)
    {
        EditorGUI.BeginChangeCheck();
        ListTemplate item = targetscript.list[index];

//Here we draw our 2 fields
        item.templateName = EditorGUI.TextField(new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight), item.templateName);
        item.so = (SOClass)EditorGUI.ObjectField(new Rect(rect.x, rect.y + 20, rect.width, EditorGUIUtility.singleLineHeight), item.so, typeof(SOClass), true);
        if (EditorGUI.EndChangeCheck())
        {
            EditorUtility.SetDirty(target);
            serializedObject.ApplyModifiedProperties();
        }
    }

    private void AddItem(ReorderableList list)
    {
        targetscript.list.Add(new ListTemplate());
        EditorUtility.SetDirty(target);
    }

    private void RemoveItem(ReorderableList list)
    {
        targetscript.list.RemoveAt(list.index);
        EditorUtility.SetDirty(target);
    }

}

4760696--452444--Unbenannt-2.jpg

Please use code tags: Using code tags properly page-2#post-4740323

Done, sorry totally forgot

You’ll need to use the SerializedObject/SerializedProperty interface to get the default drawers for things.

Meaning that you’ll want to store the result of serializedObject.FindProperty("list"), and use GetArrayElementAtIndex, FindPropertyRelative in DrawElement in order to draw the different fields.

Ok, the problem is my field is not in the targetscript(serializedObject) but in the ListTemplate Class. I cant find a way to create a reference to the ListTemplate class.

prop = serializedObject.FindProperty("unityEvent");

This works and my field is drawn but i have to find a way to do something like this(bellow), because my field is in listTemplate script:

prop = serializedObject.FindProperty(ListTemplate.unityEvent); // i need somethign like this

Thanks.

    SerializedProperty listProp;

    private void OnEnable()
    {
        targetscript = (EventListener)target;
        listProp = serializedObject.FindProperty("list")
        reorderableList = new ReorderableList(serializedObject, listProp, true, true, true, true);
        ...

    private void DrawElement(Rect rect, int index, bool active, bool focused)
    {
        var listElement = listProp.GetArrayElementAtIndex(index);
        var templateNameProp = listElement.FindPropertyRelative("templateName");
        var soProp = listElement.FindPropertyRelative("so");
        var unityEventProp = listElement.FindPropertyRelative("unityEvent");

        // draw props here. 
    }

    // This is neccessary to save changes now:
    public override void OnInspectorGUI()
    {
        reorderableList.DoLayoutList();
        serializedObject.ApplyModifiedProperties();
    }

    //But, you can drop all BeginChangeCheck/SetDirty etc.

Using this and it works good. The unity Event property is drawn succesfully.

 prop = serializedObject.FindProperty("list").GetArrayElementAtIndex(index).FindPropertyRelative("unityEvent");

EditorGUI.PropertyField(new Rect(rect.x, rect.y + 40, rect.width, EditorGUIUtility.singleLineHeight) ,prop);

Now comes my question i need to assign this but i dont know how. Just like i did to the other 2 fields:

item.unityEvent = (UnityEvent)EditorGUI.PropertyField(new Rect(rect.x, rect.y + 40, rect.width, EditorGUIUtility.singleLineHeight) ,prop);

Error says cant convert bool to unityevent.

You don’t need to assign it. The property field assigns the value for you. So it’s just:

EditorGUI.PropertyField(new Rect(rect.x, rect.y + 40, rect.width, EditorGUIUtility.singleLineHeight) ,prop);

Wow thank you so much sir, i appreciate it.