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

Unity环境下Firebase Analytics同意模式实现及日志排查咨询

Unity Firebase Analytics同意模式实现问题排查与修复

一、代码中的核心语法错误

你的代码存在几处编译和逻辑错误,直接导致同意设置逻辑无法正常执行:

  • if (dependencyStatus == Firebase.DependencyStatus.Available) 语句后缺少大括号{,后续代码未被包裹在判断块内,逻辑完全失效。
  • 存在未注释的自然语言文本:Crashlytics will use the DefaultInstance, as well;和Set a flag here for indicating that your project is ready to use Firebase.,这两行会直接引发编译失败。

二、修复后的完整代码

Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
{
    var dependencyStatus = task.Result;
    if (dependencyStatus == Firebase.DependencyStatus.Available)
    {
        // Create and hold a reference to your FirebaseApp,
        // where app is a Firebase.FirebaseApp property of your application class.
        // Crashlytics will use the DefaultInstance, as well;
        // this ensures that Crashlytics is initialized.
        Firebase.FirebaseApp app = Firebase.FirebaseApp.DefaultInstance;

        // Set a flag here for indicating that your project is ready to use Firebase.

        var FC_Consent = new Dictionary<ConsentType, ConsentStatus>();
        FC_Consent.Add(ConsentType.AnalyticsStorage, ConsentStatus.Granted);
        FC_Consent.Add(ConsentType.AdStorage, ConsentStatus.Granted);

        FirebaseAnalytics.SetConsent(FC_Consent);
        FirebaseAnalytics.SetAnalyticsCollectionEnabled(true);
    }
    else
    {
        UnityEngine.Debug.LogError(System.String.Format(
            "Could not resolve all Firebase dependencies: {0}", dependencyStatus));
        // Firebase Unity SDK is not safe to use here.
    }
});

三、日志不显示的补充配置

修复代码后,要看到预期的ConsentSettings日志,还需开启Firebase Analytics调试模式:

  • 在初始化逻辑中添加调试开关:
    // 建议在Firebase依赖检查完成前或设置同意前调用
    FirebaseAnalytics.SetDebugModeEnabled(true);
    
  • 检查Unity控制台日志级别,确保未过滤Info级别的日志(Firebase同意相关日志属于Info级别)。

四、额外注意事项

  • 确保使用最新版本的Firebase Unity SDK,旧版本对同意模式的支持可能存在缺陷。
  • 同意设置必须在发送任何分析事件前调用,避免事件被拦截。
  • 确认设备/模拟器已正确配置Firebase项目(google-services.json/GoogleService-Info.plist文件无误)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 16:13:13