Unity Term Book
Core & Architecture

Asset

Asset là mọi tài nguyên trong dự án Unity — texture, model, âm thanh, script, material — nằm trong Assets/ và được tham chiếu bằng GUID, không phải đường dẫn.

Hãy tưởng tượng...

Asset giống như kho nguyên liệu của nhà bếp. Trước khi nấu (chạy game), đầu bếp (Unity Editor) cần tập hợp đủ nguyên liệu — rau củ (textures), gia vị (materials), công thức (scripts), âm nhạc nền (audio clips). Tất cả được xếp gọn trong kho Assets. Bạn không nấu từ nguyên liệu thô — bạn tham chiếu đến chúng; thay đổi nguyên liệu gốc sẽ ảnh hưởng toàn bộ món ăn đang dùng nó.

Khái niệm chi tiết

Asset là bất kỳ file nào trong thư mục Assets/ của project Unity. Khi import, Unity tạo ra file .meta chứa GUID (định danh duy nhất toàn cầu) — đây là cầu nối để các GameObject và script tham chiếu Asset mà không phụ thuộc vào đường dẫn file cụ thể.

Asset Database (AssetDatabase) là hệ thống quản lý Assets trong Editor. Unity tự động re-import khi file thay đổi. Trong runtime, dùng Resources.Load() hoặc Addressables để tải Asset động.

Asset Types phổ biến: .png/.jpg (Texture2D), .fbx/.obj (Mesh), .mp3/.wav (AudioClip), .cs (MonoScript), .prefab (Prefab), .mat (Material), .asset (ScriptableObject).

Import Settings: mỗi loại Asset có bộ cài đặt riêng — Texture có Compression, Max Size; Audio có Load Type (Decompress on Load / Streaming). Cấu hình đúng giúp tối ưu bộ nhớ và tốc độ load đáng kể.

Sơ đồ: Phân loại Asset

📁 Assets/
🖼

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

Mỗi file → Unity tạo file .meta với GUID duy nhất → Script tham chiếu qua GUID, không qua đường dẫn

Hướng dẫn thực hành

1

Import Asset vào project

Kéo file từ Explorer/Finder vào Project Window, hoặc Assets → Import New Asset...

2

Cấu hình Import Settings

Chọn Asset → Inspector → điều chỉnh Compression, Max Size, Load Type... → Apply.

3

Tham chiếu qua SerializeField

Kéo Asset từ Project vào field [SerializeField] trong Inspector để gán tham chiếu.

4

Load động qua Addressables

Đánh dấu Asset là Addressable, dùng Addressables.LoadAssetAsync<T>(key) để load theo yêu cầu.

5

Tổ chức thư mục hợp lý

Tạo cấu trúc rõ ràng: Assets/Art/Textures/, Assets/Scripts/... Tránh để file ở root Assets.

Trình mô phỏng tương tác

Nhấn Import để thêm Asset vào project. Click vào Asset để xem Inspector. Gán vào SerializeField để tham chiếu qua GUID.

Assets: 0

📁 Assets/

Chưa có Asset nào — nhấn "Import Asset"

Inspector

Chọn một Asset

SerializeField

Kéo Asset vào đây...

Selected: None|Tip: Mỗi Asset có file .meta chứa GUID duy nhất

Ví dụ Code

Cơ bản

Tham chiếu Asset qua SerializeField và sử dụng trong runtime.

using UnityEngine;

public class WeaponController : MonoBehaviour
{
  // Kéo Asset từ Project vào các field này trong Inspector
  [SerializeField] private Sprite     weaponIcon;
  [SerializeField] private AudioClip  shootSound;
  [SerializeField] private GameObject bulletPrefab;
  [SerializeField] private Material   glowMaterial;

  void Start()
  {
      // Sử dụng Asset đã gán — không cần tìm kiếm runtime
      GetComponent<SpriteRenderer>().sprite = weaponIcon;
  }

  public void Shoot()
  {
      Instantiate(bulletPrefab, transform.position, transform.rotation);
      AudioSource.PlayClipAtPoint(shootSound, transform.position);
  }
}

Ví dụ Code

Nâng cao

Tạo và quản lý Asset động bằng Resources.Load (Runtime) với cache.

using UnityEngine;
using System.Collections.Generic;

// Runtime asset manager — load theo key, cache lại để tái sử dụng
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 tìm file trong thư mục 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);
      }
  }
}

// Sử dụng:
// var tex = AssetCache.Load<Texture2D>("Textures/hero_sprite");

📌 Ghi nhớ nhanh

  • Mỗi Asset có GUID trong file .meta — không xóa file này
  • SerializeField = tham chiếu tĩnh; Resources.Load = tải động
  • Cấu hình Import Settings (Compression, Max Size) ảnh hưởng trực tiếp đến build size
  • Dùng Addressables thay Resources.Load cho game lớn

⚠️ Lỗi thường gặp

  • ❌ Xóa hoặc ignore file .meta

    GUID bị đổi → tất cả tham chiếu trong Scene/Prefab bị mất (Missing Reference)

    ✅ Luôn commit cả file .meta vào Git cùng file Asset

  • ❌ Đặt tất cả Asset vào Resources/

    Mọi Asset trong Resources đều bị đưa vào build → tăng kích thước không cần thiết

    ✅ Chỉ đặt Asset cần load động; còn lại dùng SerializeField