AssetsBundes包的使用

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEngine.UI;
public class LoadAssetsBundles : MonoBehaviour {
    string path1;
    string path2 ;
    string path3;
    Texture texture;
    void Start()
    {
        //需要在这里赋值
        path1 = "Assets/AssetsBundles/object.unity3d";//包内容一个Cube和Sphere预设   包名+后缀
        path2 = "Assets/AssetsBundles/material.unity3d";//包内容一个材质球
        path3 = "Assets/AssetsBundles/texture.assetsbundle";//包内容一张名为timg的图片
        StartCoroutine(StartAB());
      
    }

    // Use this for initialization
    IEnumerator StartAB () {

        //第一种方式从内存中加载AB包
        #region 异步加载
        //异步加载
        AssetBundleCreateRequest request = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path1));
        yield return request;
        //加载共同依赖资源,如贴图  材质
        AssetBundleCreateRequest request2 = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path2));
        yield return request2;

        AssetBundleCreateRequest request3 = AssetBundle.LoadFromMemoryAsync(File.ReadAllBytes(path3));
        yield return request3;

        AssetBundle ab = request.assetBundle;//获取包
        AssetBundle ab2 = request2.assetBundle;//获取包
        AssetBundle ab3 = request3.assetBundle;

        //使用里面的资源
        GameObject cube = ab.LoadAsset<GameObject>("Cube");
        GameObject sphere = ab.LoadAsset<GameObject>("Sphere");

         texture = ab3.LoadAsset<Texture>("timg");//获取图片

        Debug.Log(texture.width + texture.height+texture.name);
        //场景中新建一个RawImage
        GameObject.Find("Canvas/RawImage").GetComponent<RawImage>().texture = texture;

        Instantiate(cube);
        Instantiate(sphere);
        #endregion

        #region 同步加载

        #endregion

        StopAllCoroutines();
    }

    // Update is called once per frame
    void Update () {
       
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值