- Home /
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.
Answer by Brian-Kehrer · Dec 19, 2010 at 03:46 AM
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?
You can create the ScriptableObject class with a bunch of default values, then use an editor script to create instances of it all over the place. The instances can be edited individually.
To create an instance of a scriptable object from an editor script you do something like this:
/* create multiple GunData objects named Gun1, Gun2, etc... */
for ( var i = 0; i < 10; i ++ ) {
var somePath : String = "Assets/Guns/Gun" + i + ".asset";
var freshInstance = ScriptableObject.CreateInstance(GunData);
AssetDatabase.CreateAsset(freshInstance, somePath);
}
Here's how you might define a ScriptableObject with default values in javascript:
class GunData extends ScriptableObject
{
var name : String = "Bop Gun";
var power : float = 20.4f;
}
Thanks to the folks at Schell Games for an awesome presentation on ScriptableObjects at Unite 2011!
Your answer
Follow this Question
Related Questions
Extending "GameObject/Create Other" with own prefab 1 Answer
Saving its values through custom Editor 2 Answers
CustomEditor for TextureImporter? 2 Answers
Why my prefab is auto changing? 3 Answers
Color custom editor window ? 2 Answers