How do I iterate over all scene objects from an editor script?

What is the best way to iterate over all scene objects from an editor script?

Supose I want to make a script extract some scene information.

Is this the best way?

  object[] obj = GameObject.FindSceneObjectsOfType(typeof (GameObject));
  foreach (object o in obj)
  {
       GameObject g = (GameObject) o;
       Debug.Log(g.name);
  }

Could GameObject.FindSceneObjectsOfType pick objects from other editors or other objects that are not in the current scene graph?

That should work fine. In contrast to what you might expect, Unity has no "root node" which you can ask for its children. You have to do what you do now.

Now possible in Unity via.

 // get root objects in scene
 List<GameObject> rootObjects = new List<GameObject>();
 Scene scene = SceneManager.GetActiveScene();
 scene.GetRootGameObjects( rootObjects );
 
 // iterate root objects and do something
 for (int i = 0; i < rootObjects.Count; ++i)
 {
     GameObject gameObject = rootObjects[ i ];
     doSomethingToHierarchy( gameObject );
 }

GameObject doesn't actually contain a definition for FindSceneObjectsOfType(). You'll want to use

Type[] myVar = FindObjectsOfType (typeof(Type))

instead. For all GameObjects it's just

object[] obj = GameObject.FindObjectsOfType(typeof (GameObject));
  foreach (object o in obj)
  {
       GameObject g = (GameObject) o;
       Debug.Log(g.name);
  }

The SceneDumper script on the wiki will iterate all the the selected objects and their children. Currently it just prints the object hierarchy and the names of each object's components, but it could be modified to print all the component properties, filter by type, etc.

Given a game object, you can find its top-most ancestor in the scene tree using Transform.root -- i.e. gameobj.transform.root.gameObject. But note that there can be multiple "root" nodes in a scene.