AssetBundle打包和加载_非协程异步加载AssetBundle(六)

 

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

public class LoadAssetBundle : MonoBehaviour {

	string sceneABName = "main.unity3d";

	string assetPath;
	/// <summary>
	/// ab包的相对路径
	/// </summary>
	//string assetBundleName = "test.assetbundle";
	string assetBundleName = "test.assetbundle";

	/// <summary>
	/// 需要加载的ab包里面的预设名字
	/// </summary>
	string loadPrefabName = "Cube";

	private AssetBundleCreateRequest _request;



	/// <summary>
	/// 是否加载完成
	/// </summary>
	bool isDone = false;

	/// <summary>
	/// 加载ab资源
	/// </summary>
	/// <param name="abFullPath">ab包的绝对路径</param>
	/// <param name="completeCallBack">加载完成回调</param>
	public void LoadAbAssetAsync(string abFullPath,Action<AsyncOperation>completeCallBack)
	{
		_request = AssetBundle.LoadFromFileAsync(abFullPath);
		if (_request == null)
		{
			Debug.Log("AssetBundleCreateRequest is null ");
			return;
		}
		if (_request != null) _request.completed += completeCallBack;
  
	}

	private void Update()
	{
		if(!isDone&& _request!=null)
		{
			Debug.Log(_request.progress);
		}
	}

	private void Request_completed(AsyncOperation obj)
	{
		AssetBundle asset = _request.assetBundle;
		Instantiate(asset.LoadAsset(loadPrefabName));
		
		Debug.Log("Request_completed");
		isDone = true;
		_request = null;
		asset.Unload(false);
		asset = null;

	}

	private void Start()
	{
		assetPath = Application.dataPath + "/../MyAssetBundles/";

		//加载普通ab资源
		//LoadAbAssetAsync(assetPath + assetBundleName, Request_completed);

		//加载ab场景
		LoadAbAssetAsync(assetPath + sceneABName, (async) =>
		{

			SceneManager.LoadScene("Main");
		});

	}
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值