Asset
Asset 是 Unity 项目中的任意资源——贴图、模型、音频、脚本、材质等,存放在 Assets/ 下,通过唯一 GUID 引用而非脆弱的文件路径。.meta 与 Import Settings 直接影响包体大小与加载时间;大型项目常配合 Addressables 管理依赖、分包与远程分发策略。
想象一下...
Asset 就像厨房的食材库。开灶(运行游戏)前,厨师(Unity Editor)先备好食材——蔬菜(贴图)、香料(材质)、菜谱(脚本)、背景音乐(音频)。一切都在 Assets 食材库里。你不直接“生吃”文件——而是引用它们;改了源食材,用到它的每一道菜都会更新。
概念详解
Asset 是项目 Assets/ 文件夹下的任意文件。
导入时,Unity 会创建带 GUID(全局唯一 id)的 .meta 文件——GameObject 和脚本
靠它引用 Asset,而不依赖脆弱的文件路径。
Asset Database(AssetDatabase)
在 Editor 中管理 Asset。文件变更时 Unity 会重新导入。运行时可用 Resources.Load() 或
Addressables 动态加载。
常见 Asset 类型: .png/.jpg
(Texture2D)、.fbx/.obj(Mesh)、.mp3/.wav(AudioClip)、
.cs(MonoScript)、.prefab(Prefab)、.mat(Material)、
.asset(ScriptableObject)。
Import Settings:每类 Asset 都有各自的旋钮—— Texture Compression / Max Size;Audio Load Type(Decompress on Load / Streaming)。正确 设置能大幅降低内存占用和加载时间。
示意图:Asset 分类
Textures
.png .jpg .tga .psd
Texture2D · RenderTexture
Audio
.mp3 .wav .ogg
AudioClip
Models
.fbx .obj .blend
Mesh · Avatar
Scripts
.cs
MonoScript
Materials
.mat .shader
Material · Shader
Data Assets
.prefab .asset .anim
Prefab · SO · AnimClip
动手步骤
把 Asset 导入项目
从资源管理器/Finder 拖文件到 Project 窗口,或 Assets → Import New Asset…
配置 Import Settings
选中 Asset → Inspector → 调整 Compression、Max Size、Load Type… → Apply。
用 SerializeField 引用
把 Project 中的 Asset 拖到 Inspector 里的 [SerializeField] 字段。
用 Addressables 按需加载
把 Asset 标为 Addressable,需要时调用 Addressables.LoadAssetAsync<T>(key)。
保持文件夹整洁
使用清晰路径:Assets/Art/Textures/、Assets/Scripts/… 避免把文件堆在 Assets 根目录。
交互模拟器
按 Import 添加 Asset。点击查看属性。赋给 SerializeField,通过 GUID 引用。
📁 Assets/
还没有 Asset — 点击 "Import Asset"
Inspector
选择一个 Asset
SerializeField
把 Asset 拖到这里...
—
GUID: —
代码示例
基础通过 SerializeField 引用 Asset,并在运行时使用。
using UnityEngine;
public class WeaponController : MonoBehaviour
{
// Drag Assets from Project onto these fields in the Inspector
[SerializeField] private Sprite weaponIcon;
[SerializeField] private AudioClip shootSound;
[SerializeField] private GameObject bulletPrefab;
[SerializeField] private Material glowMaterial;
void Start()
{
// Use the assigned Asset — no runtime search needed
GetComponent<SpriteRenderer>().sprite = weaponIcon;
}
public void Shoot()
{
Instantiate(bulletPrefab, transform.position, transform.rotation);
AudioSource.PlayClipAtPoint(shootSound, transform.position);
}
}代码示例
进阶用 Resources.Load 在运行时加载并缓存 Asset。
using UnityEngine;
using System.Collections.Generic;
// Runtime asset manager — load by key, cache for reuse
public class AssetCache : MonoBehaviour
{
private static Dictionary<string, Object> _cache = new();
public static T Load<T>(string path) where T : Object
{
if (_cache.TryGetValue(path, out var cached))
return cached as T;
// Resources.Load looks under Assets/Resources/
T asset = Resources.Load<T>(path);
if (asset != null)
_cache[path] = asset;
else
Debug.LogWarning($"Asset not found: {path}");
return asset;
}
public static void Unload(string path)
{
if (_cache.TryGetValue(path, out var asset))
{
Resources.UnloadAsset(asset);
_cache.Remove(path);
}
}
}
// Usage:
// var tex = AssetCache.Load<Texture2D>("Textures/hero_sprite");📌 快速记忆
- ▸每个 Asset 的
.meta里有 GUID——切勿随意删除 - ▸
SerializeField= 静态引用;Resources.Load= 动态加载 - ▸Import Settings(Compression、Max Size)直接影响构建体积
- ▸大型项目优先用 Addressables,而不是 Resources.Load
⚠️ 常见错误
❌ 删除或忽略 .meta 文件
GUID 变化 → Scene/Prefab 引用变成 Missing
✅ 始终把 .meta 与 Asset 一起提交到 Git
❌ 把所有 Asset 都丢进 Resources/
Resources 下的一切都会打进包 → 体积膨胀
✅ 只放必须动态加载的;其余优先 SerializeField