private void InitHierarchyView(VsScene scene, System.Collections.Generic.List <VsGameObject> objs) { hierarchyView.Items.Clear(); TreeViewItem sceneItem = new TreeViewItem(); sceneItem.Header = scene.name; foreach (var obj in objs) { MyTreeViewItem objItem = new MyTreeViewItem(); objItem.Header = obj.name; objItem.doubleClicked += () => Inspect(objItem); if (obj.childCount > 0) { objItem.CreateDummy(); objItem.Expanded += ObjItem_Expanded; } sceneItem.Items.Add(objItem); } hierarchyView.Items.Add(sceneItem); }
/// <summary> /// Refresh Unity Hierarchy /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void Refresh(object sender, EventArgs e) { ThreadHelper.ThrowIfNotOnUIThread(); if (CheckNotInBreakMode()) { return; } EnvDTE.TextSelection selection = commandWindow?.Selection as EnvDTE.TextSelection; if (selection == null) { ShowMessage( "Error!", "Cannot find command window automatically\nPlease manually activate it and try again.", OLEMSGICON.OLEMSGICON_CRITICAL); return; } if (!isInitSceneAndRootObj) { dte.Debugger.ExecuteStatement(Statement: "UnityEngine.SceneManagement.Scene scene;"); dte.Debugger.ExecuteStatement(Statement: "UnityEngine.GameObject[] objs;"); isInitSceneAndRootObj = true; } // Get Active Scene string sceneInfoStr = GetDebuggerExecutionOutput(Statement: "scene = UnityEngine.SceneManagement.SceneManager.GetActiveScene();", TreatAsExpression: true, selection); VsScene scene = new VsScene(sceneInfoStr); // Get Root GameObjects string objInfoStr = GetDebuggerExecutionOutput(Statement: "objs = scene.GetRootGameObjects();", TreatAsExpression: true, selection); string[] gameObjectNames = OuputParseUtil.GetGameObjectNamesFromInfo(objInfoStr); // Get ChildCount of each GameObjects int saveLineNum; selection.SaveLinePosition(out saveLineNum); for (int i = 0; i < scene.rootCount; ++i) { dte.Debugger.ExecuteStatement(Statement: $"objs[{i}].transform.childCount;", TreatAsExpression: true); } string childCountInfoStr = selection.GetTextBetweenCurPosToLine(saveLineNum); string[] childCountsStr = childCountInfoStr.SplitAndTrim("\r\n", StringSplitOptions.RemoveEmptyEntries); var objs = new System.Collections.Generic.List <VsGameObject>(); for (int i = 0; i < gameObjectNames.Length; ++i) { VsGameObject go = new VsGameObject(); go.name = gameObjectNames[i]; go.childCount = int.Parse(childCountsStr[i]); objs.Add(go); } InitHierarchyView(scene, objs); selection.EndOfDocument(); }