说明:异步加载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. 在JS方法中返回多个值的三种方法

    在使用JS编程中,有时需要在一个方法返回两个个或两个以上的数据,用下面的几种方法都可以实现: 1 使用数组的方式,如下: <html> <head> <title> ...

  2. jquery获取父元素及祖先元素

    parent是找当前元素的第一个父节点,parents是找当前元素的所有父节点 先说一下parent和parents的区别 从字面上不难看出 parent是指取得一个包含着所有匹配元素的唯一父元素的元 ...

  3. [LeetCode] Wiggle Sort

    Problem Description: Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[ ...

  4. ExtJS4随笔(1) -- 在VS中加入Ext4的智能提示

    将辅助文件加入到Web工程内即可. 辅助文件

  5. 【IIS】IIS6.1配置 *.config 文件 的MIME类型用于升级程序

    参考:http://blog.csdn.net 1. 2. 请求筛选中允许config文件下载, 3. 添加.config到 MIME类型. 3.注意:筛选项.

  6. 软件工程(FZU2015)赛季得分榜,第11回合(beta冲刺+SE总结)

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

  7. 扩展htmlhelper.DropDownListFor 支持list数据源和option增加属性

    mvc自带的DropDownListFor数据源必须是IEnumerable<SelectListItem>.并且option不支持增加自定义属性.在使用bootstrap-select组 ...

  8. Asp.Net MVC<四>:路由器

    路由的核心类型基本定义于System.Web.dll中,路由机制同样可应用与Web Forms,实现请求地址和物理文件的分离. web form中使用路由器的示例 路由配置 protected voi ...

  9. C#-WinForm-Treeview-树状模型

    Treeview - 树状模型 利用递归添加数据 数据放入 treeView1.Nodes.Add() 中 public Form3() { InitializeComponent(); TreeNo ...

  10. JavaScript错误之:Uncaught ReferenceError: $ is not defined

    在js开发中,很多人遇到类似问题,都找不到解决方法.Uncaught ReferenceError: $ is not defined,在这里给大家提供几个解决方法. 方法/步骤11.出现这个错误,最 ...