Hello!
I’ve been prototyping an RPG in RPG Maker VX Ace for a few months, but I feel I need a more versatile engine, so I’m trying my hand at learning Unity. I was looking into ways to create a database containing stuff (characters, jobs, items etc) akin to the database in RMVX. Someone told me to try using ScriptableObject and then write a custom editor for it. My Database class looks like this:
[System.Serializable]
public class Database : ScriptableObject {
public List<Character> characters;
public List<Job> jobs;
public List<Skill> skills;
}
Character class looks like this, and the other database items look similar:
[System.Serializable]
public class Character {
public string name = "Character";
public int experience = 0;
public Job job;
}
My first question is: can I even have a reference to a Job object inside a Character object? What I want is basically to reference a Job stored in the list of jobs in the database. If this doesn’t work, is using an ID, or maybe even just the index in the list, the best option?
My next question is about going about creating a custom editor and property drawers for my different database items. This is my custom database editor:
[CanEditMultipleObjects]
[CustomEditor(typeof(Database))]
public class DatabaseEditor : Editor {
public override void OnInspectorGUI() {
serializedObject.Update();
DrawGroup("characters");
DrawGroup("jobs");
DrawGroup("skills");
serializedObject.ApplyModifiedProperties();
}
// Draw the full list of items of a category in the database
private void DrawGroup(string groupName) {
SerializedProperty list = serializedObject.FindProperty(groupName);
EditorGUILayout.PropertyField(list);
if (list.isExpanded) {
// Draw array size field
EditorGUILayout.PropertyField(list.FindPropertyRelative("Array.size"));
EditorGUI.indentLevel++;
// Draw all the list elements
for (int i = 0; i < list.arraySize; i++) {
SerializedProperty element = list.GetArrayElementAtIndex(i);
EditorGUILayout.PropertyField(element);
}
EditorGUI.indentLevel--;
}
}
}
I have also written a quick property drawer for Character objects:
[CustomPropertyDrawer(typeof(Character))]
public class CharacterDrawer : PropertyDrawer {
public override void OnGUI (Rect position, SerializedProperty property, GUIContent label) {
property.isExpanded = EditorGUILayout.Foldout(property.isExpanded, label);
if (property.isExpanded) {
EditorGUILayout.PropertyField(property.FindPropertyRelative("name"));
EditorGUILayout.PropertyField(property.FindPropertyRelative("experience"));
// Some way to reference a Job object in the database?
}
}
}
OK, so first off, how do I reference a Job object? The Job objects are stored in the jobs field in the Database. Is there any way a Character object can get a straight reference to a Job object? What type of EditorGUI field do I use for that? I’m also getting line-high gaps in between every element in the list, why is that? Picture:
Thanks in advance for any help!