Unity-资源加载(Resources,AssetBundle)

Resources类

使用Resources加载资源,资源一定要放到Resources文件夹下,支持名字和路径加载。
1.可以有多个Resources文件夹同时存在。
2.Resources下的资源在打包后是加密压缩状态,运行时不可修改。
3.加载方法 : Load<类型>(名称或路径),名称不需要加后缀。

示例

public class TeachRes : MonoBehaviour {

    void Start () {
        //加载预制体,可以有路径
        GameObject prefab = Resources.Load<GameObject>("Prefabs/UI/Cube");
        GameObject.Instantiate(prefab);

        //加载精灵
        Sprite sprite = Resources.Load<Sprite>("SkyTileSprite");
        GameObject go = new GameObject("sp");
        go.AddComponent<SpriteRenderer>().sprite = sprite;

        //加载Texture
        Texture tex = Resources.Load<Texture>("SkyTileSprite");
        GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
        cube.GetComponent<MeshRenderer>().material.mainTexture = tex;

        //加载Texture2D -> sprite
        Texture2D tex2d = Resources.Load<Texture2D>("SkyTileSprite");
        Sprite sp = Sprite.Create(tex2d, new Rect(0, 0, tex2d.width, tex2d.height), new Vector2(0.5f, 0.5f));

        GameObject spriteRender = new GameObject("go");
        go.AddComponent<SpriteRenderer>().sprite = spriteRender ;
        //go.AddComponent<Image>().sprite = spriteRender ;

        //加载声音类型
        AudioClip clip = Resources.Load<AudioClip>("mp3");
        //加载视频类型
        MovieTexture movie = Resources.Load<MovieTexture>("movie");

        //Unity的GC
        Resources.UnloadAsset(tex);
        //释放unity认为不使用的资源
        Resources.UnloadUnusedAssets();
    }
}

WWW类

支持file://,http://,https://三种协议。
可以读取本地或者网络的资源。

示例

public class TeachWWW: MonoBehaviour {

    IEnumerator Start () {
        //本地路径
        //string url = @"file://D:\SkyTileSprite.png";
        //网络地址
        string url = @"http://192.168.11.3/SkyTileSprite.png";

        WWW www = new WWW(url);
        yield return www;

        Texture2D tex2d = www.texture;
        Sprite sprite = Sprite.Create(tex2d, new Rect(0, 0, tex2d.width, tex2d.height), new Vector2(0.5f, 0.5f));

        GameObject go = new GameObject("sprite");
        go.AddComponent<SpriteRenderer>().sprite = sprite;
        //释放bundle
        www.assetBundle.Unload(false);
    }
}

如果资源包是在线的可以使用www下载或者直接加载
WWW加载AssetBundle

public class WWWLoadAB : MonoBehaviour {

    IEnumerator Start () {
        string url = "file://"+Application.streamingAssetsPath+"/+"cube.unity3d";

        WWW www = new WWW(url);
        yield return www;

        AssetBundle bundle= www.assetBundle;
        GameObject prefab = bundle.LoadAsset<GameObject>("Cube");
        Instantiate(prefab );
    }
}

如果是在热更新资源的项目中,一般是把资源先下载到本地的persistentDataPath,然后使用AssetBundle.LoadFromFile方法加载并处理依赖资源。

AssetBundle类 加载资源和依赖

public class LoadAssetbundle : MonoBehaviour
{
    void Start()
    {
        GameObject prefab = LoadAsset("cube.unity3d","cube");
        GameObject.Instantiate(prefab);
    }

    //加载bundle下的资源(包含依赖处理)
    GameObject LoadAsset(string abName,string assetName)
    {
        // 1.加载Manifest总配置文件  
        string path = Application.streamingAssetsPath + "/StreamingAssets";
        AssetBundle manifestBundle = AssetBundle.LoadFromFile(path);
        AssetBundleManifest manifest = manifestBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");

        // 2.获取ab包的依赖文件列表  
        string[] depends = manifest.GetAllDependencies(abName);

        // 3.加载所有的依赖ab包到内存
        foreach (var depName in depends)
        {
            string depPath = Application.streamingAssetsPath + "/" + depName;
            AssetBundle depBundle = AssetBundle.LoadFromFile(depPath);
        }

        // 4.加载资源ab包
        path = Application.streamingAssetsPath + "/"+ abName;
        AssetBundle bundle = AssetBundle.LoadFromFile(path);

        // 5.返回ab包中特定预制体
        return bundle.LoadAsset<GameObject>(assetName);
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,819评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,118评论 6 342
  • 原文地址:http://gad.qq.com/article/detail/7180936作者:Loki+XUni...
    重装机霸阅读 12,624评论 0 41
  • Ubuntu的发音 Ubuntu,源于非洲祖鲁人和科萨人的语言,发作 oo-boon-too 的音。了解发音是有意...
    萤火虫de梦阅读 99,978评论 9 468
  • 今天是忧郁的 灰蒙蒙的天无声的细雨 听着楼上五音不全口齿不清的歌声 闻着从邻居家里飘出来的咖啡香味 一会烦躁不安的...
    叹叹叹息阅读 1,891评论 0 1

友情链接更多精彩内容