Skip to content

Instantly share code, notes, and snippets.

@azeitler
Created June 10, 2012 18:35
Show Gist options
  • Save azeitler/2906852 to your computer and use it in GitHub Desktop.
Save azeitler/2906852 to your computer and use it in GitHub Desktop.
AddChildInAsset and AddChildInAssetWithComponent<T>
public static GameObject AddChildInAsset (GameObject prefab, string name = "NewChild")
{
GameObject instance = (GameObject)PrefabUtility.InstantiatePrefab (prefab);
GameObject child = AddChild (instance, System.Guid.NewGuid ().ToString ());
PrefabUtility.ReplacePrefab (instance, prefab, ReplacePrefabOptions.ConnectToPrefab);
GameObject newChild = prefab.transform.FindChild (child.name).gameObject;
GameObject.DestroyImmediate (instance);
newChild.name = name;
prefab.MakeDirty ();
AssetDatabase.SaveAssets ();
AssetDatabase.Refresh ();
Debug.Log ("Child Created in Asset: " + newChild.name);
return newChild;
}
public static T AddChildInAssetWithComponent<T> (GameObject prefab, string name = "NewChild") where T : Component
{
GameObject newChild = AddChildInAsset (prefab, name);
T newComponent = newChild.AddComponent<T> ();
return newComponent;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment