前言
Lua这个语言很随意,你随意写,我不管,我也不提示错误,你开心怎样写都行,但同时BUG出的也会很随意,所以在使用lua时一定要注意规范,不然你会非常爽,哈哈,好了,正文开始,说下重要的逻辑,储存账号密码是通过写入文件储存的,以” ’ “单引号进行拆分,奇数为账号偶数为密码,关闭界面是以模块类进行调用关闭。
第一步:我们创建3个Panel 分别是 LoginPanel登陆界面 RegisterPanel注册界面 和GamePanel游戏界面。然后添加输入框,按钮(),之后我们在Assets的Builds文件夹下创建三个文件夹,这些文件夹是放置Panel预设物的,名字不要打错,然后在Textures下也创建三个文件夹,这三个文件夹是放置各自的图片素材的,创建完成后把自己的Panel预设物放到各自的文件夹下。

2.接下来们开始第二步:我们查找到AppConst和Packager两个文件,打开并按如图设置,图二圈住位置改为false方便以后修改Lua脚本时不用打包,图三为 预设物打包和图片路径设置 重点:路径千万不能错!



3.设置完之后我们进行点击Build Andtoid Resources 进行打包 如果使用的是PC端 直接打包Windows端也可以,点完之后会进行进度加载,加载完成之后,看看下方StreamingAssets文件夹里是否有你资源,有的话就打包成功没的话就失败,失败的话大部分是路径所导致的。

4.下面我们打开lua编译器开始lua的登陆注册,我用的是Sublime,不同的编译器可能会有偏差,在下图框住的位置,difine文件下,我们进行Panel和控制的定义:
在Game脚本下我们进行面板的启动,直接复制粘贴改动CtrlNames就行:如下图

然后在CtrlManager脚本下添加控制器。

5.我们开始最后一步,创建自己的lua脚本 在Controller下创建 GameCtrl LoginCtrl RegisterCtrl 在View文件夹下创建GanePanel ,LoginPanle,RegisterPanle。
重要提示:另外在Common下需要创建一个module文件,因为在工程中我写了一个模块,方便其他脚本使用,里面包含了字符串的拆分和所有Panel物体,具体使用方法已在LoginCtrl第一行引用,并设置为全局变量,即所有脚本都可以使用里面的函数和变量,类似于C#中的单例类。会在最后附上module模块代码,如下图:

创建完成后Ctrl+s会弹出下面面板,然后输入名字进行保存

7.大功告成,附上各个Panel Ctrl 和模块类类module的代码:
- LoginCtrl
Tootil= require "Common/module"
--定义一个对象
LoginCtrl={};
--添加一个本地的引用(局部变量)
local this = LoginCtrl;
local gameObject;
local myPanelBehaviour;
--构建函数
function LoginCtrl.New()
logWarn("这是一次伟大的尝试!");
return this;
end
--创建事件
function LoginCtrl.Awake()
logWarn("我们要尝试创建一个面板");
panelMgr:CreatePanel("Login",this.OnCreateCB);
end
--回调启动事件
function LoginCtrl.OnCreateCB(obj)
logWarn("恭喜创建成功!");
gameObject=obj;
logWarn("物体的名字是:"..gameObject.name);
--添加点击事件
--找到LuaBehavlour
myPanelBehaviour=gameObject:GetComponent("LuaBehaviour");
myPanelBehaviour:AddClick(LoginPanel.mybutton,this.ButtonClick)
myPanelBehaviour=gameObject:GetComponent("LuaBehaviour");
myPanelBehaviour:AddClick(LoginPanel.RegisteButton,this.OpenRegisterPanel)
end
--登陆按钮
function LoginCtrl.ButtonClick(obj)
--账户是否正确
local boolean Accountifcorrectbool=false
if(LoginPanel.AccountInputFild:GetComponent("Text").text=="" or LoginPanel.PassWordInputFild:GetComponent("Text").text=="")
then
LoginPanel.text:GetComponent("Text").text="账号密码不能为空!"
else
file= io.open("D:\\pass.txt","a+"); --打开文件
io.input(file); --对文件进行操作
local fileconnt =file:read("*all") --读取全部文件 --读取全部并赋值
print("fileconnt="..fileconnt)
file:close() --关闭文件
stringfileconnet= tostring(fileconnt)--文本转成字符串
print("stringfileconnet="..stringfileconnet)
local alluserData={} --所有账户信息的表
--把字符串一单引号进行拆分并放进表里
alluserData=Tootil.split(stringfileconnet,"\'");
--遍历表里的账户信息
for i,v in pairs(alluserData) do
--账号是奇数 密码是偶数 找到所有账号 进行账号的对比
if (i%2~=0)then --查找所有账号
print(alluserData[i])
if(alluserData[i]==LoginPanel.AccountInputFild:GetComponent("Text").text)then
print("密码:"..alluserData[i+1])
--账号已经存在 查看密码是否正确
if(alluserData[i+1]==LoginPanel.PassWordInputFild:GetComponent("Text").text)then
--账号密码正确 可以进行登陆
Accountifcorrectbool=true
break
else
LoginPanel.text:GetComponent("Text").text="密码有误,请重新输入!"
end
else
--数据库里没有这个账号
Accountifcorrectbool=false
end
end
end
if(Accountifcorrectbool==true)then --如果信息正确
LoginPanel.text:GetComponent("Text").text="登陆成功!"
--准备跳转界面
Tootil.LogionPnaelGb:SetActive(false)
Tootil.GamePanelGb:SetActive(true)
else
LoginPanel.text:GetComponent("Text").text="该账号不存在!"
end
end
end
--打开注册界面
function LoginCtrl.OpenRegisterPanel(obj)
Tootil.RegisterPnaelGb:SetActive(true)
Tootil.LogionPnaelGb:SetActive(false)
end
2.LoginPanel
local transform;
local gameObject;
LoginPanel = {};
local this = LoginPanel;
--启动事件--
function LoginPanel.Awake(obj)
gameObject = obj;
transform = obj.transform;
this.InitPanel();
--把自己进行赋值
Tootil.LogionPnaelGb=gameObject
print("定义出的面板="..Tootil.LogionPnaelGb.name)
logWarn("Awake lua--->>"..gameObject.name);
end
--初始化面板--
function LoginPanel.InitPanel()
this.text = transform:FindChild("Text").gameObject; --提示文本
this.mybutton=transform:FindChild("Button").gameObject; --登陆按钮
this.AccountInputFild=transform:FindChild("AccountInputField"):FindChild("Text").gameObject; --账户输入框
this.PassWordInputFild=transform:FindChild("PassWordInputField"):FindChild("Text").gameObject; --密码输入框
this.RegisteButton=transform:FindChild("RegistButton").gameObject; --注册按钮
this.IamegeTransform=transform:FindChild("Image"); --这里有没有无所谓
end
--单击事件--
function LoginPanel.OnDestroy()
logWarn("OnDestroy---->>>");
end
3.RegisterCtrl
--定义一个对象
RegisterCtrl={};
--添加一个本地的引用(局部变量)
local this = RegisterCtrl;
local gameObject;
local myPanelBehaviour;
--构建函数
function RegisterCtrl.New()
logWarn("这是一次伟大的尝试!");
return this;
end
--创建事件
function RegisterCtrl.Awake()
logWarn("我们要尝试创建一个面板");
panelMgr:CreatePanel("Register",this.OnCreateCB);
end
--回调启动事件
function RegisterCtrl.OnCreateCB(obj)
logWarn("恭喜创建成功!");
gameObject=obj;
logWarn("物体的名字是:"..gameObject.name);
--添加点击事件
--找到LuaBehavlour
myPanelBehaviour=gameObject:GetComponent("LuaBehaviour");
myPanelBehaviour:AddClick(RegisterPanel.OpenLoginBtn,this.OpenLoginPanel) --打开登陆界面
myPanelBehaviour=gameObject:GetComponent("LuaBehaviour");
myPanelBehaviour:AddClick(RegisterPanel.SureRegister,this.SureRegisterButton)
end
function RegisterCtrl.ButtonClick(obj)
--这个text不是一个组件 他现在需要物体 需要获取
end
--打开登陆界面
function RegisterCtrl.OpenLoginPanel(obj)
Tootil.RegisterPnaelGb:SetActive(false)
Tootil.LogionPnaelGb:SetActive(true)
end
--注册按钮
function RegisterCtrl.SureRegisterButton(obj)
local boolean isoverFind=false
if(RegisterPanel.RegAccountInputField:GetComponent("Text").text=="" or RegisterPanel.RegPsaaWordInputField:GetComponent("Text").text=="")
then
RegisterPanel.text:GetComponent("Text").text="账号密码不能为空!"
else
file= io.open("D:\\pass.txt","a+"); --打开文件
io.input(file); --对文件进行操作
local fileconnt =file:read("*all") --读取全部文件 --读取全部并赋值
print("fileconnt="..fileconnt)
file:close()
stringfileconnet= tostring(fileconnt)--文本转成字符串
print("stringfileconnet="..stringfileconnet)
local allAccountData={} --所有账户信息的表
--把字符串一单引号进行拆分并放进表里
allAccountData=Tootil.split(stringfileconnet,"\'");
--遍历表里的账户信息
for i,v in pairs(allAccountData) do
--账号是奇数 密码是偶数 找到所有账号 进行账号的对比
if (i%2~=0)then --查找所有账号
print(allAccountData[i])
if(allAccountData[i]==RegisterPanel.RegAccountInputField:GetComponent("Text").text)then
--账号已经存在 结束对比
isoverFind=false
break
else
--数据库里没有这个账号
isoverFind=true
end
end
end
if(isoverFind==true)then
RegisterPanel.text:GetComponent("Text").text="注册成功!"
file= io.open("D:\\pass.txt","a+");--打开文件
--写入账号密码
file:write(RegisterPanel.RegAccountInputField:GetComponent("Text").text.."\'"..RegisterPanel.RegPsaaWordInputField:GetComponent("Text").text.."\'");
file:close(); --关闭文件
print("账号密码写入完成,账号是:"..RegisterPanel.RegAccountInputField:GetComponent("Text").text.."密码是:"..RegisterPanel.RegPsaaWordInputField:GetComponent("Text").text);
else
RegisterPanel.text:GetComponent("Text").text="该账号已经存在!"
end
end
end
4.RegisterPanel
local transform;
local gameObject;
RegisterPanel = {};
local this = RegisterPanel;
--启动事件--
function RegisterPanel.Awake(obj)
gameObject = obj;
transform = obj.transform;
this.InitPanel();
Tootil.RegisterPnaelGb=gameObject
print("定义出的面板="..Tootil.RegisterPnaelGb.name)
logWarn("Awake lua--->>"..gameObject.name);
end
--初始化面板--
function RegisterPanel.InitPanel()
this.text = transform:FindChild("Text").gameObject; --提示文本
this.SureRegister=transform:FindChild("Button").gameObject; --确认注册按钮
this.RegAccountInputField=transform:FindChild("RegAccountInputField"):FindChild("Text").gameObject;--账户输入框
this.RegPsaaWordInputField=transform:FindChild("RegPsaaWordInputField"):FindChild("Text").gameObject;--密码输入框
this.OpenLoginBtn=transform:FindChild("OpenLoginButton").gameObject; --返回按钮
end
--单击事件--
function RegisterPanel.OnDestroy()
logWarn("OnDestroy---->>>");
end
5.GameCtrl
--定义一个对象
GameCtrl={};
--添加一个本地的引用(局部变量)
local this = GameCtrl;
local gameObject;
local myPanelBehaviour;
--构建函数
function GameCtrl.New()
logWarn("这是一次伟大的尝试!");
return this;
end
--创建事件
function GameCtrl.Awake()
logWarn("我们要尝试创建一个面板");
panelMgr:CreatePanel("Game",this.OnCreateCB);
end
--回调启动事件
function GameCtrl.OnCreateCB(obj)
end
6.GamePanel
local transform;
local gameObject;
GamePanel = {};
local this = GamePanel;
--启动事件--
function GamePanel.Awake(obj)
gameObject = obj;
transform = obj.transform;
this.InitPanel();
Tootil.GamePanelGb=gameObject
logWarn("Awake lua--->>"..gameObject.name);
end
--初始化面板--
function GamePanel.InitPanel()
gameObject:SetActive(false)
end
--单击事件--
function GamePanel.OnDestroy()
logWarn("OnDestroy---->>>");
end
7.module 模块
module={}
module.test="我就是模块"
--登陆界面
module.LogionPnaelGb=nil
--注册界面
module.RegisterPnaelGb=nil
--游戏界面
module.GamePanelGb=nil
--按照指定字符对字符串进行拆分
function module.split(input, delimiter)
input = tostring(input) --转成字符串
delimiter = tostring(delimiter) --转成字符串
if (delimiter=='') then return false end --符号不能为单引号
local pos,arr = 0, {} --定义一个值 和一个表
-- for each divider found
for st,sp in function() --匿名函数
return string.find(input, delimiter, pos, true) --查找对应的字符串 返回字符串的下标
end
do
table.insert(arr, string.sub(input, pos, st - 1))--截取字符串 从字符串input中截取 从pos截取到st-1
pos = sp + 1
end
table.insert(arr, string.sub(input, pos)) --在arr尾插去input内容
return arr --返回一个表
end
--把自己返回给调用者
return module
最后点击 Bulid Android Rsources 打下包 点击运行,大功告成,点击登陆,如下图!




本文详细介绍使用Lua语言实现游戏登录注册系统的全过程,包括界面创建、逻辑处理、账号密码存储及验证等关键步骤。
1669





