说明:异步加载lua的bundle,会优先加载cache目录下bundle(一般更新的资源都在cache下)

using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using LuaInterface; public class LuaBundleLoader : MonoBehaviour { public delegate void DelegateLoading(int idx, int total, string bundleName, string path);
public delegate void DelegateLoadOver(); //正在加载中回掉
public DelegateLoading OnLoading; //加载完成回掉
public DelegateLoadOver OnLoadOver; //总共要加载的bundle个数
private int mTotalBundleCount = 0; //当前已加载的bundle个数
private int mBundleCount = 0; #if UNITY_5
public void LoadBundle(string dir, string bundleName)
{
StartCoroutine(LoadBundles(dir, bundleName));
}
#else
public void LoadBundle(string dir, List<string> bundleList)
{
StartCoroutine(LoadBundles(dir, bundleList));
}
#endif
IEnumerator CoLoadBundle(string name, string path)
{
using (WWW www = new WWW(path))
{
if (www == null)
{
Debugger.LogError(name + " bundle not exists");
yield break;
} yield return www; if (www.error != null)
{
Debugger.LogError(string.Format("Read {0} failed: {1}", path, www.error));
yield break;
} mBundleCount++;
LuaFileUtils.Instance.AddSearchBundle(name, www.assetBundle); try
{
if (null != OnLoading)
{
OnLoading(mBundleCount, mTotalBundleCount, name, path);
}
}
catch (Exception e)
{
Debug.LogError(e.Message);
} www.Dispose();
}
} #if UNITY_5
private IEnumerator LoadBundles(string dir,string bundleName)
#else
public IEnumerator LoadBundles(string dir, List<string> bundleList)
#endif
{
var cachePath = Application.temporaryCachePath.Replace('\\', '/');
var streamingPath = Application.streamingAssetsPath.Replace('\\', '/'); List<string> list = new List<string>(); #if UNITY_5 var bundlePath = cachePath+"/"+dir+"/"+bundleName;
if (!File.Exists(bundlePath))
{
bundlePath = streamingPath + "/" + dir + "/" + bundleName;
}
else
{
#if UNITY_ANDROID && !UNITY_EDITOR
bundlePath = "file:///" + bundlePath;
#endif
}
#if UNITY_ANDROID && !UNITY_EDITOR #else
bundlePath = "file:///" + bundlePath;
#endif
using (WWW www = new WWW(bundlePath))
{
yield return www; AssetBundleManifest manifest = (AssetBundleManifest)www.assetBundle.LoadAsset("AssetBundleManifest");
list = new List<string>(manifest.GetAllAssetBundles());
//www.assetBundle.Unload(true);
www.Dispose();
}
#else
list = bundleList;
#endif
mTotalBundleCount = list.Count; for (int i = 0; i < list.Count; i++)
{
string str = list[i]; string path =cachePath+"/"+dir+"/"+str;
if (!File.Exists(path))
{
path = streamingPath + "/" + dir + "/" + str;
}
else
{
#if UNITY_ANDROID && !UNITY_EDITOR
path = "file:///" + path;
#endif
}
#if UNITY_ANDROID && !UNITY_EDITOR #else
path = "file:///" + path;
#endif
string name = Path.GetFileNameWithoutExtension(str);
StartCoroutine(CoLoadBundle(name, path));
} yield return StartCoroutine(CheckLoadFinish());
} IEnumerator CheckLoadFinish()
{
while (mBundleCount < mTotalBundleCount)
{
yield return null;
} if (null != OnLoadOver)
{
try
{
OnLoadOver();
}
catch (Exception e)
{
Debug.LogError(e.Message);
} }
} }

  

使用代码

var loader = GetComponent<LuaBundleLoader>();
if (null == loader)
{
loader = gameObject.AddComponent<LuaBundleLoader>();
} loader.OnLoading = (idx, total, bundleName, path) =>
{
Debug.Log(path+" ok");
}; loader.OnLoadOver = OnBundleLoadOver; loader.LoadBundle(LuaConst.osDir, LuaConst.osDir);

  

Unity3D LuaBundleLoader(基于cslua)的更多相关文章

  1. Thinking in Unity3D:基于物理着色(PBS)的材质系统

    关于<Thinking in Unity3D> 笔者在研究和使用Unity3D的过程中,获得了一些Unity3D方面的信息,同时也感叹Unity3D设计之精妙.不得不说,笔者最近几年的引擎 ...

  2. Unity3D LuaComponent(基于ulua)

    LuaComponent可以支持配一个需要执行在这个gameObject上的lua脚本,并且每个gameObject上的lua都是一个实例 using UnityEngine; using LuaIn ...

  3. Thinking in Unity3D

    关于<Thinking in Unity3D> 笔者在研究和使用Unity3D的过程中,获得了一些Unity3D方面的信息,同时也感叹Unity3D设计之精妙.不得不说,笔者最近几年的引擎 ...

  4. Unity3D游戏开发初探—1.跨平台的游戏引擎让.NET程序员新生

    一.Unity3D平台简介 Unity是由Unity Technologies开发的一个让轻松创建诸如三维视频游戏.建筑可视化.实时三维动画等类型互动内容的多平台的综合型游戏开发工具,是一个全面整合的 ...

  5. 【Unity3D】AR应用中,关于东南西北方位的判断。

    这篇文章的应用场景是这样子的: 首先我们要做的是一个带有LBS定位服务(比如高德地图.百度地图等)AR功能,在这个场景中,会有一些地图上的”点“(如派出所.学校)是我们需要显示在我们的AR镜头上的,如 ...

  6. Unity3D 装备系统学习Inventory Pro 2.1.2 总结

    前言 写在最前面,本文未必适合纯新手,但有一些C#开发经验的还是可以看懂的,虽然本人也是一位Unity3D新人,但是本文只是自己在学习Inventory Pro的学习总结,而不是教程,本人觉得要读懂理 ...

  7. 在Unity3D的网络游戏中实现资源动态加载

    用Unity3D制作基于web的网络游戏,不可避免的会用到一个技术-资源动态加载.比如想加载一个大场景的资源,不应该在游戏的开始让用户长时间等待全部资源的加载完毕.应该优先加载用户附近的场景资源,在游 ...

  8. PureMVC和Unity3D的UGUI制作一个简单的员工管理系统实例

    前言: 1.关于PureMVC: MVC框架在很多项目当中拥有广泛的应用,很多时候做项目前人开坑开了一半就消失了,后人为了填补各种的坑就遭殃的不得了.嘛,程序猿大家都不喜欢像文案策划一样组织文字写东西 ...

  9. (转)在Unity3D的网络游戏中实现资源动态加载

    原文:http://zijan.iteye.com/blog/911102 用Unity3D制作基于web的网络游戏,不可避免的会用到一个技术-资源动态加载.比如想加载一个大场景的资源,不应该在游戏的 ...

随机推荐

  1. DayOfWeek

    int a  =(int)oneDate.DayOfWeek;  返回的直接就是 1,2,3,4,5,6,  星期日返回的是0

  2. 20145233 2016-2017 1 linux题目总结

    20145233 2016-2017 1 linux题目总结 第一周考试知识汇总 判断:实验楼环境中所有的默认系统用户名和密码均为 shiyanlou.(x ). 填空:Linux Bash中,Ctr ...

  3. CSS3系列之3D制作

    一.序 博主最近这些天,突发奇想的想研究一下CSS3的东西,从而提升一下CSS的能力,在学习的过程中发现其实CSS3是一个挺复杂的东西,深入的研究,你可能会涉及到初中的光学理论来帮助理解一些概念,同时 ...

  4. 关于c++类的内存分配

    参考:这里 虽然有些地方错了,但是也可以一看,大概能加深对c++类相关的内存分配的了解 然后这还不算十分深入,更深入的可以看这里. 这本书是时候读一下了:<深度探索C++对象模型> (待续 ...

  5. 软件工程(FZU2015)赛季得分榜,第10回合(alpha冲刺)

    目录 第一回合 第二回合 第三回合 第四回合 第五回合 第6回合 第7回合 第8回合 第9回合 第10回合 第11回合 积分规则 积分制: 作业为10分制,练习为3分制:alpha30分: 团队项目分 ...

  6. 使用Extjs组件实现Top-Left-Main布局并且增加事件响应

    每次在毕业答辩会上,看到同专业的同学只要是XXX管理系统,就是下图所示的界面,看来这中布局还是很受欢迎的(偷笑).接下来进入我们正题,在web项目无论是前端还是后台管理比较常见的布局就是Top-Lef ...

  7. javascript中怎么判断对象{}为空

    有时候通过AJAX方法调用返回的是一个JSON对象,而这个对象可能在开发过程中会没有数据是一个空{}. JavaScript判断object/json 是否为空,可以使用jQuery的isEmptyO ...

  8. BZOJ 3781: 小B的询问

    3781: 小B的询问 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 643  Solved: 435[Submit][Status][Discuss ...

  9. bzoj 3055礼物运送 floyed + 状压DP

    bzoj 3055: 礼物运送 floyed first 设f[i][S]表示取到了S集合中的所有点(不一定是经过的所有点),最后停在了i的最优值. 初始就f[i][{i}] = dis[1][i] ...

  10. Javascript知识点记录(二)

    Javascript入门易,精通难,基本上是共识的一个观点.在这个篇幅里,主要对一些难点进行记录. 鸭子类型 Javascript属于动态类型语言的一种.对变量类型的宽容,给了很大的灵活性.由于无需类 ...