【问题标题】:Can't get RunTransaction with Firebase in Unity to work无法在 Unity 中使用 Firebase 的 RunTransaction 工作
【发布时间】:2019-09-18 15:12:09
【问题描述】:

我正在 Unity 中开发一款游戏,使用 Firebase 获取用户和游戏数据。 将游戏保存到 Unity 一切都很好,但在多人游戏中,我面临将开放游戏与唯一玩家 2 配对的挑战。 流程是: 1.你寻找一个状态为“开放”的游戏 2.如果X秒内没有找到游戏,则创建一个状态为“open”的新游戏

我有一个查询返回打开的游戏(状态为“打开”的游戏) 此查询添加了 Firebase 事件处理程序

public void OnButtonLookForOpenMPGames()
{
this.OpenFirebaseGamesAsHost = this.LiveGamereference.OrderByChild("State").EqualTo(WYMSettings.MP_GAME_STATE_OPEN_TO_PLAYER2)
                .LimitToFirst(3);

this.OpenFirebaseGamesAsHost.ChildAdded += this.OnOpenMPGameFound;
}

这会在处理程序中正常触发:

 private void OnOpenMPGameFound(object sender, ChildChangedEventArgs args)
    {
        Debug.Log("Looking for open games");

        if (args.DatabaseError != null)
        {
            // handle errors
        }
        else
        {
            Debug.Log("Open game found (it may be my own)");

            // remove event listener because we got a hit
            // it will fire as many times as it's true!
            this.OpenFirebaseGamesAsHost.ChildAdded -= this.OnOpenMPGameFound;

            if (args.Snapshot.Child("HostId").Value.ToString() != FirebaseAuth.DefaultInstance.CurrentUser.UserId)
            {
                Debug.Log("Other game than mine found open");

                // this.AddDelayedUpdateAction(() =>
                //      {
                //          this.MPGameLockInTransaction(args.Snapshot.Reference);
                //      });

                this.MPGameLockInTransaction(args.Snapshot.Reference);
            }
        }
    }

问题:处理程序将此传递给事务函数,但由于内部异常(内部任务故障)而失败 - 我尝试了许多不同的变体,但无法从官方 Firebase 示例中找出这一点.

问题代码: 我想要实现的是仅将该游戏锁定给这两个玩家,即本示例中的主机和玩家 2,查询开放游戏。 State 是一个 int,但为了清楚起见,这里是一个字符串。

 private void MPGameLockInTransaction(DatabaseReference mpreference)
    {
        mpreference.RunTransaction(mutableData => 
        {

                MultiPlayerGame transactionMPG = mutableData.Value as MultiPlayerGame;

                if (transactionMPG == null)
                {
                    return TransactionResult.Abort();
                } 

                if (transactionMPG.State != "open")
                {
                    // game is taken, abort
                    Debug.Log("transaction aborted");
                    return TransactionResult.Abort();
                }


                transactionMPG.State = "game started";

                mutableData.Value = transactionMPG;

            return TransactionResult.Success(mutableData);


        }).ContinueWith(task =>
        {
            if (task.Exception != null)
            {
                Debug.Log("Transactionlock to game failed" + task.Exception.ToString());
                // Look over again
                // not implemented yet
           }
            else
            {
                Debug.Log("starting game immediately");

                this.AddDelayedUpdateAction(() => this.StartMPGameImmediatelyFromSearch());

            }
        });
    }

【问题讨论】:

  • 您能否添加您得到的异常并指出具体在哪一行?
  • 这是异常:Transactionlock to game failedSystem.AggregateException:发生了一个或多个错误。 ---> Firebase.Database.DatabaseException:内部任务出错---> System.AggregateException:发生了一个或多个错误。 ---> Firebase.FirebaseException --- 内部异常堆栈跟踪结束 --- --- 内部异常堆栈跟踪结束 --- --- 内部异常堆栈跟踪结束 --- ---> (Inner Exception #0) Firebase.Database.DatabaseException:内部任务出错 ---> System.AggregateException:发生一个或多个错误。 ---> Firebase.FirebaseException
  • 发生在这一行:Debug.Log("Transactionlock to game failed" + task.Exception.ToString());
  • 嗯,奇怪..虽然这个Debug.Log("transaction aborted"); 可能已经是一个问题,但您是否尝试将其删除?我不是 Firebase 专家 .. 你能逐行检查所有值吗?
  • 可变数据第一次为空(如预期)然后发生错误(并不断触发,不是代码崩溃)。但是,上一个函数中的引用是正确的,最后一个函数中的引用也是正确的。

标签: unity3d firebase-realtime-database


【解决方案1】:

这只是一个猜测,但可能与线程有关。

在较新的 .Net 版本中,ContinueWith 可能会在大多数 Unity API 可能无法调用的线程上结束(包括 Debug.Log)。

相反,您可以尝试使用 extensions 中的 ContinueWithOnMainThread,而不是正是为此原因创建的。

使用 .NET 4.x 运行时,延续可能经常在后台执行,而此扩展方法将它们推送到 主线程

.NET 3.x 运行时没有这个问题,因为 Parse 库中包含延续,它隐式地做到了这一点。

【讨论】:

    猜你喜欢
    • 2016-06-19
    • 1970-01-01
    • 2023-02-10
    • 2018-11-27
    • 2021-12-30
    • 2015-03-15
    • 2019-08-07
    相关资源
    最近更新 更多