Skip to content

Instantly share code, notes, and snippets.

@azeitler
Created June 10, 2012 18:56
Show Gist options
  • Save azeitler/2906921 to your computer and use it in GitHub Desktop.
Save azeitler/2906921 to your computer and use it in GitHub Desktop.
RemoveChildInAsset
public static void RemoveChildInAsset (GameObject child)
{
GameObject prefab = PrefabUtility.FindPrefabRoot (child);
RemoveChildInAsset (prefab, child);
}
public static void RemoveChildInAsset (GameObject prefab, GameObject child)
{
try {
string oldName = child.name;
child.name = System.Guid.NewGuid ().ToString ();
GameObject instance = (GameObject)PrefabUtility.InstantiatePrefab (prefab);
GameObject oldChild = instance.transform.FindChild (child.name).gameObject;
GameObject.DestroyImmediate (oldChild);
PrefabUtility.ReplacePrefab (instance, prefab, ReplacePrefabOptions.ConnectToPrefab);
prefab.MakeDirty ();
AssetDatabase.SaveAssets ();
AssetDatabase.Refresh ();
GameObject.DestroyImmediate (instance);
Debug.Log ("Child Removed in Asset: " + oldName);
} catch (System.Exception ex) {
Debug.Log ("Could not remove child in Asset: " + ex.ToString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment