最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Compare a list of clones of an object to a list with the original objects in unity - Stack Overflow

programmeradmin5浏览0评论

I have a list of 10 game objects and i want to compare this list to a list with clones of these game objects. I want to check if the content of the two lists are the same regardless of the order but the one contains original objects and the other contains clones of the same objects. These are my lists

public List<GameObject> Materials = new List<GameObject>();
public List<GameObject> materialsRequired = new List<GameObject>();

and this is the algorithm i use to compare them which I got from some other stackoverflow answer:

 public static bool ScrambledEquals<T>(IEnumerable<T> list1, IEnumerable<T> list2)
    {
        var cnt = new Dictionary<T, int>();
        foreach (T s in list1)
        {
            if (cnt.ContainsKey(s))
            {
                cnt[s]++;
            }
            else
            {
                cnt.Add(s, 1);
            }
        }
        foreach (T s in list2)
        {
            if (cnt.ContainsKey(s))
            {
                cnt[s]--;
            }
            else
            {
                return false;
            }
        }
        return cnt.Values.All(c => c == 0);
    }

even though this answer works if both lists contain the original asset of the game object, it doesnt work when one contains the same objects but instead of the original game objects it contains different instances/clones of the object.

Does anyone know how I can fix this?

发布评论

评论列表(0)

  1. 暂无评论