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.