Unity3D 从代码中加载资源

来源:互联网 发布:淘宝手机触摸屏 编辑:程序博客网 时间:2024/05/22 06:11

首先把需要加载的资源做成预设,资源路径必须放在Resources文件夹下



第一种:通过资源路径加载

<pre name="code" class="csharp">private string cubePath = "Model/BuJianModel_1_Models";void Start () {LoadResource (testPath);}public void LoadResource(string path) {//把资源加载到内存中Object objPreb = Resources.Load(path, typeof(GameObject));//用加载得到的资源对象,实例化游戏对象,实现游戏物体的动态加载GameObject gameObj = Instantiate(objPreb) as GameObject;}




第二种:用拖动的方式


public Object[] per;void Start () {for (int i = 0; i < per.Length; i++) {GameObject gameObj = Instantiate(per[i]) as GameObject;}}

之后还需要对生成的资源设置父对象与位置信息



0 0
原创粉丝点击