动态添加列表 [英] Dynamically adding list

查看:66
本文介绍了动态添加列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想动态创建一个列表.

I would like to dynamically create a list.

当前,我让用户输入他们想要的名称.然后,我希望他们提交的名称成为列表的名称.

Currently I have the user entering a name that they want. Then I would like the name that they have submitted to be the name of the list.

例如:

用户输入"Collection1",然后我想在另一个脚本上动态创建一个列表,如下所示:

The user entered "Collection1", then I would like to create a list dynamically on another script like the following:

List<int> Collection1 = new List<int>();

目前,我只能执行此操作,有没有一种方法可以创建列表或重命名列表,例如从 tempList Collection1 ?

Currently I'm stuck on how to do this, is there a way to create the list or rename the list for example from tempList to Collection1?

背景比较多,所以目前我正在尝试创建纸牌游戏.用户可以将选定的卡片放入卡座的位置.我想给用户一个命名牌组的选项.因此,为了区分多个卡片组(我使用列表来完成),我需要为列表使用不同的名称.最好是我想让用户插入卡片组的名称.因此,在这种情况下,我需要能够重命名/更改/创建一个基于其插入的牌组名称的列表.因此,如果用户希望将其牌组命名为 collection1 ,我希望将列表命名为 collection1List .

Abit of background, so currently im trying to create a card game. Where users are able to put the selected cards into a deck. I would like to give the user an option to name the decks. So in order to differentiate between multiple decks (which i use list to complete), i need to have a different name for the list. Preferably i would like to let the users insert the name of the deck. So in this case, i would need to be able to rename/change/ create a list that is based on their inserted deck name. So if the user wants to name their deck as collection1, i would like to have the list named as collection1List.

还有一种可能性,就是用户(如玩家)会创建多副牌.因此,不适合创建预定义列表.

There is also a possibility that the user (as in the player) will create more than one deck of cards. So it will not be suitable to create a predefined list.

推荐答案

是否可以创建列表或将列表重命名,例如从tempList更改为Collection1?

is there a way to create the list or rename the list for example from tempList to Collection1?

否!

这将是什么用例?用户不会写/读/关心代码...作为开发人员,您...在编译代码之后,您不只是以后在运行时扩展已编译的代码;)

What would be the use case for this? The user doesn't write/read/care about the code ... you as a developer are ... after that your code is compiled and you don't just extend the compiled code afterwards on runtime ;)

听起来您要使用的是 Dictionary< string,List< int>>> ,因此您可以通过 string 键名(例如,例如

It sounds like what you want to use is a Dictionary<string, List<int>> so you can address different Lists via a string key name like e.g.

private Dictionary<string, List<int>> _lists = new Dictionary<string, List<int>>();

// list is optional => if you already have a list you want to register pass it in
// if only a string is passed in a new list is automatically created
public void AddList(string keyName, List<int> list = null)
{
    if(_lists.ContainsKey(keyName))
    {
        Debug.LogError($"Already contains a list with key name {keyName}", this);
        return;
    }

    if(list == null) list = new List<int>();

    _lists.Add(keyName, list);
}

public List<int> GetList(string keyName)
{
    if(!_lists.ContainsKey(keyName))
    {
        Debug.LogError($"Does not contain a list with key name {keyName}", this);
        return null;
    }

    return _lists[keyName];
}
// or alternatively
public bool TryGetList(string keyName, out List<int> list)
{
    return _lists.TryGetValue(keyName);
}

public void RemoveList(string keyName)
{
    if(!_lists.ContainsKey(keyName))
    {
        Debug.LogError($"Does not contain a list with key name {keyName}", this);
        return null;
    }

    _lists.Remove(keyName);
}


现在由用户" IF 实际上,您的意思是说,开发人员在Unity中使用您的脚本会变得有些复杂,但是您可以做类似的事情


Now IF by "User" you actually mean a developer using your script in Unity it can get a bit more complex but you could do something like

[Serializable]
public class ListEntry
{
    public string Name;
    public List<int> Values;
}

// Edit via the Inspector in Unity
public ListEntry[] lists;

然后,您只需通过检查器添加列表并命名即可.

then you can simply add and name your lists via the Inspector.

然后我仍将在运行时使用before代码(或类似的代码),并使用例如Inspector中的值简单地创建一次字典.

I would then still use the before code (or something similar) on runtime and simply create the dictionary once using the values from the Inspector e.g.

private void Awake()
{
    foreach(var list in lists)
    {
        _lists.Add(list.Name, list.Values);
    }
}

当然,您可以/应该实施一些措施,以确保所有列表名称都是唯一的.在 OnValidate 中或在自定义编辑器脚本中都可以..但是我认为这超出了这个问题的范围;)

And of course you then can/should implement something that makes sure that all list names are unique. Either in OnValidate or in a custom editor script .. but I think that is a bit over the scope of this question ;)

这篇关于动态添加列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆