前言
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 打下包 點擊運行,大功告成,點擊登陸,如下圖!