码迷,mamicode.com
首页 > 其他好文 > 详细

AssetBundle学习笔记(2)

时间:2017-10-05 14:14:29      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:学习   sys   加载   deb   button   sha   实例化   log   oid   

二.AssetBundle的加载

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LoadFromAB : MonoBehaviour {

    // Use this for initialization
    void Start ()
    {
        AssetBundle ab2 = AssetBundle.LoadFromFile("AssetBundles/cube.unity3d");
        AssetBundle ab = AssetBundle.LoadFromFile("AssetBundles/share.unity3d");        
        GameObject wallprefab = ab2.LoadAsset<GameObject>("Cube");
        Instantiate(wallprefab);
    }
    
    // Update is called once per frame
    void Update () {
        
    }
}

在使用AssetBundle包的时候先把资源加载到内存当中。官方文档中讲述的是,先加载贴图之类的包,然后加载物体资源包,但是经过实际测试,这个加载实际上是不分先后的,这个就略屌了。为了测试是否在物体实例化出来以后贴图是否能够找到并打上,我们改写代码测试一下。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LoadFromAB : MonoBehaviour {

    // Use this for initialization
    void Start ()
    {
        AssetBundle ab2 = AssetBundle.LoadFromFile("AssetBundles/cube.unity3d");        
        GameObject wallprefab = ab2.LoadAsset<GameObject>("Cube");
        Instantiate(wallprefab);
        //AssetBundle.LoadFromFile("AssetBundles/share.unity3d");
    }
    
    // Update is called once per frame
    void Update ()
    {
        if (Input.GetMouseButtonDown(0))
        {
            AssetBundle.LoadFromFile("AssetBundles/share.unity3d");
            Debug.Log("加载成功");
        }
    }
}

结果就是在鼠标左键按下的时候,物体贴图成功的加载了出来。

但是为了避免发生不可预料的结果,建议还是按照官方文档中加载步骤进行

 

AssetBundle学习笔记(2)

标签:学习   sys   加载   deb   button   sha   实例化   log   oid   

原文地址:http://www.cnblogs.com/UnityLittleBird/p/7629091.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!