For AI agents: the complete documentation index is available at /lite-db/zh/llms.txt, the full documentation bundle is available at /lite-db/zh/llms-full.txt, and this page is available as Markdown at /lite-db/zh/getting-started.md.
  • 简体中文
  • 快速开始

    本页介绍从安装到第一次查询的最小流程。

    安装

    在 Unity Package Manager 中打开 Add package from git URL... 菜单,输入以下地址。

    https://github.com/achieveonepark/lite-db.git

    依赖

    包用途
    com.cysharp.unitask异步 DB 文件复制与初始化
    com.unity.test-frameworkUnity 测试环境

    面向 Unity 2022.3 及以上版本。

    准备 DB 文件

    LiteDB 读取 SQLite DB 文件并映射到 C# 模型。你可以用 DB Browser for SQLite 之类的工具创建 DB 文件。

    默认的初始化 API 使用以下位置。

    用途路径
    源 DBApplication.streamingAssetsPath/data/file.db
    运行时副本Application.persistentDataPath/data/localdatasqlite.db

    把源 DB 文件放在 StreamingAssets/data/file.db。

    编写数据模型

    为要查询的表编写对应的 C# 类。建议把 Id 作为主键。

    using Achieve.Database;
    using SQLite;
    
    [Table("Quest")]
    public sealed class Quest : IDataBase
    {
        [PrimaryKey]
        public int Id { get; set; }
        public string Name { get; set; }
        public int RewardGold { get; set; }
    }

    初始化与第一次查询

    在游戏启动时调用 await LiteDB.Initialize(),会把源 DB 文件复制到运行时路径并连接。之后用 LiteDB.Get<T>(id) 等 API 读取数据。

    await LiteDB.Initialize();
    
    var quest = LiteDB.Get<Quest>(1);
    if (LiteDB.TryGetValue<Quest>(2, out var nextQuest))
    {
        Debug.Log(nextQuest.Name);
    }
    Info

    在 Android 上通过 UnityWebRequest 读取 StreamingAssets 文件,其他平台使用文件复制。若要直接指定路径,请使用 LiteDB.Initialize(path)。

    后续步骤