You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity环境下如何从Firebase数据库筛选含指定字符串的分类?

在Unity中用Firebase Realtime Database筛选包含指定字符串的分类

一、先明确数据结构

根据你给出的结构,假设数据库节点结构如下:

{
  "category 1": "GCPVN-GCPVQ-GCPVR-GCPVP-GCPUZ-GCPUY-GCPUV-GCPVJ-GCPVM",
  "category 2": "GCW2H-GCW2K-GCW2M-GCW2J-GCWRV-GCWRU-GCWRG-GCW25-GCW27"
}

即每个分类的键为category 1这类标识,值是用连字符分隔的ID字符串。

二、StartAt/EndAt/EqualTo的适用范围

Firebase Realtime Database的这三个查询方法仅支持字典序的前缀/精确匹配,能处理的场景有限:

  • 精确匹配:用EqualTo查找值完全等于指定字符串的节点
  • 前缀匹配:用StartAt("目标前缀") + EndAt("目标前缀" + "\uf8ff")(\uf8ff是Unicode末尾字符,覆盖所有前缀匹配项)查找值以目标字符串开头的节点

如果你的需求是值中任意位置包含指定字符串(比如GCPVN出现在字符串中间),这三个方法无法直接实现,因为Firebase不支持模糊子串查询。

三、可行解决方案

方案1:前缀匹配场景(值以目标字符串开头)

如果要找所有值以GCPVN开头的分类,Unity代码示例:

FirebaseDatabase.DefaultInstance.GetReference("你的根节点路径")
    .OrderByValue()
    .StartAt("GCPVN")
    .EndAt("GCPVN" + "\uf8ff")
    .GetValueAsync().ContinueWith(task => {
        if (task.IsFaulted) {
            Debug.LogError(task.Exception);
        } else if (task.IsCompleted) {
            DataSnapshot snapshot = task.Result;
            foreach (DataSnapshot childSnapshot in snapshot.Children) {
                string categoryKey = childSnapshot.Key;
                string categoryValue = childSnapshot.Value.ToString();
                Debug.Log($"匹配到分类:{categoryKey} = {categoryValue}");
            }
        }
    });

方案2:任意位置包含场景(模糊匹配)

这种情况Firebase原生查询不支持,可选择以下两种方式:

  1. 客户端本地过滤:先拉取所有分类数据到Unity客户端,再用字符串Contains方法筛选(数据量小时适用):
FirebaseDatabase.DefaultInstance.GetReference("你的根节点路径")
    .GetValueAsync().ContinueWith(task => {
        if (task.IsFaulted) {
            Debug.LogError(task.Exception);
        } else if (task.IsCompleted) {
            DataSnapshot snapshot = task.Result;
            string targetStr = "GCPVN";
            foreach (DataSnapshot childSnapshot in snapshot.Children) {
                string categoryValue = childSnapshot.Value.ToString();
                if (categoryValue.Contains(targetStr)) {
                    string categoryKey = childSnapshot.Key;
                    Debug.Log($"匹配到分类:{categoryKey} = {categoryValue}");
                }
            }
        }
    });
  1. 优化数据结构:把每个分类下的ID拆为独立子节点(Firebase推荐的反范式设计,查询效率最高):
{
  "categories": {
    "category 1": {
      "GCPVN": true,
      "GCPVQ": true,
      ...
    },
    "category 2": {
      "GCW2H": true,
      ...
    }
  }
}

对应的Unity查询代码:

FirebaseDatabase.DefaultInstance.GetReference("categories")
    .OrderByChild("GCPVN")
    .EqualTo(true)
    .GetValueAsync().ContinueWith(task => {
        if (task.IsFaulted) {
            Debug.LogError(task.Exception);
        } else if (task.IsCompleted) {
            DataSnapshot snapshot = task.Result;
            foreach (DataSnapshot childSnapshot in snapshot.Children) {
                string categoryKey = childSnapshot.Key;
                Debug.Log($"包含GCPVN的分类:{categoryKey}");
            }
        }
    });

四、补充说明

  • 若你实际需求是筛选键中包含指定字符串,可替换为OrderByKey()配合StartAt/EndAt,但根据你的结构,显然更可能是值的匹配需求。
  • 操作前需确保Firebase数据库规则允许读取目标节点数据。

内容的提问来源于stack exchange,提问作者siforti

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 07:27:16