private static uint LimitRenderer(Renderer renderer) { renderer.GetSharedMaterials(materials); if (materials.Count > MaxMaterialsOnRenderer) { var distinctMaterials = materials.Distinct(); materials.RemoveAll(m => m == null); if (distinctMaterials.Count() == 1) { renderer.sharedMaterials = distinctMaterials.ToArray(); } else { materials.Clear(); materials.AddRange(distinctMaterials); int remove = Math.Max(0, materials.Count - MaxMaterialsOnRenderer - 1); materials.RemoveRange(MaxMaterialsOnRenderer, remove); renderer.sharedMaterials = materials.ToArray(); } } for (int j = 0; j < materials.Count; j++) { if (!materials[j]) { continue; } if (ShaderBlacklistSet.Contains(materials[j].shader.name)) { materials[j].shader = fallbackShader; materials[j].enableInstancing = true; } int rq = materials[j].renderQueue; if (rq >= 5000) { materials[j].renderQueue = 4999; } if (rq < 1000) { materials[j].renderQueue = 1000; } } return((uint)materials.Count); }
/// <summary> /// Removes all components from gameObject and its children that are not on a whitelist. /// </summary> /// <param name="gameObject">The GameObject to sanitize.</param> /// <param name="assetType">The type of asset to sanitize that determines the allowed components.</param> /// <param name="removeFromAllowedList">Component types to remove from the default allowed components lists for the asset type.</param> /// <returns>The time taken in milliseconds.</returns> public static uint SanitizeGameObject(GameObject gameObject, AssetType assetType, List <Type> removeFromAllowedList = null) { var previous = Application.GetStackTraceLogType(LogType.Error); Application.SetStackTraceLogType(LogType.Error, StackTraceLogType.None); stopWatch.Stop(); stopWatch.Reset(); stopWatch.Start(); Debug.Log($"Starting SanitizeGameObject for '{gameObject.name}'"); var allowedTypes = GetAllowedTypesHashSet(assetType); Debug.Log("allowedTypes count: " + allowedTypes.Count); if (removeFromAllowedList != null) { foreach (var item in removeFromAllowedList) { allowedTypes.Remove(item); } } removedComponents.Clear(); if (ShaderBlacklistSet.Count == 0) { ShaderBlacklistSet.UnionWith(Whitelists.ShaderBlacklist); } uint materialCount = 0; gameObject.GetComponentsInChildren(true, components); for (int i = 0; i < components.Count; i++) { Component component = components[i]; if (component == null) { continue; } var renderer = component as Renderer; if (renderer) { materialCount += LimitRenderer(renderer); } Type compType = component.GetType(); if (compType == transformType || allowedTypes.Contains(compType)) { if (greyList.Contains(compType)) { var mono = component as MonoBehaviour; if (mono) { mono.enabled = false; } } continue; } RemoveComponent(component, compType); } if (removedComponents.Length > 0) { Debug.LogError(removedComponents.ToString(), gameObject); } stopWatch.Stop(); Application.SetStackTraceLogType(LogType.Error, previous); return((uint)(stopWatch.Elapsed.TotalMilliseconds * 1000)); }