【问题标题】:Automate a set in Unity在 Unity 中自动化设置
【发布时间】:2022-08-12 04:12:31
【问题描述】:

我想知道是否有一种方法可以自动化 Unity 中的集合并将其添加到列表中。例如我有三个字符串变量:

string a;
string b;
string c;

我想将结构化集创建为:

(a)
(b)
(c)
(a, b)
(a, c)
(b, c)
(a, b, c)

有没有一种方法可以让脚本自动执行此操作,或者 Unity 中是否有一个属性可以实现此结果?

我试图通过将每个添加到列表中来手动完成,但如果有更多集合,那只是很多手动工作。我试图避免这种技术。

  • 所以你想要组合。你试过什么?
  • 是的。因为我不知道 C# 中是否有集合,所以我只尝试创建一个字符串并将 1 和 3 之间的值随机化以添加到该字符串中。不是真正的方法。你有什么建议?
  • 我正在做的另一种方法是将这些集合中的每一个添加到字符串列表中。更多的手工作业问题。
  • 您正在显示类似(a) 的输出,但这不是返回类型。你想在这里做什么? List<List<string>> 还是别的什么?
  • 是的,我只想要一个列表,每行都有这些字符串。基本上第1行:(a),第2行:(a,b)等。

标签: c# unity3d


【解决方案1】:

我正在使用另一个问题的答案,因为赏金问题不能重复关闭:

How to use LINQ to find all combinations of n items from a set of numbers?

public class Program
{
    public static void Main()
    {
        var result = string.Join("\n", new []{"a", "b", "c"}.DifferentCombinationsUpTo(3).Select(x=> "("+string.Join(",", x)+")"));
        Console.WriteLine(result);
    }
}

public static class Ex
{
    public static IEnumerable<IEnumerable<T>> DifferentCombinationsUpTo<T>(this IEnumerable<T> elements, int k)
    {
        return Enumerable.Range(1, k).SelectMany(x=> elements.DifferentCombinations(x));
    }
    
    public static IEnumerable<IEnumerable<T>> DifferentCombinations<T>(this IEnumerable<T> elements, int k)
    {
        return k == 0 ? new[] { new T[0] } :
          elements.SelectMany((e, i) =>
            elements.Skip(i + 1).DifferentCombinations(k - 1).Select(c => (new[] {e}).Concat(c)));
    }
}

结果:

(a)
(b)
(c)
(a,b)
(a,c)
(b,c)
(a,b,c)

【讨论】:

    【解决方案2】:

    根据对this 问题的回答查找数组和列表的解决方案。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using UnityEngine;
    
    public class CombinationsLog : MonoBehaviour
    {
        void Start()
        {
            var data = new char[] { 'A', 'B', 'C' };
    
            var result = CombinationsArr(data);
    
            foreach (var item in result)
                Debug.Log($"[{string.Join(", ", item)}]");
    
            var dataList = new List<string> { "A", "B", "C" };
    
            var resultList = CombinationsList(dataList);
    
            Debug.Log("/***********************LIST LOG********************************");
    
            foreach (var item in resultList)
                Debug.Log($"[{string.Join(", ", item)}]");
    
        }
    
        private IEnumerable<T[]> CombinationsArr<T>(IEnumerable<T> source) {
            if (null == source)
                throw new ArgumentNullException(nameof(source));
    
            T[] data = source.ToArray();
    
            return Enumerable
              .Range(1, (1 << (data.Length)) - 1)
              .Select(index => data
                 .Where((v, i) => (index & (1 << i)) != 0)
                 .ToArray());
        }
    
        private IEnumerable<List<T>> CombinationsList<T>(IEnumerable<T> source) {
            if (null == source)
                throw new ArgumentNullException(nameof(source));
    
            T[] data = source.ToArray();
    
            return Enumerable
              .Range(1, (1 << (data.Length)) - 1)
              .Select(index => data
                 .Where((v, i) => (index & (1 << i)) != 0)
                 .ToList());
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-24
      • 2022-11-25
      • 1970-01-01
      • 2021-07-15
      • 1970-01-01
      • 1970-01-01
      • 2023-01-01
      相关资源
      最近更新 更多