Lua登入註冊
使用LuaFramework_UGUI-master做登入註冊
今天分享如何使用lua程式語言做登入註冊,適合Lua初學者,我是一步一步做的,內容有點多,但是非常的細,也很簡單易學,只要一步一步跟著做,肯定會做出來,在寫Lua指令碼時,一定要精力集中,認真一點,注意每一個單詞的拼寫,不然出錯了它不會像c#一樣友好細緻的提醒你,希望能幫到需要的人。
2.登入註冊步驟
2.1建立資料夾
在LuaFramework資料夾下的Examples資料夾下的Builds和Textures資料夾中,分別建立Login和Register兩個資料夾,Builds下建立的資料夾是放登入註冊面板預設物,Textures下建立的資料夾是放要用到的圖片,
、
2.2 修改c#指令碼
找到unity中的Packager指令碼,這個指令碼是Lua中自帶的指令碼,在我們需要的時候新增程式碼即可
如下圖,我在裡面添加了登入註冊的預設物資料夾和登入註冊要用的圖片資料夾,注意要看一下自己的圖片是png還是JPG,下面我為演示了兩種格式(如下程式碼塊的紅色箭頭標識)
2.3、開始建立登入註冊介面
分別建立Login和Register兩個介面,並做成預設物,放到上面2.1中指定的資料夾
2.4 、寫Luaj指令碼,重點了(一定要認真)
2.4.1、 首先修改Lua自帶的指令碼logic中的Game指令碼
紅色框中的是要寫的程式碼,藍色箭頭指向的是註釋掉指令碼中原來的程式碼
2.4.2 其次修改Lua自帶的指令碼logic中的CtrManager指令碼
2.4.3.最後修改Lua自帶的指令碼Common中的define指令碼
2.4.4.兩個登入指令碼(LoginCtrl和LoginPanel)
2.4.4.1 、LoginPanel 指令碼內容
在這裡local transform; local gameObject; --定義一個介面表 LoginPanel = {}; local this = LoginPanel ; --啟動事件-- function LoginPanel .Awake(obj) gameObject = obj; transform = obj.transform; this.InitPanel(); logWarn("Awake lua--->>"..gameObject.name); end --初始化面板-- function LoginPanel .InitPanel() --找元件 this.text= transform:FindChild("Text").gameObject; this.button=10; this.button=transform:FindChild("Button").gameObject; this.UserInputField=transform:FindChild("User_InputField"):FindChild("Text").gameObject; this.PassWardInputField=transform:FindChild("PassWord_InputField"):FindChild("Text").gameObject; this.RegisterBtn=transform:FindChild("Register_Button").gameObject; this.myImageTransform=transform:FindChild("Image"); end function LoginPanel.Update() --旋轉圖片 local angles = this.myImageTransform.localEulerAngles; --0.5秒轉一次 --angles.z =angles.z +0.5*Time.deltaTime; --1秒鐘轉30 angles.z =angles.z +30*Time.deltaTime; this. myImageTransform.localEulerAngles = angles; logWarn("不停的呼叫!!!!!!!"); end --單擊事件-- function LoginPanel .OnDestroy() logWarn("OnDestroy---->>>"); end插入程式碼片
2.4.4.2 、LoginCtrl 指令碼內容
--定義一個物件
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);
--新增點選事件
--找到LuaBehaviour
myPanelBehaviour=gameObject:GetComponent("LuaBehaviour");
myPanelBehaviour:AddClick(LoginPanel.button,this.ButtonClick);
myPanelBehaviour=gameObject:GetComponent("LuaBehaviour");
myPanelBehaviour:AddClick(LoginPanel.RegisterBtn,this.RegisterBtnClick);
end
--登入按鈕
function LoginCtrl.ButtonClick(obj)
--賬號是否正確
local boolean Accountifcorrectbool=false
if LoginPanel.UserInputField:GetComponent("Text").text==""or LoginPanel.PassWardInputField:GetComponent("Text").text==""
then
LoginPanel.text:GetComponent("Text").text="賬號為空";
else
--開啟本地檔案
local AlluserDatafile=io.open("F:\\pass.txt","r")
--讀取json字串
local alluserdataString=AlluserDatafile:read("*a")
--關閉檔案
AlluserDatafile:close()
--把儲存賬號密碼的json字串轉成物件
local alluserdatalist=cjson.decode(alluserdataString)
logWarn("alluserdatalist="..alluserdataString)
for k,v in pairs(alluserdatalist) do
if(LoginPanel.UserInputField:GetComponent("Text").text==alluserdatalist[k].Account and LoginPanel.PassWardInputField:GetComponent("Text").text==alluserdatalist[k].PassWord)
then
logWarn("登入成功")
--準備跳轉介面
LoginPanel.text:GetComponent("Text").text="登入成功";
else
LoginPanel.text:GetComponent("Text").text="賬號或密碼有誤";
end
end
end
end
--按照指定字元對字串進行拆分
function string.split(input,delimiter)
--轉成字串
input=tostring(input)
delimiter=tostring(delimiter)
if(delimiter=='')then return false end
--定義一個位置初始值為0 定義一個表
local pos,arr =0,{}
--匿名函式
for st,sp in function()
return string.find(input,delimiter,pos,true)--查詢對應的字串 返回字串的下標
end
do
table.insert(arr,string.sub(input,pos,st-1))
pos=sp+1
end
table.insert(arr,string.sub(input,pos))--在arr尾部插input內容
return arr--返回一個表
end
--開啟註冊點選事件
function LoginCtrl.RegisterBtnClick(obj)
destroy(gameObject)
local ctrl = CtrlManager.GetCtrl(CtrlNames.Register);
if ctrl ~= nil and AppConst.ExampleMode == 1 then
ctrl:Awake();
end
end
2.4.5、兩個註冊指令碼(RegisterCtrl和RegisterPanel)
2.4.5.1、RegisterPanel指令碼內容
local transform;
local gameObject;
RegisterPanel = {};
local this = RegisterPanel ;
--啟動事件--
function RegisterPanel .Awake(obj)
gameObject = obj;
transform = obj.transform;
this.InitPanel();
logWarn("Awake lua--->>"..gameObject.name);
end
--初始化面板--
function RegisterPanel .InitPanel()
this.text= transform:FindChild("Text").gameObject;
this.mybutton=10;
this.mybutton=transform:FindChild("Register_Button").gameObject;
this.UserInputField=transform:FindChild("Register_user_InputField"):FindChild("Text").gameObject;
this.PassWardInputField=transform:FindChild("Register_Pwd_InputField"):FindChild("Text").gameObject;
this.SurePassWardInputField=transform:FindChild("Register_SurePwd_InputField"):FindChild("Text").gameObject;
this.BackBtn=transform:FindChild("Back_Button").gameObject;
end
--單擊事件--
function RegisterPanel .OnDestroy()
logWarn("OnDestroy---->>>");
end
2.4.5.2、RegisterCtrl指令碼內容
--定義一個物件
RegisterCtrl={};
--新增一個本地的引用
local this=RegisterCtrl;
local gameObject;
local myPanelBehaviour;
cjson=require "cjson"
--構建函式
function RegisterCtrl.New()
logWarn("這是一次偉大的嘗試!!!");
return this;
end
function RegisterCtrl.Awake()
logWarn("我們要嘗試建立一個面板");
panelMgr:CreatePanel("Register",this.OnCreateCB);
destroy(gameObject);
end
function RegisterCtrl.OnCreateCB(obj)
logWarn("恭喜建立成功");
gameObject=obj;
logWarn("物體的名字是:".. gameObject.name);
--新增點選事件
--找到LuaBehaviour
myPanelBehaviour=gameObject:GetComponent("LuaBehaviour");
myPanelBehaviour:AddClick(RegisterPanel.mybutton,this.ButtonClick);
myPanelBehaviour=gameObject:GetComponent("LuaBehaviour");
myPanelBehaviour:AddClick(RegisterPanel.BackBtn,this.BackBtnClick);
end
--註冊按鈕
function RegisterCtrl.ButtonClick(obj)
--所有使用者資訊
local AllUserDataList={}
--是否有相同賬號
local boolean isoverFind=false
if RegisterPanel.UserInputField:GetComponent("Text").text==""or RegisterPanel.PassWardInputField:GetComponent("Text").text==""or RegisterPanel.SurePassWardInputField:GetComponent("Text").text==""
then
RegisterPanel.text:GetComponent("Text").text="賬號或密碼為空!!!";
else
--打來路徑,並給裡面的內容複製
local datafile=io.open("F:\\pass.txt","w+")
--讀取檔案中的所有內容
local userdataJsonString=datafile:read("*a")
--關閉檔案
datafile.close()
--解析json
--如果字串的長度大於0
if(#userdataJsonString>0)then
--把json 轉成物件列表
local userdataList=cjson.decode(userdataJsonString)
--遍歷表
for k,v in pairs(userdataList) do
--把上次使用者的資訊寫進表裡
table.insert(this.AllUserDataList,userdataList[k])
--如果賬號相同
if (RegisterPanel.UserInputField:GetComponent("Text").text==userdataList[k].Account) then
--若有相同使用者名稱 註冊失敗哦
isoverFind=false
break
else
isoverFind=true
end
end
end
if(isoverFind==true or #userdataJsonString==0) then--註冊成功
currentData={Account=RegisterPanel.UserInputField:GetComponent("Text").text,PassWord=RegisterPanel.PassWardInputField:GetComponent("Text").text}
table.insert(AllUserDataList,currentData)
--把儲存所有使用者資訊的表 轉成jsson字串 等待其他地方解析使用
AlluserCjsonString=cjson.encode(AllUserDataList)
--把json字串以io形式儲存到本地
local alldatafile=io.open("F:\\pass.txt","w")
--把json字串寫入檔案
alldatafile:write(AlluserCjsonString)
--關閉檔案
alldatafile.close()
RegisterPanel.text:GetComponent("Text").text="註冊成功!!!";
else
RegisterPanel.text:GetComponent("Text").text="註冊失敗!!!";
end
end
end
function readAll(file)
--讀取全部檔案
local content=file:read("*all")
file:close()
return content
end
--按照指定字元對字串進行拆分
function string.split(input,delimiter)
--轉成字串
input=tostring(input)
delimiter=tostring(delimiter)
if(delimiter=='')then return false end
--定義一個位置初始值為0 定義一個表
local pos,arr =0,{}
--匿名函式
for st,sp in function()
return string.find(input,delimiter,pos,true)--查詢對應的字串 返回字串的下標
end
do
table.insert(arr,string.sub(input,pos,st-1))
pos=sp+1
end
table.insert(arr,string.sub(input,pos))--在arr尾部插input內容
return arr--返回一個表
end
--返回登入點選事件
function RegisterCtrl.BackBtnClick(obj)
destroy(gameObject)
--返回登入
local ctrl = CtrlManager.GetCtrl(CtrlNames.Login);
if ctrl ~= nil and AppConst.ExampleMode == 1 then
ctrl:Awake();
end
end
2.4.6、執行測試,在每次修改Lua指令碼時都要Resources一下,如果沒有配置Android,就用下面的Windows
溫馨提示:如果不想每次都Resources可以修改c#指令碼AppConst,把下面紅線標註的地方true改成false
3.這樣一個簡單的使用LuaFramework_UGUI-master製作登入註冊就完成了。
生命不止,學習不止。