+ {
+ { 43, "Mesh" }, { 28, "Texture2D" }, { 21, "Material" }, { 74, "AnimationClip" },
+ { 48, "Shader" }, { 89, "Cubemap" }, { 117, "Texture3D" }, { 91, "AnimatorController" },
+ };
+
+ static void BuildHints(SceneSizeReport r)
+ {
+ long total = Math.Max(1, r.FileBytes);
+ double Share(long b) => 100.0 * b / total;
+
+ long prefab = r.ByType.Where(e => e.Key.StartsWith("PrefabInstance")).Sum(e => e.Bytes);
+ if (Share(prefab) > 15)
+ r.Hints.Add($"PrefabInstance overrides take {Share(prefab):0}% of the file. Large m_Modifications lists usually mean many per-instance overrides. Apply overrides to the prefab, use prefab variants, or reduce serialized fields on prefab components.");
+
+ var scripts = r.ByType.Where(e => e.Key.StartsWith("MonoBehaviour")).ToList();
+ long mono = scripts.Sum(e => e.Bytes);
+ if (Share(mono) > 15)
+ r.Hints.Add($"MonoBehaviour data takes {Share(mono):0}%. Biggest scripts: " +
+ string.Join(", ", scripts.Take(3).Select(e => e.Key.Replace("MonoBehaviour: ", "") + " " + Fmt(e.Bytes))) +
+ ". Check for large serialized arrays/strings; mark runtime caches [NonSerialized] or move data to ScriptableObjects / assets.");
+
+ var embedded = r.Docs.Where(d => EmbeddedAssetTypes.ContainsKey(d.ClassId)).ToList();
+ if (embedded.Count > 0)
+ r.Hints.Add($"{embedded.Count} asset object(s) are embedded in the scene ({Fmt(embedded.Sum(d => d.Bytes))}): " +
+ string.Join(", ", embedded.GroupBy(d => EmbeddedAssetTypes[d.ClassId]).Select(g => g.Key + " x" + g.Count())) +
+ ". Save them as project assets (e.g. generated/ProBuilder meshes, runtime-created materials).");
+
+ if (r.TopDocs.Count > 0 && Share(r.TopDocs[0].Bytes) > 10)
+ r.Hints.Add($"A single object dominates: {r.TopDocs[0].DisplayType} '{r.TopDocs[0].Name}' is {Share(r.TopDocs[0].Bytes):0}% of the file. See its largest fields in the report.");
+
+ int goCount = r.Docs.Count(d => d.ClassId == 1);
+ if (goCount > 20000)
+ r.Hints.Add($"{goCount:N0} GameObjects in the scene. Consider prefabs, static batching via assets, additive scenes, or generating content at runtime.");
+
+ long perObject = goCount > 0 ? (r.FileBytes - r.UnownedBytes) / goCount : 0;
+ if (goCount > 0 && r.Hints.Count == 0)
+ r.Hints.Add($"No single culprit; size is spread over {goCount:N0} objects (~{Fmt(perObject)} each). Check the per-type table for the heaviest component types.");
+ }
+
+ public static string Fmt(long bytes)
+ {
+ if (bytes >= 1L << 30) return $"{bytes / (double)(1L << 30):0.00} GB";
+ if (bytes >= 1L << 20) return $"{bytes / (double)(1L << 20):0.00} MB";
+ if (bytes >= 1L << 10) return $"{bytes / 1024.0:0.0} KB";
+ return bytes + " B";
+ }
+ }
+
+ // ------------------------------------------------------------------ Writers
+
+ public static class SceneSizeReportWriter
+ {
+ static string Fmt(long b) => SceneSizeReport.Fmt(b);
+ static string Pct(long b, long total) => total > 0 ? $"{100.0 * b / total:0.0}%" : "0%";
+
+ public static string ToText(SceneSizeReport r)
+ {
+ var sb = new StringBuilder();
+ long t = r.FileBytes;
+ sb.AppendLine($"Scene: {r.ScenePath}");
+ sb.AppendLine($"Size: {Fmt(t)} ({t:N0} bytes) | {r.Docs.Count:N0} objects | {r.Docs.Count(d => d.ClassId == 1):N0} GameObjects");
+ sb.AppendLine();
+
+ if (r.Hints.Count > 0)
+ {
+ sb.AppendLine("HINTS");
+ foreach (var h in r.Hints) sb.AppendLine(" * " + h);
+ sb.AppendLine();
+ }
+ if (!r.IsText) return sb.ToString();
+
+ sb.AppendLine("BY TYPE");
+ foreach (var e in r.ByType.Take(r.TopN))
+ sb.AppendLine($" {Fmt(e.Bytes),10} {Pct(e.Bytes, t),6} x{e.Count,-7} {e.Key}");
+ sb.AppendLine();
+
+ sb.AppendLine("BY FIELD (Type.field, summed over all objects)");
+ foreach (var e in r.ByField)
+ sb.AppendLine($" {Fmt(e.Bytes),10} {Pct(e.Bytes, t),6} x{e.Count,-7} {e.Key}");
+ sb.AppendLine();
+
+ sb.AppendLine("LARGEST SINGLE OBJECTS");
+ foreach (var d in r.TopDocs)
+ {
+ sb.AppendLine($" {Fmt(d.Bytes),10} {Pct(d.Bytes, t),6} {d.DisplayType} '{d.Name}' &{d.FileId}");
+ foreach (var f in d.Fields.OrderByDescending(f => f.Value).Take(3))
+ sb.AppendLine($" {"",10} {"",6} {f.Key}: {Fmt(f.Value)}");
+ }
+ sb.AppendLine();
+
+ sb.AppendLine("LARGEST GAMEOBJECTS (object + own components)");
+ foreach (var n in r.TopObjects)
+ sb.AppendLine($" {Fmt(n.OwnBytes),10} {Pct(n.OwnBytes, t),6} {n.Path}");
+ sb.AppendLine();
+
+ sb.AppendLine("LARGEST HIERARCHY SUBTREES");
+ foreach (var n in r.TopSubtrees)
+ sb.AppendLine($" {Fmt(n.SubtreeBytes),10} {Pct(n.SubtreeBytes, t),6} {n.Path} ({CountNodes(n):N0} objects)");
+ sb.AppendLine();
+
+ sb.AppendLine($"Scene settings / unowned: {Fmt(r.UnownedBytes)} YAML header: {Fmt(r.HeaderBytes)}");
+ return sb.ToString();
+ }
+
+ public static string ToHtml(SceneSizeReport r)
+ {
+ long t = r.FileBytes;
+ var sb = new StringBuilder();
+ sb.Append("Scene Size Report");
+
+ sb.Append($"{H(Path.GetFileName(r.ScenePath))}
");
+ sb.Append($"{H(r.ScenePath)} | {Fmt(t)} ({t:N0} bytes) | {r.Docs.Count:N0} objects | {r.Docs.Count(d => d.ClassId == 1):N0} GameObjects | {DateTime.Now:yyyy-MM-dd HH:mm}
");
+
+ if (r.Hints.Count > 0)
+ {
+ sb.Append("Hints
");
+ foreach (var h in r.Hints) sb.Append("- " + H(h) + "
");
+ sb.Append("
");
+ }
+ if (!r.IsText) return sb.Append("").ToString();
+
+ Table(sb, "By type", "Type", r.ByType.Take(r.TopN).Select(e => (e.Key, e.Count, e.Bytes, (string)null)), t);
+ Table(sb, "By field (Type.field, summed over all objects)", "Field", r.ByField.Select(e => (e.Key, e.Count, e.Bytes, (string)null)), t);
+ Table(sb, "Largest single objects", "Object", r.TopDocs.Select(d => (
+ $"{d.DisplayType} '{d.Name}' &{d.FileId}", 1, d.Bytes,
+ string.Join(", ", d.Fields.OrderByDescending(f => f.Value).Take(4).Select(f => $"{f.Key} {Fmt(f.Value)}")))), t);
+ Table(sb, "Largest GameObjects (object + own components)", "Path", r.TopObjects.Select(n => (n.Path, 1, n.OwnBytes, (string)null)), t);
+ Table(sb, "Largest hierarchy subtrees", "Path", r.TopSubtrees.Select(n => (n.Path, CountNodes(n), n.SubtreeBytes, (string)null)), t);
+
+ sb.Append($"Other
Scene settings / unowned: {Fmt(r.UnownedBytes)} | YAML header: {Fmt(r.HeaderBytes)}
");
+ return sb.Append("