SceneDumper

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Scene dumper script, first version.)
 
m (Text replace - "</csharp>" to "</syntaxhighlight>")
 
(3 intermediate revisions by 2 users not shown)
Line 8: Line 8:
 
* comparing the scene from before and during playback to see what objects and components are created dynamically
 
* comparing the scene from before and during playback to see what objects and components are created dynamically
 
* finding missing scripts in your scene (search the dump for "(null)")
 
* finding missing scripts in your scene (search the dump for "(null)")
 +
 +
For a more full-featured version of this script, including a custom editor window for better IDE integration,
 +
see http://zeroandone.ca/unity.
  
 
== Usage ==
 
== Usage ==
Line 15: Line 18:
 
Potential useful extensions to this script include:
 
Potential useful extensions to this script include:
 
* a GUI for selecting what to dump
 
* a GUI for selecting what to dump
 +
* control over location of output file
 
* use of System.Reflection to dump the fields and properties of components
 
* use of System.Reflection to dump the fields and properties of components
  
 
== C# Script - SceneDumper.cs ==
 
== C# Script - SceneDumper.cs ==
<csharp>// SceneDumper.cs
+
<syntaxhighlight lang="csharp">// SceneDumper.cs
 
//
 
//
 
// History:
 
// History:
Line 72: Line 76:
 
     }
 
     }
 
}
 
}
</csharp>
+
</syntaxhighlight>

Latest revision as of 20:45, 10 January 2012

Contents

[edit] Description

This script dumps the scene hierarchy to a text file. It dumps all game object recursively, starting with the selected objects. The text dump contains object names, hierarchy, and the names of all components.

This is useful for things like:

  • quick review of your scene structure
  • comparing the state of the scene from day to day or milestone to milestone
  • comparing two scenes
  • comparing the scene from before and during playback to see what objects and components are created dynamically
  • finding missing scripts in your scene (search the dump for "(null)")

For a more full-featured version of this script, including a custom editor window for better IDE integration, see http://zeroandone.ca/unity.

[edit] Usage

Save it as "SceneDumper.cs" below your project's Editor folder. You will now have a Debug menu containing a "Dump Scene" menu item. Select one or more objects in your scene hierarchy and select the menu item.

[edit] Future Work

Potential useful extensions to this script include:

  • a GUI for selecting what to dump
  • control over location of output file
  • use of System.Reflection to dump the fields and properties of components

[edit] C# Script - SceneDumper.cs

// SceneDumper.cs
//
// History:
// version 1.0 - December 2010 - Yossarian King
 
using StreamWriter = System.IO.StreamWriter;
 
using UnityEngine;
using UnityEditor;
 
public static class SceneDumper
{
    [MenuItem("Debug/Dump Scene")]
    public static void DumpScene()
    {
        if ((Selection.gameObjects == null) || (Selection.gameObjects.Length == 0))
        {
            Debug.LogError("Please select the object(s) you wish to dump.");
            return;
        }
 
        string filename = @"C:\unity-scene.txt";
 
        Debug.Log("Dumping scene to " + filename + " ...");
        using (StreamWriter writer = new StreamWriter(filename, false))
        {
            foreach (GameObject gameObject in Selection.gameObjects)
            {
                DumpGameObject(gameObject, writer, "");
            }
        }
        Debug.Log("Scene dumped to " + filename);
    }
 
    private static void DumpGameObject(GameObject gameObject, StreamWriter writer, string indent)
    {
        writer.WriteLine("{0}+{1}", indent, gameObject.name);
 
        foreach (Component component in gameObject.GetComponents<Component>())
        {
            DumpComponent(component, writer, indent + "  ");
        }
 
        foreach (Transform child in gameObject.transform)
        {
            DumpGameObject(child.gameObject, writer, indent + "  ");
        }
    }
 
    private static void DumpComponent(Component component, StreamWriter writer, string indent)
    {
        writer.WriteLine("{0}{1}", indent, (component == null ? "(null)" : component.GetType().Name));
    }
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox