r/UnityHelp • • 18d ago

UNITY I have a question

/r/unity/comments/1xpeknf/i_have_a_question/
1 Upvotes

1 comment sorted by

1

u/LargeLaser 18d ago

use scriptable objects, they're very easy to use
just make one class per data type like this:

[CreateAssetMenu(fileName = "WeaponConfig", menuName = "Data/WeaponConfig", order = 1)]
public class WeaponConfig: ScriptableObject
{
    public string displayName;
    public float damage;
}

create your data objects in say Resources/Data/Weapons
right click in the project panel and tap Data/WeaponConfig, name it something gamey
then load them all on game startup, something like this:

Dictionary<string, WeaponConfig> weaponConfigs;
..
Load(out weaponConfigs, "Data/Weapons");
..

void Load<T>(out Dictionary<string, T> dest, string path) where T : ScriptableObject
{
    var configs = Resources.LoadAll<T>(path);
    dest = new Dictionary<string, T>();
    foreach (var config in configs)
    {
        dest[config.name] = config;
    }
}

then do something like

var weapon = weaponConfigs["sword"];

or whatever the object is called
good luck!