usingUnityengine;usingSystem.Collections; Public classLoadprefab:monobehaviour {//declare a reference to this object, and Uiroot will create a sub-object under this object later . PublicGameobject Uiroot; //declares the name of the child object preset to be loaded stringPrefabname ="Template"; voidStart () {if(Uiroot! =NULL) { //the preset is loaded into memory according to the path as a gameobject existsGameobject go = Resources.load ("ui/"+ prefabname) asGameobject; //hanging sub-objects using the Nguitools.addchild methodGameobject NEWOBJ =Nguitools.addchild (Uiroot,go); /*Method 2 * Gameobject NewObj2 = Instantiate (go, uiRoot.transform.position, uiRoot.transform.rotation) as Gameobj ect NewObj2.transform.parent = Uiroot.transform; */ //the name of the new object can be printed outDebug.Log ("the newborn becomes a sub-object called:"+newobj.name); } } //Update is called once per frame voidUpdate () {}}
Set an object as a sub-object of another object----Nguitools.addchild () method