https://www.cnblogs.com/lixiang-share/p/4639288.html
Resources.Load:使用这种方式加载资源,首先需要下Asset目录下创建一个名为Resources的文件夹,这个命名是U3D规定的方式,然后把资源文件放进去,
当然也可以在Resources中再创建子文件夹,当然在代码加载时需要添加相应的资源路径,下面是一个简demo,两个预设,Cube和Sphere,
其中Cube放在Resource中的Prebs中,而Sphere放在Resources跟目录下,下面分别实现Resources.Load资源的加载
复制代码
using UnityEngine;
using System.Collections;
public class LoadResDemo : MonoBehaviour {
private string cubePath = "Prebs/MyCubePreb";
private string spherePath = "MySpherePreb";
void Start () {
//把资源加载到内存中
Object cubePreb = Resources.Load(cubePath, typeof(GameObject));
//用加载得到的资源对象,实例化游戏对象,实现游戏物体的动态加载
GameObject cube = Instantiate(cubePreb) as GameObject;
//以下同理实现Sphere的动态实例化
//把资源加载到内存中
Object spherePreb = Resources.Load(spherePath, typeof(GameObject));
//用加载得到的资源对象,实例化游戏对象,实现游戏物体的动态加载
GameObject sphere = Instantiate(spherePreb) as GameObject;
}
void Update () {
}
}
复制代码
将上面的脚本附加到某个游戏对象上,在运行游戏时就可以看到场景中动态创建的上面的游戏对象了