如何添加撤消按钮功能?

问题描述 投票:0回答:1

这是我的轮盘游戏项目,我是 Unity 的新学习者。在这个项目中,我面临创建撤消按钮的问题。

这是我的撤消按钮代码:

public void Undo() {
    if (Bets.Count != 0 && BetSpaces.Count != 0) {
        var lastBet = Bets.Last();
        var lastSpace = BetSpaces.Last();
        //UserDetail.Balance += lastBet.chip;
        UserDetail.Balance += BetSpaces.Last().stack.value;
        UpdateBalance(UserDetail.Balance);
        lastSpace.SetAllMeshes(false);
        lastSpace.Clear();
        Bets.Remove(lastBet);
        BetSpaces.Remove(lastSpace);
        var totalBets = int.Parse(TotalBetCount.text);
        if (totalBets > 0) TotalBetCount.text = (totalBets--).ToString();
        TakeButton.interactable = false;
    }
    var indexesToRemove = new List<int>();
    BetSpaces.ForEach(X => {
        GlobalFunctions.PrintLog($"X: {X.stack.value}");
        if (X.stack.value == 0) {
            indexesToRemove.Add(BetSpaces.IndexOf(X));
        } else {
            X.SetAllMeshes(true);
        }
    });
    for (int i = 0; i < indexesToRemove.Count; i++) {
        Bets.RemoveAt(indexesToRemove[i]);
        BetSpaces.RemoveAt(indexesToRemove[i]);
    }
    UndoButton.interactable = Bets.Count != 0;
    SceneRoulette._Instance.clearButton.interactable = Bets.Count != 0;
    SceneRoulette._Instance.rollButton.interactable = Bets.Count != 0;
}

以下是该功能如何工作的详细说明:

状况检查:

--> 它首先检查是否有任何投注(Bets.Count!= 0)。

--> 如果没有下注,则本质上意味着没有什么可撤消的,因此函数返回而不执行任何进一步的操作。

撤销最后的赌注:

--> 如果有投注,则会检索上次投注的详细信息 (lastBet) 以及相应的投注空间 (lastSpace)。

--> 然后将投注金额添加回玩家的余额 (UserDetail.Balance += BetSpaces.Last().stack.value),有效地退还玩家未完成的投注。

--> 它清除与最后一个投注空间 (lastSpace.SetAllMeshes(false)) 相关的视觉效果,删除游戏界面上投注的任何视觉指示。

--> 它从各自的列表中删除最后一个投注 (Bets.Remove(lastBet)) 和相应的投注空间 (BetSpaces.Remove(lastSpace))。

--> 它会更新 UI 上显示的总投注数,如果总投注数大于 0,则将其减少未完成投注的金额。

处理剩余投注:

--> 然后迭代所有下注空间,检查其中是否有任何一个的堆栈值为 0,表示尚未对它们下注。

--> 如果投注空间的堆栈值为 0,则会从各自的列表中删除投注和投注空间。

--> 此步骤确保从考虑中删除任何空的投注空间(玩家未下注的地方),保持投注和投注空间列表同步。

更新按钮交互性:

--> 最后,它根据剩余赌注的存在来更新游戏界面上各个按钮的交互性。

--> 如果还有剩余赌注 (Bets.Count != 0),则会启用清除和滚动按钮,同时禁用撤消按钮。

--> 如果没有剩余赌注,它将禁用所有按钮,因为没有什么可做的。

总的来说,Undo() 函数为玩家提供了一种机制,可以撤回他们上次的下注操作,返回他们的下注金额,并从游戏界面中删除下注的视觉指示。

为了测试此代码,我将 1R 的 3 个赌注放在同一位置,然后按下撤消按钮。它将删除整个 3R。一次下注,但我想取消对单独时间的下注。有人可以帮助我吗?

c# unity-game-engine unityscript
1个回答
0
投票

正如第一条评论已经说过的,您可以使用可以撤消自身的命令。这个想法是有一个

Command
类,有一个
Do
和一个
Undo
方法。 这些方法有一个
ICommandContext context
参数。这基本上就是您的游戏应用程序。它允许命令访问游戏并对其进行操作。

public abstract class Command
{
    public abstract void Do(ICommandContext context);
    public abstract void Undo(ICommandContext context);
}

从这个抽象类中,您可以派生出不同的命令,例如一个

PlaceBetCommand
。这个具体类必须存储在调用
Do
时能够撤消
Do
命令所需的所有信息。

// Primary constructor
public class PlaceBetCommand : Command
{
    public PlaceBetCommand(/* TODO: add parameters describing the bet */)
    {
        // TODO: store the parameters
    }

    public override void Do(ICommandContext context)
    {
        // TODO: place the bet and possibly store additional information that you will need in Undo.
    }

    public abstract void Undo(ICommandContext context)
    {
        // Undo the bet
    }
}

Then you need a undo-stack and posssibly a redo-stack (but you didn't ask for this).
``` c#
private static readonly Stack<Command> _undoStack = new();

下注是这样的

vat bet = new PlaceBetCommand(/* pass the required parameters */);
bet.Do(this);
_undoStack.Push(bet);

撤销操作如下:

if (_undoStack.Count > 0) {
    var command = _ndoStack.Pop();
    command.Undo(this);
}

我让你弄清楚细节,因为我不知道你如何下注以及你可能有什么其他命令。

如果你想能够一一撤消放置的 3 Rs,那么将其分成 3 个命令并分别执行和推送。

© www.soinside.com 2019 - 2024. All rights reserved.