DuplicateManager.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. public class DuplicateManager : MonoBehaviour
  6. {
  7. private void Start()
  8. {
  9. var objects = FindObjectsOfType<GameObject>();
  10. var objectsAtPos = new Dictionary<Vector3, List<GameObject>>();
  11. Debug.Log($"Found {objects.Length} GameObjects");
  12. var i = 0;
  13. foreach (var o in objects)
  14. {
  15. var pos = o.transform.position;
  16. if (!objectsAtPos.ContainsKey(pos))
  17. {
  18. var l = new List<GameObject> {o};
  19. objectsAtPos[pos] = l;
  20. }
  21. else
  22. {
  23. objectsAtPos[pos].Add(o);
  24. }
  25. i++;
  26. if (i % 100 == 0)
  27. {
  28. Debug.Log($"{i}/{objects.Length} objects done!");
  29. }
  30. }
  31. Debug.Log($"Done sorting");
  32. var objs = objectsAtPos.Values.Where(l => l.Count > 1);
  33. Debug.LogWarning($"{objs.Count()} gameobjects at exact same position");
  34. Debug.Log("Deleting objects..");
  35. foreach (var o in objs)
  36. {
  37. o.RemoveAt(0);
  38. foreach (var d in o)
  39. {
  40. Destroy(d);
  41. }
  42. }
  43. }
  44. }