Custom Data structs & Inspector

Greetings Unity Community,

I am trying to do something a little advanced with my game, basically I am making a shooter which has 50+ permutations of guns. And my goal is to allow the designer to tweak all kinds of variables involving a gun. So my instict was to create a GunData class which represents all the fields for a gun.

[System.Serializable]
public class GunData : Object
{
    public string Name = "Unnamed";
    public float Force = 10.0f;
    public float FireRate = 0.05f;
    public float Damage = 5.0f;
    public float ReloadTime = 0.5f;
    public float Recoil = 1.0f;
    public float Accuracy = 1.0f;
    public float Range = 100.0f;
    public int BulletsPerClip = 40;
    public int Clips = 20;
    public int GunID;
    public Transform Mesh;
    private GunType _type;
//...
}

And so, this poses a problem as I need to know what all 50 guns will be as a player can have any two from a long array. So to make this data driven, I added a GunEditor to inspect these.... Which uses reflection and creates a field.

[CustomEditor(typeof(GunData))] public class GunEditor : Editor {

public void OnInspectorGUI() 
{
    Type targetType = target.GetType();
    FieldInfo[] targetFields = targetType.GetFields(BindingFlags.Public | BindingFlags.Instance);

    foreach (FieldInfo field in targetFields)
    {
        EditorGUILayout.BeginHorizontal(); 
        EditorGUILayout.PrefixLabel(field.Name);

        field.SetValue(target, field.GetValue(target));

        EditorGUILayout.EndHorizontal(); 
    }
}

}

So this brings me to the real question... In my hiearchy I have a gameobject called Assets, which has a script GameData representing a singleton of all the objects that will be dynamically shifted around the scene alot. This includes a way for me to look up info in other scripts and to centralize it.

public class GameData : MonoBehaviour 
{
    public static GameData Instance = null;
    [SerializeField] public GunData[] GunData;
    // ...
}

My question is, I can see the array of Gun Data in the inspector when viewing my Assets node (GameData script component) but I can't find an easy way of figuring out how to populate the array with premade GunData's.

Anyone have suggestions ? Basically I want a pre-runtime array of gundata accessible through a GameData singleton, so that when a player does something like PickUp weapon it knows which prefab to instantiate among other things.

Can't you just go to your array in the inspector, increase it's size and set up your GunData? Or do you mean reading the data from a file? Of course your CustomEditor will only kick in if the GunData is inspected on it's own. I also wonder why you need a custom inspector, as the default unity inspector should be totally sufficient for what you have.

I believe increasing the size of the array in the inspector only allows you to edit the individual items if they are structs, not classes. If they're classes it just allows you to drag and drop a pre-existing instance into the slot.

1 Answer

1

For this, you should probably be using ScriptableObjects. They are assets that live in your project.

How do you create an instance of a ScriptableObject? Do I have to write completely custom code to create an instance and save it to disk?