private static void GetPrefabInstanceCounts() { GameObject[] prefabInstances = (GameObject[])FindObjectsOfType(typeof(GameObject)); #if UNITY_2018_3_OR_NEWER Dictionary <GameObject, bool> prefabHasChildCacheDict = new Dictionary <GameObject, bool>(); List <GameObject> prefabAssetList = GPUInstancerUtility.GetCorrespondingPrefabAssetsOfGameObjects(prefabInstances); Dictionary <GameObject, int> distinctPrefabs = new Dictionary <GameObject, int>(); foreach (GameObject prefab in prefabAssetList) { if (!distinctPrefabs.ContainsKey(prefab)) { distinctPrefabs.Add(prefab, 1); } else { distinctPrefabs[prefab]++; } } foreach (GameObject prefab in distinctPrefabs.Keys) { CalculateInstaneCountsForPrefab(prefab, prefabHasChildCacheDict, distinctPrefabs[prefab]); } #else foreach (GameObject go in prefabInstances) { bool isPrefabInstance = PrefabUtility.GetPrefabType(go) == PrefabType.PrefabInstance; if (isPrefabInstance) { #if UNITY_2018_2_OR_NEWER GameObject prefab = (GameObject)PrefabUtility.GetCorrespondingObjectFromSource(go); #else GameObject prefab = (GameObject)PrefabUtility.GetPrefabParent(go); #endif int prefabIndex = prefabList.IndexOf(prefab); if (prefabIndex >= 0) { instanceCountArray[prefabIndex]++; } } } #endif maxInstanceCount = 0; for (int i = 0; i < instanceCountArray.Length; i++) { if (maxInstanceCount < instanceCountArray[i]) { maxInstanceCount = instanceCountArray[i]; } } }
private static void GetPrefabInstanceCounts() { GameObject[] prefabInstances = (GameObject[])FindObjectsOfType(typeof(GameObject)); #if UNITY_2018_3_OR_NEWER List <GameObject> prefabAssetList = GPUInstancerUtility.GetCorrespondingPrefabAssetsOfGameObjects(prefabInstances); foreach (GameObject prefab in prefabAssetList) { int prefabIndex = prefabList.IndexOf(prefab); if (prefabIndex >= 0) { instanceCountArray[prefabIndex]++; } GameObject prefabContents = GPUInstancerUtility.LoadPrefabContents(prefab); List <Transform> childTransforms = new List <Transform>(prefabContents.GetComponentsInChildren <Transform>()); childTransforms.Remove(prefabContents.transform); foreach (Transform childTransform in childTransforms) { GameObject cgo = childTransform.gameObject; if (PrefabUtility.GetPrefabAssetType(cgo) == PrefabAssetType.Regular) { GameObject cprefab = PrefabUtility.GetCorrespondingObjectFromSource(cgo); if (cprefab != null && cprefab.transform.parent == null) { int cprefabIndex = prefabList.IndexOf(cprefab); if (cprefabIndex >= 0) { instanceCountArray[cprefabIndex]++; } } } } GPUInstancerUtility.UnloadPrefabContents(prefab, prefabContents, false); } #else foreach (GameObject go in prefabInstances) { bool isPrefabInstance = PrefabUtility.GetPrefabType(go) == PrefabType.PrefabInstance; if (isPrefabInstance) { #if UNITY_2018_2_OR_NEWER GameObject prefab = (GameObject)PrefabUtility.GetCorrespondingObjectFromSource(go); #else GameObject prefab = (GameObject)PrefabUtility.GetPrefabParent(go); #endif int prefabIndex = prefabList.IndexOf(prefab); if (prefabIndex >= 0) { instanceCountArray[prefabIndex]++; } } } #endif maxInstanceCount = 0; for (int i = 0; i < instanceCountArray.Length; i++) { if (maxInstanceCount < instanceCountArray[i]) { maxInstanceCount = instanceCountArray[i]; } } }