Hey I’m a beginner with programming and game development and I just started using the Unity Editor for making tools to make things easier for me when creating some basic things. I’ve been messing with creating a tool to help me create information for npcs. I’m using Three scripts in total for this. One for the Unity Editor window, the second for defining the information my npcs can have, and the third for storing all of the information I make on a list. Ill link all the code for these in that order so you can see how i’m doing this.
So now that I’ve gotten all that out of the way my question for this is if this is a good way of storing information for Game Objects or would it be better for me to hard code all the information into a script for me to reference later on?
Script for my Unity Editor
using UnityEngine;
using UnityEditor;
using System.Collections;
public class NpcEditor : EditorWindow
{
NPCDatabase database;
string charName;
int id;
int health;
int stam;
int mana;
Character.NPCType type;
private void Update()
{
database = GameObject.FindGameObjectWithTag("NPC Database").GetComponent<NPCDatabase>();
}
[MenuItem("Window/NPC Editor")]
public static void ShowWindow()
{
GetWindow<NpcEditor>("NPC Editor");
}
void OnGUI()
{
GUILayout.Label("Npc Editor", EditorStyles.boldLabel);
charName = EditorGUILayout.TextField("Name", charName);
id = EditorGUILayout.IntField("id", id);
health = EditorGUILayout.IntField("Health", health);
stam = EditorGUILayout.IntField("Stamina", stam);
mana = EditorGUILayout.IntField("Mana", mana);
type = (Character.NPCType)EditorGUILayout.EnumPopup("Type", type);
if (GUILayout.Button("Create NPC"))
{
database.AddNPC(charName, id, health, stam, mana, type);
}
}
}
Script for defining my npcs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Character
{
[Header("Details")]
public string characterName;
public int characterID;
public NPCType npcType;
[Header("Statistics")]
public int characterHealth;
public int characterStamina;
public int characterMana;
public enum NPCType { Aggressive, Neutral, Friendly }
public Character(string name, int id, int health, int stam, int mana, NPCType type)
{
characterName = name;
characterID = id;
characterHealth = health;
characterStamina = stam;
characterMana = mana;
npcType = type;
}
}
script for storing the information
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[DisallowMultipleComponent]
public class NPCDatabase : MonoBehaviour {
public List<Character> npc = new List<Character>();
public void AddNPC(string name, int id, int health, int stam, int mana, Character.NPCType type)
{
npc.Add(new Character(name, id, health, stam, mana, type));
}
}
Heres some picture of what this looks like in Unity