将参数传递给 Unity Action

Passing parameters to Unity Action

当我启动我的游戏对象时,我会循环遍历所有对话框。对于每个对话框都有一个动作。启动任务或更改 NPC 阶段。

我在下面传递给 PopulateConversationList 的方法在我单击对话交互中的最后一个按钮时被调用,即 "Okay"。下面我传递了一个 ActiveNpcQuest(questId) 方法,并传递了 questId(从对话数据中获取),如下所示:

//loop through all dialogs
for (int i = 0; i < dialogData.dialogs.Count; i++) 
    {
        startsQ = dialogData.dialogs[i].dialogStartsQuest;
        questId = dialogData.dialogs[i].dialogBelongToQuestId;

            //if we have a quest, activate it (if already active, nothing happens)
            if (startsQ) 
            {
                PopulateConversationList(dialogData.dialogs[i].dialogsInThisStage[j].pages, "Okay", npcObject.npcName, dialogData.dialogs[i].npcStage, () =>
                {
                    ActivateNPCQuest(questId); //This is the parameter I want to send
                });
            }
            //If this dialog ALWAYS changes the NPC stage, then change stage
            else if (autoChangeStage) 
            {
                PopulateConversationList(dialogData.dialogs[i].dialogsInThisStage[j].pages, "Okay", npcObject.npcName, dialogData.dialogs[i].npcStage, ChangeNPCStage);
            }
        }

我猜 questId 的值实际上并没有保存在 ActivateNPCQuest 方法中。这很有意义。但是有什么办法可以让我在这里做我想做的事吗?

我的PopulateConversationList声明:

public void PopulateConversationList(List<string> fullConversation, string onLastPagePrompt, string npcName, int stage, UnityAction action)

我的ActiveNpcQuest-方法:

public void ActivateNPCQuest(int id)
{
    for (int i = 0; i < npcQuests.Count; i++)
    {
        if (npcQuests[i].questId == id && !npcQuests[i].active && !npcQuests[i].objective.isComplete)
        {
            npcQuests[i].active = true;
            npcObject.hasActiveQuest = true;
        }
    }
}

不知道要搜索什么,不确定 Unity Actions 是 Lambda 表达式还是事件。

如果没有完整的代码并且不知道您的期望是什么,很难理解您的问题,但看起来您由于关闭而遇到了问题。尝试在循环的每次迭代中将 questId 分配给一个新变量,并将其传递给 ActivateNPCQuest 方法以查看是否获得所需的结果:

//loop through all dialogs
for (int i = 0; i < dialogData.dialogs.Count; i++) 
    {
        startsQ = dialogData.dialogs[i].dialogStartsQuest;
        var questId = dialogData.dialogs[i].dialogBelongToQuestId; \<----

            //if we have a quest, activate it (if already active, nothing happens)
            if (startsQ) 
            {
                PopulateConversationList(dialogData.dialogs[i].dialogsInThisStage[j].pages, "Okay", npcObject.npcName, dialogData.dialogs[i].npcStage, () =>
                {
                    ActivateNPCQuest(questId); //This is the parameter I want to send
                });
            }
            //If this dialog ALWAYS changes the NPC stage, then change stage
            else if (autoChangeStage) 
            {
                PopulateConversationList(dialogData.dialogs[i].dialogsInThisStage[j].pages, "Okay", npcObject.npcName, dialogData.dialogs[i].npcStage, ChangeNPCStage);
            }
        }