一般在实际项目开发过程中,UI 界面制作好以后会拖拽成为一个 Prefab 资源,和 Scene 场景分离,在需要加载到该场景的时候,再动态的加载显示UI界面
动态加载方法:
第一步:将制作好的UI面板拖拽成为一个Prefab资源,放到Resources文件夹下
第二步,编写脚本挂在 UI Root 上即可
- Resources.Load<T>(path):从资源文件夹中获取T,其中T的路径为path
- NGUITools.AddChild(父对象, 子对象):NGUI 提供的一个实例化物体,设置子物体的一个内置函数
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UIAdd : MonoBehaviour
{
private GameObject myUI;
void Start()
{
myUI = Resources.Load<GameObject>("Sprite");
NGUITools.AddChild(gameObject, myUI);
}
}