您的位置:首页 > 编程语言 > Lua

项目记录15--热更新ulua加入框架测试和.android热更新脚本

2016-01-07 09:30 597 查看
这几天一直在忙公司的一个外包项目,也挺累的。却是头疼到处乱挂脚本,没有任何的悬念。没办法外包就是如此。总之就是坑爹的节奏。真想全删了重写。可惜时间不够,再过几天就要出版本。

抽点时间出来研究下ulau,怎么说呢,本来写的框架就把热更新考虑上去了可以说是留个位置吧,就是不在组件上挂脚本,这个和ulua实现热更新有点像。已经测试过资源的更新,随便改变里面的组件添加只要不是改变###规范的随便,算是一种半更新(呵呵)。所以现在把ulua弄上去填补下,最大限度不改变底层框架的前提下做部分的lua热更新,不会所有界面都使用lua,考虑到效率,还有习惯,还是喜欢使用c#写代码。比如活动面板,商店等就使用lua,其它还是c#。

本来的框架不挂脚本,而且是动态加载面板http://blog.csdn.net/u010665359/article/details/50445220最后面的一个视频地址。所以有点像ulua里面的例子。不多说记录下两天的成果,只做一个开端,真要使用时候才真改框架里面部分来做适应。

1.使用ulua第一步就是生产WrapFile,所以第一步就是调用工具里面的Gen Lua Wrap File,注意一点就是要等菊花转完。在ulua-->Source-->LuaWrap下就会产生对应的文件了。

2.需要添加自己的c#文件就要修改:ulua-->Editor-->wrapFile文件。在这里加入自己的这个很重要不然后面调用不通过,文件

_GT(typeof(BaseUI)),

_GT(typeof(BaseUiContainer)),

_GT(typeof(BasePanel)),

在LuaWrap下就可以看到自己的文件对应的文件。

3.编写lua脚本和自己框架里面的文件对应上,最大限度不改变底层。

/**************************************************

* 此文件由工具自动生成,请别随便手动修改

* 生成时间 :2016-1-6 22:26

* 如果要手动修改: 请在相对应的手动代码块之间添加

* 1.手动添加包或者其他定义等 请在'手动添加using' 和 '手动添加usingEnd' 注释之间添加

* 2.手动添加代码块 请在'手动添加代码块' 和 '手动添加代码块End' 注释之间添加

* 3.手动添加变量 请在'手动添加变量' 和 '手动添加变量End' 注释之间添加

* 4.手动添加控件绑定 请在'手动添加控件绑定' 和 '手动添加控件绑定' 注释之间添加

* 5.手动添加Close 请在'手动添加Close' 和 '手动添加CloseEnd' 注释之间添加

* 同时请不要手动添加以上字符串,这些作为手动代码标识

*

**************************************************/

using System;

using System.Text;

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.UI;

/*****************手动添加using**************/

/*****************手动添加usingEnd***********/

public class LoginModule : BasePanel

{

protected override string _resPath{ get { return "LoginModule"; } }

private LuaScriptMgr _mrglua;

public LoginModule()

: base()

{

}

// private LoginPanel _loginPanel;

// private RegisterPanel _registerPanel;

/*****************手动添加变量***************/

/*****************手动添加变量End************/

protected override void _RegisterComponents()

{

base._RegisterComponents();

// _loginPanel = new LoginPanel(this, _RegisterUI("@@@LoginPanel").gameObject);

// _registerPanel = new RegisterPanel(this, _RegisterUI("@@@RegisterPanel").gameObject);

/*****************手动添加控件绑定***********/

_mrglua = new LuaScriptMgr ();

_mrglua.Start();

_mrglua.DoFile("LoginModule");

_mrglua.CallLuaFunction("LoginModule._RegisterComponents", this);

/*****************手动添加控件绑定End********/

}

/*****************手动添加代码块***************/

protected override void _Init()

{

base._Init ();

_mrglua.CallLuaFunction("LoginModule._Init", this);

}

protected override void _InitEvent()

{

base._InitEvent ();

_mrglua.CallLuaFunction("LoginModule._InitEvent", this);

}

protected override void _RemoveEvent()

{

base._InitEvent ();

_mrglua.CallLuaFunction("LoginModule._RemoveEvent", this);

}

/*****************手动添加代码块End************/

public override void Close()

{

// _loginPanel.Close();

// _registerPanel.Close();

/*****************手动添加Close**************/

/*****************手动添加CloseEnd***********/

base.Close();

}

}

------------------------------------------------lua-------------------------------

local transform;

local gameObject;

LoginModule = {};

local this = LoginModule;

local LoginChildPanel;

local RegisterPanel;

local LoginBtn;

local TempThis;

function LoginModule._RegisterComponents(obj)

print("Lua : _RegisterComponents");

gameObject = obj.uiObject; --自己底层的封装

transform = obj.uiObject.transform;

TempThis = obj;

LoginChildPanel = gameObject.transform:Find("@@@LoginPanel");

RegisterPanel = gameObject.transform:Find("@@@RegisterPanel");

print(LoginChildPanel.name);

print(RegisterPanel.name);

end

function LoginModule._Init()

print("Lua : _Init");

end

function LoginModule._InitEvent()

print("Lua : _InitEvent");

end

function LoginModule._RemoveEvent()

print("Lua : _RemoveEvent");

end

function LoginModule.Close()

print("Lua : Close");

TempThis:Close(); --回调c#里面的close 测试通过

end

4.需要注意_mrglua.DoFile("LoginModule");这里的路径是相对路径在ulau里面就是ulua-->lua,如果需要更改为自己的一个路径就需要改Unit.luaPath。

5.现在就可以当做一个普通的组件来使用了。当然这里只是简单的调用,需要改的还很多。

6.就是热更新,说白了就是把lua文件当成资源文件就好,起码这样理解容易接受,既然是资源文件就可以打成assetBundle加载时候加载成TextAsset直接DoString或者DoFile或者压缩直接下载,写入到本地。

8.android热更新脚本。

using UnityEngine;
using System.Collections;
using System;

public class LoadEvent : MonoBehaviour {

public string ScriptName;
// 要动态加载的脚本名

// Use this for initialization
void Awake()
{
if (Application.platform == RuntimePlatform.IPhonePlayer)
{
gameObject.AddComponent(ScriptName);
}
else
{
GameMain main = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<GameMain>();
if (main)
{
Type script = main.m_ScriptAssembly.GetType(ScriptName);
if (script != null)
{
gameObject.AddComponent(script);
}
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: