C# / Inspector Serialze Woes

I have recently re-entered into the world of programming after quite the hiatus, now I have dove back in head first and promptly sunk :stuck_out_tongue: So where I am currently running into issue, is in attempting to write a script that will allow me to pair up multiple definitions for levels to a world definition (I am using the word definition loosely).

If you skim over the comments in the code below, you will see what I had in my mind when I began writing the script. I currently am struggling where it comes to defining the array of WorldData as a SerializedField, in reading through the documentation it came to light that the ability doesn’t exist to serialize a standard class. I would like to stray away from having to have my WorldData and LevelData classes as GameObjects as that would create an unnecessary number of GameObjects that shouldn’t be needed, I would also like to avoid using a XML definition file and having to use Resource.Load if at all possible for the Texture2D field within both of my data classes.

So I guess after all of this rambling, I am asking if someone might be able to be able to provide some insight on how I might achieve what I am attempting to preform here. As I am fairly certain that the solution is not that complicated or far away, but I simply have spent to much time staring at the same script. Thank you in advance for anyone able to assist!

/* 
 * Each field editable within Inspector
 * [RELATION CONTAINER]
 * -+ World 1
 * ---+ Level 1
 * ---+ Level 2
 * -+ World 2
 * ---+ Level 1
 * ---+ Level 2
 * ---+ Level 3
 * -+ World 3
 * ---+ Level 1
 * -+ World 4
 * ---+ Level 1
 * ---+ Level 2
 * 
 * Usage pseudocode: Application.LoadLevel(WorldToLevelRelations[1].Level[2].LevelName);
 */

using UnityEngine;

public class WorldLevelRelation : MonoBehaviour
{
    //-------------------------------------------------------------------
    #region Fields

    [SerializeField]
    private WorldData[] m_WorldLevelRelations;

    #endregion


    //-------------------------------------------------------------------
    #region Properties

    public WorldData[] WorldToLevelRelations
    {
        get
        {
            // Return class field
            return this.m_WorldLevelRelations;
        }
    }


    #endregion

    //-------------------------------------------------------------------
    #region Awake class method

    private void Awake()
    {
        // Do not destroy this game object
        DontDestroyOnLoad(this);
    }

    #endregion
}

public class WorldData
{
    //-------------------------------------------------------------------
    #region Fields

    [SerializeField]
    private string m_WorldName = "";
    [SerializeField]
    private Texture2D m_WorldPreview;
    [SerializeField]
    private LevelData[] m_Level;

    #endregion


    //-------------------------------------------------------------------
    #region Properties

    public string WorldName
    {
        get
        {
            // Return class field
            return this.m_WorldName;
        }
    }

    public Texture2D WorldPreview
    {
        get
        {
            // Return class field
            return this.m_WorldPreview;
        }
    }

    public LevelData[] Level
    {
        get
        {
            // Return class field
            return this.m_Level;
        }
    }

    #endregion
}

public class LevelData
{
    //-------------------------------------------------------------------
    #region Fields

    [SerializeField]
    private string m_LevelName = "";
    [SerializeField]
    private Texture2D m_LevelPreview;
    [SerializeField]
    private string m_InternalLevelName = "";

    #endregion


    //-------------------------------------------------------------------
    #region Properties

    public string LevelName
    {
        get
        {
            // Return class field
            return this.m_LevelName;
        }
    }

    public Texture2D LevelPreview
    {
        get
        {
            // Return class field
            return this.m_LevelPreview;
        }
    }

    public string InternalLevelName
    {
        get
        {
            // Return class field
            return this.m_InternalLevelName;
        }
    }

    #endregion
}

Are there any thoughts on how to make this come to life, or am I going to be more close to being tied with using an XML based system?

It sounds like you are trying to use the editor to configure gameobjects but also not utilize the gameObject/Component system (i.e., configure one really complex gameObject with the editor). Put your class on gameObjects and everything will work out fine; stop fighting the component system.

The thing is, and maybe I did a poor way of explaining it in the intial post, is I am trying to use the inspector to be able to manage an array set of data (Strings and Textures). I personally see it silly to have to create individual GameObjects for each definition I want added to the array, where the data set doesn’t use any component features. What I am trying to do could all be done with a set of static classes that, save for a Texture2D reference, use nothing of Unity. I am just trying to find a way to use the power of the inspector to be able to edit this data, instead of another format (XML, SQL, ect.)

I think I have a feel for what you want to do but if you want to keep the data organized in a logical manner and work with it in the inspector - to my mind at least - a gameObject to represent that data set in the inspector (and then instantiated or otherwise loaded at an appropriate time) might be the best route. …even if it’s exactly what you don’t want to do.

but no - I know of no way to expose and work with arrays of classes in the inspector.

If there is one, I’d be interested in knowing it as well.

After looking through this a bit more, and playing with the concept of using GameObjects to house all of the data; I have decided to simply go with the usage of XML and a static class for the time being. I feel that this proves to be far more clean then having a large number of empty GameObjects populating the scene.

I hope at some point to maybe create a custom inspector within the editor to handle the editing of this data, but in the mean time, I feel that XML will suit the project decently. Though, I would still like to hear if anyone is able to come up with anything further, so if you have a thought, please feel free to share.