private static void CalculateInstaneCountsForPrefab(GameObject prefab, Dictionary <GameObject, bool> prefabHasChildCacheDict, int additionCount = 1) { int prefabIndex = prefabList.IndexOf(prefab); if (prefabIndex >= 0) { instanceCountArray[prefabIndex] += additionCount; } else { return; } bool hasKey = prefabHasChildCacheDict.ContainsKey(prefab); if (hasKey && !prefabHasChildCacheDict[prefab]) { return; } GameObject prefabContents = GPUInstancerUtility.LoadPrefabContents(prefab); List <Transform> childTransforms = new List <Transform>(prefabContents.GetComponentsInChildren <Transform>()); childTransforms.Remove(prefabContents.transform); if (!hasKey) { prefabHasChildCacheDict.Add(prefab, childTransforms.Count > 0); } foreach (Transform childTransform in childTransforms) { if (!childTransform) { continue; } GameObject cgo = childTransform.gameObject; if (PrefabUtility.GetPrefabAssetType(cgo) == PrefabAssetType.Regular) { GameObject cprefab = PrefabUtility.GetCorrespondingObjectFromSource(cgo); CalculateInstaneCountsForPrefab(cprefab, prefabHasChildCacheDict, additionCount); } } GPUInstancerUtility.UnloadPrefabContents(prefab, prefabContents, false); }
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]; } } }
public static void SetRenderersEnabled(GPUInstancerPrefabPrototype prefabPrototype, bool enabled) { #if UNITY_2018_3_OR_NEWER GameObject prefabContents = GPUInstancerUtility.LoadPrefabContents(prefabPrototype.prefabObject); #else GameObject prefabContents = prefabPrototype.prefabObject; #endif MeshRenderer[] meshRenderers = prefabContents.GetComponentsInChildren <MeshRenderer>(true); if (meshRenderers != null && meshRenderers.Length > 0) { for (int mr = 0; mr < meshRenderers.Length; mr++) { meshRenderers[mr].enabled = enabled; } } BillboardRenderer[] billboardRenderers = prefabContents.GetComponentsInChildren <BillboardRenderer>(true); if (billboardRenderers != null && billboardRenderers.Length > 0) { for (int mr = 0; mr < billboardRenderers.Length; mr++) { billboardRenderers[mr].enabled = enabled; } } LODGroup lodGroup = prefabContents.GetComponent <LODGroup>(); if (lodGroup != null) { lodGroup.enabled = enabled; } if (prefabPrototype.hasRigidBody) { Rigidbody rigidbody = prefabContents.GetComponent <Rigidbody>(); if (enabled || prefabPrototype.autoUpdateTransformData) { if (rigidbody == null) { GPUInstancerPrefabPrototype.RigidbodyData rigidbodyData = prefabPrototype.rigidbodyData; if (rigidbodyData != null) { rigidbody = prefabPrototype.prefabObject.AddComponent <Rigidbody>(); rigidbody.useGravity = rigidbodyData.useGravity; rigidbody.angularDrag = rigidbodyData.angularDrag; rigidbody.mass = rigidbodyData.mass; rigidbody.constraints = rigidbodyData.constraints; rigidbody.detectCollisions = true; rigidbody.drag = rigidbodyData.drag; rigidbody.isKinematic = rigidbodyData.isKinematic; rigidbody.interpolation = rigidbodyData.interpolation; } } } else if (rigidbody != null && !prefabPrototype.autoUpdateTransformData) { DestroyImmediate(rigidbody, true); } } #if UNITY_2018_3_OR_NEWER GPUInstancerUtility.UnloadPrefabContents(prefabPrototype.prefabObject, prefabContents, true); #endif EditorUtility.SetDirty(prefabPrototype.prefabObject); prefabPrototype.meshRenderersDisabled = !enabled; EditorUtility.SetDirty(prefabPrototype); }