assetbundle标签_标签 AssetBundle 下的文章

using System;

using System.Collections;

using System.Collections.Generic;

using System.Diagnostics;

using UnityEngine;

using Debug = UnityEngine.Debug;

public class ResTest2 : MonoBehaviour

{

private AssetBundleManifest abm;

private List absList = new List();

private List abs = new List();

// Use this for initialization

void Start()

{

StartCoroutine(LoadABM(ABMLoaded));

}

IEnumerator LoadABM(Action action)

{

WWW www = new WWW(Application.streamingAssetsPath + "/windows.t");

yield return www;

if (string.IsNullOrEmpty(www.error))

{

abm = www.assetBundle.LoadAsset("AssetBundleManifest");

}

www.Dispose();

www = null;

if (action != null)

{

action();

}

}

void ABMLoaded()

{

Debug.Log("ABM加载完成");

LoadAllAB();

}

#region 测试整体的预加载耗时

Stopwatch load = new Stopwatch();

#endregion

///

/// 没有依赖文件的AB

///

Dictionary lABs = new Dictionary();

///

/// 有依赖文件的AB

///

Dictionary dABs = new Dictionary();

///

/// 加载所有AB文件

///

private void LoadAllAB()

{

#region 测试整体的预加载耗时

load.Start();

#endregion

string[] abNames = abm.GetAllAssetBundles();

Debug.LogWarning("AB总数:" + abNames.Length);

foreach (string abName in abNames)

{

string[] dNames = abm.GetAllDependencies(abName);

if (dNames != null && dNames.Length > 0)

{

AB ab = new AB();

ab.name = abName;

foreach (string dName in dNames)

{

AB dAB = new AB();

dAB.name = dName;

ab.dABs.Add(dAB);

}

if (!dABs.ContainsKey(abName))

{

dABs.Add(abName, ab);

}

}

else

{

AB ab = new AB();

ab.name = abName;

if (!lABs.ContainsKey(abName))

{

lABs.Add(abName, ab);

}

}

}

Debug.LogWarning("无依赖:" + lABs.Count);

Debug.LogWarning("有依赖:" + dABs.Count);

labCount = lABs.Count;

foreach (AB ab in lABs.Values)

{

StartCoroutine(LoadLAB(ab, OnLLoaded));

}

}

private void OnLLoaded()

{

Debug.LogWarning("无依赖的AB加载完毕");

labCount = dABs.Count;

foreach (AB ab in dABs.Values)

{

StartCoroutine(LoadLAB(ab, OnDLoaded));

}

}

private void OnDLoaded()

{

#region 测试预加载整体耗时

load.Stop();

Debug.LogWarning("加载耗时:" + load.ElapsedMilliseconds);

load.Reset();

#endregion

Debug.LogWarning("有依赖的AB加载完毕");

LoadABI("lobby.ui.lobbyroot", null);

}

///

/// 无依赖的ab数

///

private int labCount = 0;

///

/// 已加载的无依赖ab数

///

private int lloadedCount = 0;

///

/// 加载无依赖的AB

///

///

///

///

private IEnumerator LoadLAB(AB ab, Action action)

{

WWW www = new WWW(Application.streamingAssetsPath + "/" + ab.name + ".t");

yield return www;

if (string.IsNullOrEmpty(www.error))

{

AssetBundle assetBundle = www.assetBundle;

ab.ab = assetBundle;

ab.isLoaded = true;

www.Dispose();

www = null;

lloadedCount += 1;

if (lloadedCount == labCount)

{

labCount = 0;

lloadedCount = 0;

if (action != null)

{

action();

}

}

}

else

{

Debug.LogWarning("www.error:" + www.error);

}

}

private void LoadABI(string abName, Action action)

{

#region 测试整体的预加载耗时

load.Start();

#endregion

AB ab = null;

dABs.TryGetValue(abName, out ab);

if (ab != null)

{

UnityEngine.Object[] assets = ab.ab.LoadAllAssets();

GameObject go = Instantiate(assets[0]) as GameObject;

}

#region 测试预加载整体耗时

load.Stop();

Debug.LogWarning("加载耗时:" + load.ElapsedMilliseconds);

load.Reset();

#endregion

//WWW www = new WWW(Application.streamingAssetsPath + "/" + abName + ".t");

//yield return www;

//if (string.IsNullOrEmpty(www.error))

//{

// AssetBundle ab = www.assetBundle;

// abs.Add(ab);

// UnityEngine.Object[] assets = ab.LoadAllAssets();

// www.Dispose();

// www = null;

// absList.Add(abName);

// GameObject go = Instantiate(assets[0]) as GameObject;

// yield return new WaitForEndOfFrame();

//}

}

private void OnApplicationQuit()

{

for (int i = 0; i < abs.Count; i++)

{

abs[i].Unload(true);

abs[i] = null;

}

abs.Clear();

absList.Clear();

Resources.UnloadUnusedAssets();

}

}

using System.Collections.Generic;

using UnityEngine;

///

/// AssetBundle Item

///

public class AB

{

///

/// abname

///

public string name = "";

public AssetBundle ab = null;

///

/// 依赖文件

///

public List dABs = new List();

///

/// 是否已加载

///

public bool isLoaded = false;

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值