Skip to content

Commit

Permalink
Merge pull request #543 from anatawa12/pick-gc-debug
Browse files Browse the repository at this point in the history
pick gc debug
  • Loading branch information
anatawa12 authored Oct 9, 2023
2 parents 0b2d858 + 45c46bd commit 9cbbc66
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-PRERELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog].

## [Unreleased]
### Added
- Feature for debugging GC Objects `#543`

### Changed

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog].

## [Unreleased]
### Added
- Feature for debugging GC Objects `#543`

### Changed

Expand Down
59 changes: 59 additions & 0 deletions Editor/Processors/TraceAndOptimize/FindUnusedObjectsProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEditor;
using UnityEngine;
using VRC.Dynamics;
Expand Down Expand Up @@ -253,6 +254,64 @@ private void CollectDataForGc()

foreach (var component in _session.GetComponents<Component>())
component.gameObject.GetOrAddComponent<GCData>().data.Add(componentDataMap[component]);
_session.GetRootComponent<Transform>().gameObject.AddComponent<GCDataRoot>();
}

class GCDataRoot : MonoBehaviour
{
}

[CustomEditor(typeof(GCDataRoot))]
class GCDataRootEditor : Editor
{
public override void OnInspectorGUI()
{
if (GUILayout.Button("Copy All Data"))
{
GUIUtility.systemCopyBuffer = CreateData();
}

if (GUILayout.Button("Save All Data"))
{
var path = EditorUtility.SaveFilePanel("DebugGCData", "", "data.txt", "txt");
if (!string.IsNullOrEmpty(path))
{
System.IO.File.WriteAllText(path, CreateData());
}
}

string CreateData()
{
var root = ((Component)target).gameObject;
var collect = new StringBuilder();
foreach (var gcData in root.GetComponentsInChildren<GCData>())
{
collect.Append(RuntimeUtil.RelativePath(root, gcData.gameObject)).Append(":\n");

foreach (var componentData in gcData.data.Where(componentData => componentData.component))
{
collect.Append(" ").Append(componentData.component.GetType().Name).Append(":\n");
collect.Append(" ActiveNess: ").Append(componentData.activeness).Append('\n');
collect.Append(" Dependencies:\n");
var list = new List<string>();
foreach (var dependencyInfo in componentData.dependencies.Where(x => x.component))
{
var path = RuntimeUtil.RelativePath(root, dependencyInfo.component.gameObject);
var types = dependencyInfo.component.GetType().Name;
list.Add($"{path}({types})({dependencyInfo.type},{dependencyInfo.flags})");
}

list.Sort();
foreach (var line in list)
collect.Append(" ").Append(line).Append("\n");
}

collect.Append("\n");
}

return collect.ToString();
}
}
}

class GCData : MonoBehaviour
Expand Down

0 comments on commit 9cbbc66

Please sign in to comment.