How to using script get the children gamobjects on Edit time

I have a gameobject,it has some children gameobjects,

12171-floor.jpg

i want to get the children gameobjects,Bone,Bone_001,Bone_002…,and random rotate every one, on edit time,i just don’t know ,when i attach the script to the gameobject “Floor 001”,how to get that children gameobjects,save that in a gameobject array.i using javascript,didn’t know more about c#.

Yes, it’s absolutely possible to run a script or call functions in edit mode. I am not going to show you how to run scripts in edit mode as I believe this should only be used in specific circumstances, but I will show you ContextMenu : Unity - Scripting API: ContextMenu

This still needs some setup if you are using this in your script and not in the Editor folder. Here is an example :

#pragma strict
 
#if UNITY_EDITOR
@ContextMenu( "Run from ContextMenu" )
function RunfromContextMenu()
{
    Debug.Log( "Running from ContextMenu" );
    Startup();
}
#endif
 
var helloString : String = "Im being run from the Context Menu";
 
function Startup()
{
    Debug.Log( helloString );
}

So how does it work? Copy this into a new script, make a new scene and attach script to an empty gameObject. Now in the Inspector, right-click on the script component, then a drop-down selection box appears. The normal options are Reset Remove Component and now there should be another one called Run from Context Menu

alt text

http://docs.unity3d.com/Documentation/Images/manual/Inspector-3.jpg

Click on Run from Context Menu, then check the console for the debugs in the script. That’s it.


So how to populate an array using the ContextMenu method?

This script will run from ContextMenu to populate an array, then the scene can be saved with the array populated.

#pragma strict
import System.Collections.Generic;

#if UNITY_EDITOR
@ContextMenu( "Find Children From Context Menu" )
function FindChildrenFromContextMenu() 
{
    Debug.Log("Finding Children from ContextMenu");
	FindChildren();
}
#endif


public var allChildren : GameObject[];

private var children = new List.< GameObject >();


function FindChildren() 
{
	var allTransforms : Transform[] = transform.GetComponentsInChildren.< Transform >();
	
	// Add every immediate child only of the transform to the list above as a gameobject
	for ( var child : Transform in allTransforms )
	{
		// make sure this isn't the parent/root object
		if ( child != transform )
		{
			children.Add( child.gameObject );
		}
	}
	
	allChildren = children.ToArray();
}