- Home /
Serializing list of base and derived classes with custom inspector
Hi everyone!
I have a problem trying to save an array of a list of custom base and derived classes. I have tried to follow the Serialization Megapost but I am still unable to save the data within the array of lists.
GameConfigManager:
public class GameConfigManager : MonoBehaviour
{
public MultidimensionalCategoryGame[] categoryGameList;
public int playsToUnlock;
}
MultidimensionalCategoryGame.cs
[System.Serializable]
public class MultidimensionalCategoryGame : ScriptableObject
{
[SerializeField]
public List<Powerup> array;
public void OnEnable()
{
hideFlags = HideFlags.HideAndDontSave;
if (array == null)
{
array = new List<Powerup>();
}
}
}
CategoryGame.cs
[System.Serializable]
public class CategoryGame : Powerup
{
public int discount;
public string[] instructions;
public override void OnEnable()
{
base.OnEnable();
if (instructions == null)
{
discount = 0;
instructions = new string[0];
}
}
}
Powerup.cs
[System.Serializable]
public class Powerup : ScriptableObject
{
public string name;
public string description;
public float cost;
public virtual void OnEnable()
{
hideFlags = HideFlags.HideAndDontSave;
if (name == null)
{
name = "";
description = "";
cost = 0;
}
}
From my testing, I can save the integer playsToUnlock variable within GameConfigManager. I can also enter data for the MultidimensionalCategoryGame objects in the inspector, but when I hit play my data within MultidimensionalCategoryGame objects are not saved.
Sorry for the long post! Thanks for your time and please help!!!
Answer by cygnusfear · Nov 07, 2012 at 03:22 PM
Not sure if this is going to help you out; but Jacob Pennock wrote a nice post on how to create custom assets.
This will provide you with the means to do offline editing on your powerup and drag that powerup straight into your inspector.
http://www.jacobpennock.com/Blog/?p=670
If this doesn't help out, let me know and I'll dig a bit deeper into your code
Thanks cygnusfear! This post helped me find the solution with some tweaking on the inspector side!!!
For those that had the same problem as I did, I had to reorganize my data structure to avoid editing arrays of lists within the same class. Originally I was trying to edit the Powerups/CategoryGame data in the GameConfig$$anonymous$$anager, but now I save the $$anonymous$$ultidimensionalCategoryGame as an asset file (i followed the link cygnusfear posted above to do so), and then assign each $$anonymous$$ultidimensionalCategoryGame asset into the list in my GameConfig$$anonymous$$anager.
Hope this helps anyone who had the same problem as I did!
Your answer
Follow this Question
Related Questions
Why can't a MonoBehaviour subtype with generic arguments be serialized in a list since Unity 4.5? 0 Answers
Populating list of custom class through inspector 1 Answer
Saving editor-only variables 0 Answers
Dictionary in inspector 4 Answers
Showing properties from a base class in inspector? 0 Answers