Serialization for Custom Inspector - Problems!

Hi everyone!

I’m coming up with a custom inspector that allows to configure modular event dispatching using C# Notification Center as base.

However, the main idea that is making it flexible may be screwing things. What tried to do is making it in way that it works with Templates of events:

using UnityEngine;
using UnityEditor;
using System;
using System.Collections;
using System.Collections.Generic;

[Serializable]
public class EventsSerialization : ScriptableObject
{
	[SerializeField]
	MonoScript eF;
	[SerializeField]
    List<EventForm> eventos;
	
	void OnEnable ()
	{
		if (eventos == null)
            eventos = new List<EventForm>();
	
		hideFlags = HideFlags.HideAndDontSave;
		
	}
	
	public void OnGUI ()

    {
		GUILayout.Label("Add event Template",EditorStyles.boldLabel);
		eF =  EditorGUILayout.ObjectField(eF, typeof(MonoScript),false) as MonoScript;

        foreach (var instance in eventos)

            instance.OnGUI ();

		if(eF)
		{
        if (GUILayout.Button ("Add Event"))
		{
			EventForm eventForm = (EventForm) CreateInstance(eF.GetClass());
			eventos.Add(eventForm);
		}
		}
		
		if(eventos.Count>0)
		{
			if(GUILayout.Button ("Remove Event"))
				eventos.Remove(eventos[eventos.Count-1]);
		}

    }

}

It’s just a matter of attaching the MonoScript object at the inspector field and getting it’s type for adding the intended template to the list. Using this systems it’s even possible to create a list with multiple different events.

And so, all templates Inherit from EventForm Class:

using System;

using System.Collections.Generic;

using UnityEngine;

using UnityEditor;


[Serializable]

public class EventForm : ScriptableObject

{
    public void OnEnable() { hideFlags = HideFlags.HideAndDontSave; }
	
	public virtual void OnGUI ()

    {
	}
}

As this one for example:

using System;

using System.Collections.Generic;

using UnityEngine;

using UnityEditor;

[Serializable]

public class SlideEventForm : EventForm

{

    [SerializeField]
	GameObject target;
	[SerializeField]
	float timeKey;
	[SerializeField]
	string notificationString = "";

 

    public override void OnGUI()

    {

        base.OnGUI ();

        target = EditorGUILayout.ObjectField(target,typeof(GameObject),true) as GameObject;
		timeKey = EditorGUILayout.FloatField("timeKey",timeKey);
		notificationString = EditorGUILayout.TextField("Notification",notificationString);

    }

}

And Finally the MonoBehavior to be added to a scene GameObject:

using UnityEngine;
using UnityEditor;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

public class EventScheduler : MonoBehaviour {
	
	public List<EventForm> eventos;

	void Start () {
	
	}
	
	void Update () {
	
	}
}

And it’s custom editor script:

using UnityEngine;
using UnityEditor;
using System.Collections;
using System;

[CustomEditor(typeof(EventScheduler))]
public class EditorEventScheduler : Editor {
	
	[SerializeField]
	EventsSerialization m_SerialziedThing;
	
	private EventScheduler scheduler;
	
	void Awake()
	{
		scheduler = (EventScheduler) target;
	}
	
	void OnEnable ()

    {

        hideFlags = HideFlags.HideAndDontSave;

        if (m_SerialziedThing == null)

            m_SerialziedThing = CreateInstance<EventsSerialization>();

    }

	public override void OnInspectorGUI()
	{
		GUILayout.Label ("Events Serialization", EditorStyles.boldLabel);

        m_SerialziedThing.OnGUI ();
	}
}

And when I use it, everything goes fine, except when I fire the play button… every setting is erased.

Please, I tried serialize every class and field, but nothing seems to work… even EditorUtility.SetDirty did not work.

Help =[

Bump? =[

re-bump!

Was about to say use EditorUtility.SetDirty… Works just fine for me. How do you use SetDirty? Can you give an example?

Hi, thanks for the reply!
What I do is just add the method at the final line of OnInspectorGUI block:

using UnityEngine;

using UnityEditor;

using System.Collections;

using System;

 

[CustomEditor(typeof(EventScheduler))]

public class EditorEventScheduler : Editor {

    

    [SerializeField]

    EventsSerialization m_SerialziedThing;

    

    private EventScheduler scheduler;

    

    void Awake()

    {

        scheduler = (EventScheduler) target;

    }

    

    void OnEnable ()

 

    {

 

        hideFlags = HideFlags.HideAndDontSave;

 

        if (m_SerialziedThing == null)

 

            m_SerialziedThing = CreateInstance<EventsSerialization>();

 

    }

 

    public override void OnInspectorGUI()

    {

        GUILayout.Label ("Events Serialization", EditorStyles.boldLabel);


        m_SerialziedThing.OnGUI ();
        EditorUtility.SetDirty(target);

    }

}