using UnityEngine; using System.Collections; public class LoadPrefab : MonoBehaviour { //声明UIRoot这个物体的引用,待会儿将会在这个物体下生成子物体 public GameObject uiRoot; //声明要加载的子物体预设的名称 string prefabName = "Template"; void Start () { if (uiRoot != null) { //根据路径将预设加载进内存作为一个GameObject存在 GameObject go = Resources.Load("UI/" + prefabName) as GameObject; //使用NGUITools.AddChild方法挂子物体 GameObject newObj = NGUITools.AddChild(uiRoot,go); /*方法2 * GameObject newObj2 = Instantiate(go, uiRoot.transform.position, uiRoot.transform.rotation) as GameObject; newObj2.transform.parent = uiRoot.transform; */ //可以将新物体的名称打印出来 Debug.Log("新生成了一个子物体名叫:"+newObj.name); } } // Update is called once per frame void Update () { } }
时间: 2024-10-05 05:07:44