123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine;
- public class DuplicateManager : MonoBehaviour
- {
- private void Start()
- {
- var objects = FindObjectsOfType<GameObject>();
- var objectsAtPos = new Dictionary<Vector3, List<GameObject>>();
- Debug.Log($"Found {objects.Length} GameObjects");
- var i = 0;
- foreach (var o in objects)
- {
- var pos = o.transform.position;
- if (!objectsAtPos.ContainsKey(pos))
- {
- var l = new List<GameObject> {o};
- objectsAtPos[pos] = l;
- }
- else
- {
- objectsAtPos[pos].Add(o);
- }
- i++;
- if (i % 100 == 0)
- {
- Debug.Log($"{i}/{objects.Length} objects done!");
- }
- }
- Debug.Log($"Done sorting");
- var objs = objectsAtPos.Values.Where(l => l.Count > 1);
- Debug.LogWarning($"{objs.Count()} gameobjects at exact same position");
- Debug.Log("Deleting objects..");
- foreach (var o in objs)
- {
- o.RemoveAt(0);
- foreach (var d in o)
- {
- Destroy(d);
- }
- }
- }
- }
|