您的位置:首页 > 移动开发 > Unity3D

[Unity热更新]更新lua脚本 (二)

2016-03-04 23:53 615 查看
unity中各种特殊的文件夹:
http://www.xuanyusong.com/archives/3229
unity中的四种路径:
http://www.manew.com/forum.php?mod=viewthread&tid=21404&extra=page%3D1%26filter%3Dtypeid%26typeid%3D143&_dsign=12bde134 http://www.manew.com/thread-23491-1-1.html?_dsign=451c13c5 http://www.cnblogs.com/murongxiaopifu/p/4199541.html
Application.dataPath(只读)

此属性用于返回程序的数据文件所在文件夹的路径。

游戏数据的存储路径。

Application.streamingAssetsPath(只读)

此属性用于返回流数据的缓存目录,返回路径为相对路径,适合设置一些外部数据文件的路径。

工程目录下面的Assets/StreamingAssets。

Application.persistentDataPath(可读可写)

此属性用于返回一个持久化数据存储目录的路径,可以在此路径下存储一些持久化的数据文件。

设备中的公开目录,根据平台的不同而不同。这里面的文件不会因为App升级而删除。

Application.temporaryCachePath(只读)

此属性用于返回一个临时数据的缓存目录。

设备的临时存储路径。

android:

Application.dataPath                                  /data/app/xxx.xxx.xxx.apk

Application.streamingAssetsPath           jar:file:///data/app/xxx.xxx.xxx.apk/!/assets

Application.persistentDataPath                /data/data/xxx.xxx.xxx/files

Application.temporaryCachePath            /data/data/xxx.xxx.xxx/cache

ios:

Application.dataPath                                   Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxx.app/Data

Application.streamingAssetsPath           Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxx.app/Data/Raw

Application.persistentDataPath               Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/Documents

Application.temporaryCachePath            Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/Library/Caches

注:

Application.persistentDataPath 才是移动端可用的保存生成文件的地方
放到resource中打包后不可以更改
放到Application.dataPath中移动端是没有访问权限的

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

unity5中的assetbundle(打包与加载):http://blog.csdn.net/lyh916/article/details/49815871

1.将lua脚本等资源进行打包,并对打包文件进行压缩

使用ulua插件后的测试脚本:

luanet.load_assembly("UnityEngine");
GameObject = luanet.import_type("UnityEngine.GameObject");
PrimitiveType = luanet.import_type("UnityEngine.PrimitiveType");

--Capsule
local go = GameObject.CreatePrimitive(PrimitiveType.Capsule);


2.将压缩文件上传到服务器,进入游戏登录界面后,对比版本号,下载更新资源,并解压

服务器端:使用tomcat服务器,将压缩文件复制到WebRoot文件夹内即可。编写的jsp脚本:

<body>
<%
String version = request.getParameter("version");
String platform = request.getParameter("platform");
if(Float.parseFloat(version) != 2f)
response.getWriter().write("http://localhost:8080/TestLua/AB.zip");
else
response.getWriter().write("your version is latest");
%>
</body>


using UnityEngine;
using System.Collections;
using System.IO;

public class TestHttp : MonoBehaviour {

void Start()
{
StartCoroutine(CheckUpdate());
}

IEnumerator CheckUpdate()
{
//post
WWWForm form = new WWWForm();
form.AddField("version", "1.0");
form.AddField("platform", "android");
WWW www = new WWW("http://localhost:8080/TestLua/index.jsp", form);

//get
//WWW www = new WWW("http://localhost:8080/TestLua/index.jsp?version=1.0&platform=android");

yield return www;

if (www.error != null)
{
Debug.Log(www.error);
}
else
{
Debug.Log(www.text);
Debug.Log(Application.persistentDataPath);
if (www.text.Contains(".zip"))
{
Debug.Log("downloading");
StartCoroutine(DownLoad(www.text));
}
}
www.Dispose();
}

IEnumerator DownLoad(string downLoadUrl)
{
WWW www = new WWW(downLoadUrl);
yield return www;

if (www.error != null)
{
Debug.Log(www.error);
}
else
{
downLoadUrl = downLoadUrl.Substring(0, downLoadUrl.IndexOf(".zip"));
string fileName = downLoadUrl.Substring(downLoadUrl.LastIndexOf("/") + 1);
fileName = fileName + ".zip";
Debug.Log("fileName:  " + fileName);

string outputPath = Application.persistentDataPath + "/"+ fileName;
Debug.Log("outputPath:  " + outputPath);
string outputDir = Path.GetDirectoryName(outputPath);
Debug.Log("outputDir:  " + outputDir);

if (!Directory.Exists(outputPath))
{
Directory.CreateDirectory(outputDir);
}

byte[] bytes = www.bytes;
File.WriteAllBytes(outputPath, bytes);
Debug.Log("downloaded");
ZipTool.UnZip(outputPath, outputDir, true);
}
www.Dispose();
}

}


3.从登录界面进入主界面,热更新完成

using UnityEngine;
using System.Collections;
using LuaInterface;

public class LoadAssetbBundle : MonoBehaviour {

void Start()
{
string dir = @"file:///" + Application.persistentDataPath + "/";
//Debug.Log(dir);

AssetbBundleManager.Instance.Load(dir, "newuluascript.unity3d",
(ab, name) =>
{
string content = AssetbBundleManager.Instance.GetString(ab, name);
LuaScriptMgr lua = new LuaScriptMgr();
lua.Start();
lua.DoString(@content);
});
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  热更新