Unity3D LuaBundleLoader(基于cslua)
说明:异步加载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)的更多相关文章
- Thinking in Unity3D:基于物理着色(PBS)的材质系统
关于<Thinking in Unity3D> 笔者在研究和使用Unity3D的过程中,获得了一些Unity3D方面的信息,同时也感叹Unity3D设计之精妙.不得不说,笔者最近几年的引擎 ...
- Unity3D LuaComponent(基于ulua)
LuaComponent可以支持配一个需要执行在这个gameObject上的lua脚本,并且每个gameObject上的lua都是一个实例 using UnityEngine; using LuaIn ...
- Thinking in Unity3D
关于<Thinking in Unity3D> 笔者在研究和使用Unity3D的过程中,获得了一些Unity3D方面的信息,同时也感叹Unity3D设计之精妙.不得不说,笔者最近几年的引擎 ...
- Unity3D游戏开发初探—1.跨平台的游戏引擎让.NET程序员新生
一.Unity3D平台简介 Unity是由Unity Technologies开发的一个让轻松创建诸如三维视频游戏.建筑可视化.实时三维动画等类型互动内容的多平台的综合型游戏开发工具,是一个全面整合的 ...
- 【Unity3D】AR应用中,关于东南西北方位的判断。
这篇文章的应用场景是这样子的: 首先我们要做的是一个带有LBS定位服务(比如高德地图.百度地图等)AR功能,在这个场景中,会有一些地图上的”点“(如派出所.学校)是我们需要显示在我们的AR镜头上的,如 ...
- Unity3D 装备系统学习Inventory Pro 2.1.2 总结
前言 写在最前面,本文未必适合纯新手,但有一些C#开发经验的还是可以看懂的,虽然本人也是一位Unity3D新人,但是本文只是自己在学习Inventory Pro的学习总结,而不是教程,本人觉得要读懂理 ...
- 在Unity3D的网络游戏中实现资源动态加载
用Unity3D制作基于web的网络游戏,不可避免的会用到一个技术-资源动态加载.比如想加载一个大场景的资源,不应该在游戏的开始让用户长时间等待全部资源的加载完毕.应该优先加载用户附近的场景资源,在游 ...
- PureMVC和Unity3D的UGUI制作一个简单的员工管理系统实例
前言: 1.关于PureMVC: MVC框架在很多项目当中拥有广泛的应用,很多时候做项目前人开坑开了一半就消失了,后人为了填补各种的坑就遭殃的不得了.嘛,程序猿大家都不喜欢像文案策划一样组织文字写东西 ...
- (转)在Unity3D的网络游戏中实现资源动态加载
原文:http://zijan.iteye.com/blog/911102 用Unity3D制作基于web的网络游戏,不可避免的会用到一个技术-资源动态加载.比如想加载一个大场景的资源,不应该在游戏的 ...
随机推荐
- linux 安全狗
下载 ;安装; service safedog start 或者 sdstart;sdui
- 如何使用Log4net创建日志及简单扩展
第一步:在项目中添加对log4net.dll的引用,这里引用版本是1.2.10.0.第二步:程序启动时读取log4net的配置文件.如果是CS程序,在根目录的Program.cs中的Main方法中添加 ...
- 安卓开发:一种快速提取安卓app的UI图标资源的方法
在做安卓设计时,找美工设计界面的漂亮图标是必不可少的,但是对于一个初创团队来说,请一个UI的成本其实也挺高的,此时对于一个偏技术的产品经理来说,从其他成熟的产品的apk中提取图标就是一个很便捷的方法, ...
- 87 resize2fs-增大或者收缩未加载的“ext2/ext3”文件系统的大小
resize2fs命令被用来增大或者收缩未加载的"ext2/ext3"文件系统的大小.如果文件系统是处于mount状态下,那么它只能做到扩容,前提条件是内核支持在线resize., ...
- 【USACO 2.3】Money Systems(dp)
v种货币,求有多少种组成和为n. dp[i][j]表示前i种货币价格为j有多少种方案,dp[i][j]+=dp[i-1][j-c]. http://train.usaco.org/usacoprob2 ...
- java分析源码-ReentrantLock
一.前言 在分析了 AbstractQueuedSynchronier 源码后,接着分析ReentrantLock源码,其实在 AbstractQueuedSynchronizer 的分析中,已经提到 ...
- 静态关键字static(2)
static关键字主要有两种作用: 第一,为某特定数据类型或对象分配单一的存储空间,而与创建对象的个数无关. 第二,实现某个方法或属性与类而不是对象关联在一起 具体而言,在Java语言中,static ...
- grunt自动化构建工具
一.什么是grunt? 是基于nodejs的项目构建工具,grunt和grunt插件是通过npm安装并管理的,npm是node.js的包管理器 二.为什么要用grunt? 自动化.对于反复重复的任务, ...
- Ambari——大数据平台的搭建利器
转载自http://www.ibm.com/developerworks/cn/opensource/os-cn-bigdata-ambari/ 扩展 Ambari 管理一个自定义的 Service ...
- centos 7.0 nginx 1.7.9成功安装过程
centos 7.0根目录 的目录构成 [root@localhost /]# lsbin dev home lib64 mnt proc run srv tmp varboot etc lib me ...