gpt4 book ai didi

c# - 在 C# Unity3D 中使用 List<> 存储方法?

转载 作者:行者123 更新时间:2023-12-02 17:41:00 26 4
gpt4 key购买 nike

我正在尝试将方法列表存储在 List<dynamic> 中创建我的 Quest 系统的一部分。我从来没有使用过列表,我也不知道dynamic是什么。关键字确实如此,所以我在理解上遇到了问题。

首先,我创建了一个测试函数,并尝试将其存储在 List<dynamic> 中,然后通过控制台输出 Test 函数返回的内容 ( Debug.Log() )。代码如下:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Quests : MonoBehaviour
{
public List<dynamic> activeQuests = new List<dynamic>();


void Start()
{
activeQuests.Add(Test());
Debug.Log(activeQuests[0]);
}

public bool Test()
{
return true;
}
}

Debug.Log(activeQuests[0]); 下出现一条红线错误消息如下所示 -

"One or more types required to compile a dynamic expression cannot be found. Are you missing a reference?"

我该如何完成这项工作?

编辑:我也在寻找一种方法,为每个带参数的函数添加重载方法。

最佳答案

如果方法总是参数较少,我建议使用这个:

public List<System.Action> activeQuests = new List<System.Action>();

// usage
activeQuests.Add(Test)

也许需要一个返回值。那么你应该选择 Systen.Func<T ...>代表。

    public List<System.Func<bool>> activeQuests = new List<System.Func<bool>>();

// usage
activeQuests.Add(Test)

关于c# - 在 C# Unity3D 中使用 List<> 存储方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31837849/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com