Created
June 10, 2012 18:35
-
-
Save azeitler/2906852 to your computer and use it in GitHub Desktop.
AddChildInAsset and AddChildInAssetWithComponent<T>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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