unity-scriptableobjects
它能做什么
该技能讲解如何把 Unity 的设置和共享数据保存在项目资源文件中,并在不紧密耦合的情况下连接各个游戏系统。适合希望 Unity 项目结构更清晰的开发者。
适用场景
- ✓当你希望设计师可以把属性或关卡设置编辑为 Unity 资源文件时。
- ✓当你需要替换许多脚本都依赖的单例或管理器时。
- ✓当互不相关的 Unity 系统需要共享同一个数值,或互相发送事件时。
技能链接
https://github.com/gamedev-skills/awesome-gamedev-agent-skills/blob/main/skills/unity/unity-scriptableobjects/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请把我的武器伤害和射速保存到 Unity 资源文件中,让我无需修改代码就能调整它们。”
每周 5 个新技能,1 分钟读完。
工作原理
该技能讲解如何把数据类创建为资源,在脚本中引用它们,并通过共享通道传递事件,使各个系统保持松散连接。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Unity ScriptableObject Architecture
Use ScriptableObject assets to store shared data and decouple systems in Unity 6.3 LTS —
configuration, event channels, and registries that live as project assets instead of being
hard-wired into scenes or singletons. Targets Unity 6.3 LTS (6000.3).
When to use
- Use when you need designer-editable config (weapon stats, level data), to share one value
between unrelated systems, to decouple senders from listeners via event channels, or to
build a runtime registry of active objects — without a
static/singleton manager. - Use when the project has
*.assetdata files backed by: ScriptableObjectclasses.
When not to use: per-instance runtime state that differs per GameObject (that belongs on
a MonoBehaviour) — a ScriptableObject asset is shared by everyone who references it. Saving
player progress to disk → save-systems. Plain DTOs that never need to be an asset can just be
[System.Serializable] classes.
Core workflow
- Define the class deriving from
ScriptableObjectand tag it with[CreateAssetMenu]so designers can create instances from the Assets menu. - Create one or more
.assetinstances in the Project window; each is a shared, named piece of data referenced by[SerializeField]fields. - Reference, don't copy. MonoBehaviours hold a reference to the asset; they all see the same data, so changing the asset changes every consumer.
- For decoupling, model signals and shared variables as ScriptableObjects: a "FloatVariable" the HUD reads and the player writes; an "event channel" the player raises and many systems listen to. Neither side references the other.
- Reset runtime mutations in
OnEnableif the asset is mutated during play, because edits made in the Editor at runtime persist on the asset (a frequent source of "my values changed after I played"). - Verify by inspecting the asset values during Play mode and confirming consumers react.
Patterns
1. Config/data asset
using UnityEngine;
[CreateAssetMenu(fileName = "WeaponData", menuName = "Game/Weapon Data", order = 0)]
public class WeaponData : ScriptableObject
{
public string displayName = "Pistol";
public int damage = 10;
public float fireRate = 0.25f;
public GameObject projectilePrefab;
}
public class Weapon : MonoBehaviour
{
[SerializeField] private WeaponData data; // assign the shared asset in the Inspector
private void Fire() => Debug.Log($"{data.displayName} for {data.damage}");
}
2. Shared runtime variable (decouples producer from consumer)
[CreateAssetMenu(menuName = "Game/Float Variable")]
public class FloatVariable : ScriptableObject
{
[SerializeField] private float initialValue;
[System.NonSerialized] public float runtimeValue; // not saved to the asset
private void OnEnable()
{
runtimeValue = initialValue; // reset when the asset loads
#if UNITY_EDITOR
// With Reload Domain off, OnEnable does not run again on Play, so also reset there.
UnityEditor.EditorApplication.playModeStateChanged -= ResetOnPlay;
UnityEditor.EditorApplication.playModeStateChanged += ResetOnPlay;
#endif
}
#if UNITY_EDITOR
private void OnDisable() => UnityEditor.EditorApplication.playModeStateChanged -= ResetOnPlay;
private void ResetOnPlay(UnityEditor.PlayModeStateChange change)
{
if (change == UnityEditor.PlayModeStateChange.ExitingEditMode) runtimeValue = initialValue;
}
#endif
}
// Player writes playerHealth.runtimeValue; the HUD reads it — neither references the other.
3. Creating an instance at runtime (not an asset on disk)
// For transient SO data you build in code (e.g. a generated config).
var temp = ScriptableObject.CreateInstance<WeaponData>();
temp.damage = 25;
// ...use temp... Destroy(temp); // clean up runtime-created instances
Pitfalls
- Editing an SO at runtime persists in the Editor — values you change during Play stay
changed on the asset after you stop. Keep mutable runtime state in
[NonSerialized]fields reset inOnEnable, or it will surprise you. (In a build, asset edits do not persist across launches.) - Disabled Domain Reload skips your
OnEnablereset — with Enter Play Mode Options enabled and Reload Domain off (a Unity 6.3 LTS fast-iteration setting), already-loaded SOs are
常见问题
ScriptableObject 和普通组件是一回事吗?
不是。ScriptableObject 是共享的项目资源,而组件属于场景中的某一个对象。
我可以把玩家进度保存在其中吗?
不能作为主要存档。将进度保存到磁盘由另一份存档技能负责。
它适用于 Unity 6 吗?
适用。该技能针对 Unity 6.3 LTS。
详情
作者的原始描述
Architect Unity 6.3 LTS data and decoupling with ScriptableObjects: config/data assets, shared runtime variables, event channels, and runtime sets/registries. Use when designing data-driven systems, replacing singletons/managers, creating .asset data with CreateAssetMenu, or when the user mentions ScriptableObject, SO architecture, or data assets.
热度
- GitHub 星标
- 6.6k
- 排名
- #13850
- 许可证
- Apache-2.0
- 更新时间
- 2026/10/28
评论
- …