Performance: ScriptableObjects for game data
This concept affects runtime memory usage and load times by how game data is stored and accessed.
[CreateAssetMenu(fileName = "ItemData", menuName = "Game/ItemData")] public class ItemData : ScriptableObject { public int damage; public float weight; } // Shared asset reused public ItemData swordAsset; public ItemData axeAsset;
public class ItemData { public int damage; public float weight; } // Each instance creates its own copy var sword = new ItemData { damage = 10, weight = 2.5f }; var axe = new ItemData { damage = 15, weight = 3.0f };
| Pattern | Memory Usage | Load Time | Runtime Overhead | Verdict |
|---|---|---|---|---|
| Duplicated class instances | High - each instance duplicates data | Slower - more data to load | High - more allocations | [X] Bad |
| ScriptableObject shared assets | Low - data stored once | Faster - asset reused | Low - references only | [OK] Good |
ScriptableObjects in Unity game development?[CreateAssetMenu] with optional parameters like fileName and menuName, applied to a class inheriting from ScriptableObject.using UnityEngine;
[CreateAssetMenu(fileName = "EnemyData", menuName = "Game/Enemy")]
public class EnemyData : ScriptableObject {
public int health = 100;
}
public class Enemy : MonoBehaviour {
public EnemyData data;
void Start() {
Debug.Log($"Enemy health: {data.health}");
}
}public class PlayerData : ScriptableObject {
public int score;
}
public class Player : MonoBehaviour {
public PlayerData data = new PlayerData();
void Start() {
Debug.Log(data.score);
}
}Pass the test and this pattern moves unknown