Files
JJBB/Assets/Project/Script/LuaScripts/UI/Lua/LuaUtil.txt
2024-08-23 15:49:34 +08:00

223 lines
6.5 KiB
Plaintext

--Lua工具模块
Util = {}
-- CS接口简写
TabMan = CS.GCGame.Table.TableManager
String = CS.System.String
StrDic = CS.GCGame.Table.StrDictionary
-- GetClientDictionaryString
-- GetServerDictionaryFormatString
Hashtable = CS.System.Collections.Hashtable
LoadAB = CS.LoadAssetBundle.Instance
-- SetImageSprite
CSUtil = CS.GCGame.Utils
PlayerData = CS.GameManager.gameManager.PlayerDataPool
-- GetItemContainer(containertype)
-- GetItemCountByDataId
ContainerType = CS.Games.Item.GameItemContainer.Type
ConsumeType = CS.CONSUM_TYPE
MoneyType = CS.MONEYTYPE
Notify = CS.GUIData.AddNotifyData
UIMan = CS.UIManager
-- ShowUI(UIPathData pathData, OnOpenUIDelegate delOpenUI = null, object param = null, bool defaultHide = false)
-- CloseUI(UIPathData pathData)
LuaUIMan = CS.LuaUIManager.Instance
-- CloseUI(string uiName)
-- ShowLuaUI (string uiName, OnOpenUIDelegate callBack = null, object param = null, bool defaultHide = false)
-- ShowLuaUIAsChild (string uiName, GameObject parent, OnOpenUIDelegate callBack = null, object param = null, bool defaultHide = false)
MsgBox = CS.MessageBoxLogic
-- OpenOKCancelBox(string text, string title = null, OnOKClick funcOKClicked = null, OnCancelClick funcCancelClicked = null, int nCountDown = GlobeVar.INVALID_ID, PASSWORD password = PASSWORD.INVALID, int okBtnDelay = 0, int autoOKBtn = 0,bool NeedShock = false,bool showCloseBtn = false, voidNoneDel disableCallBack = null)
GetMoneySprite = CS.UICurrencyItem.GetCurrencySprite
LuaUIBehaviour = CS.LuaUIBehaviour
-- 获得表格长度
function Util.GetLen(o)
if o == nil then
--print("in nil")
return 0
elseif type(o) == "table" then
--print("in table")
local count = 0
for k, v in pairs(o) do
count = count + 1
end
return count
else
--print("in not table")
--print("o.type = " .. type(o))
return 0
end
end
-- 输出表格
function Util.Print(o, lv)
local curlv = nil
if lv == nil or type(lv) ~= "number" then
curlv = 0
elseif lv < 0 then
curlv = 0
else
curlv = lv
end
if o == nil then
return
elseif type(o) == "table" then
for k, v in pairs(o) do
if type(v) == "table" then
print(Util.PrintMul(" ", curlv + 1) .. k .. " : ")
Util.Print(v, curlv + 1)
else
print(Util.PrintMul(" ", curlv) .. k .. " - " .. tostring(v))
end
end
else
print(Util.PrintMul(" ", curlv) .. k .. " - " .. tostring(v))
end
end
-- 输出表格
function Util.PrintTable(tab,ind)
-- 只是简单打印表里面的数据
local tbName = tostring(tab)
local function dumpTab(tab,ind)
if(tab==nil)then return "nil" end
local str="{"
if(ind==nil)then ind=" " end
--//each of table
for k,v in pairs(tab) do
--//key
if(type(k)=="string")then
k = tostring(k).." = ";
else
k = "["..tostring(k).."] = "
end--//end if
--//value
local s=""
if(type(v)=="nil")then
s="nil"
elseif(type(v)=="boolean")then
if(v) then s="true" else s="false" end
elseif(type(v)=="number")then
s=v
elseif(type(v)=="string")then
s="\""..v.."\"";
elseif(type(v)=="table")then
s=dumpTab(v,ind.." ")
s=string.sub(s,1,#s-1)
end
str=str.."\n"..ind..k..s.." ,"
end
str = str.."\n"..ind.."}".."\n"
return str
end
print(tbName..dumpTab(tab))
end
function Util.PrintMul(char, num)
if num == nil or type(num) ~= "number" or type(char) ~= "string" then
return ""
else
local result = ""
while num > 0 do
result = result .. char
num = num - 1
end
return result
end
end
function Util.ToInt(str)
if type(str) ~= "number" or type(str) ~= "string" then
print("Util.ToInt : can't conver " .. type(str) " to int !")
else
return tonumber(str)
end
end
-- 格式化数字,将大数部分转为中文
function Util.FormatNum(num)
return CSUtil.ConvertLargeNumToPretty(tonumber(num))
end
-- Log
function Log(str)
CS.Module.Log.LogModule.DebugLog("Lua/" .. str)
end
function WLog(str)
CS.Module.Log.LogModule.WarningLog("Lua/" .. str)
end
function ELog(str)
CS.Module.Log.LogModule.ErrorLog("Lua/" .. str)
end
-- 字符串分行的函数;有必要可以修改为公用函数
function Util.Split(source, delimiter)
local result = { }
local from = 1
local delim_from, delim_to = string.find(source, delimiter, from)
while delim_from do
table.insert(result, string.sub(source, from, delim_from-1))
from = delim_to + 1
delim_from, delim_to = string.find(source, delimiter, from)
end
table.insert(result, string.sub(source, from))
return result
end
-- C#层按钮事件绑定到Lua实例
--[[
function Util.BindClick(receiver, button)
button.onClick:AddListener(function ()
receiver:OnClick()
end)
end
]]
function Util.AddButtonListener(listener, buttonObj)
local button = buttonObj:GetComponent('Button')
button.onClick:RemoveAllListeners()
button.onClick:AddListener(listener)
end
--打印表结构
debugex = {}
debugex.print_table = function(t)
local print_r_cache={}
local function sub_print_r(t,indent)
if (print_r_cache[tostring(t)]) then
print(indent.."*"..tostring(t))
else
print_r_cache[tostring(t)]=true
if (type(t)=="table") then
for pos,val in pairs(t) do
if (type(val)=="table") then
print(indent.."["..pos.."] => "..tostring(t).." {")
sub_print_r(val,indent..string.rep(" ",string.len(pos)+8))
print(indent..string.rep(" ",string.len(pos)+6).."}")
elseif (type(val)=="string") then
print(indent.."["..pos..'] => "'..val..'"')
else
print(indent.."["..pos.."] => "..tostring(val))
end
end
else
print(indent..tostring(t))
end
end
end
if (type(t)=="table") then
print(tostring(t).." {")
sub_print_r(t," ")
print("}")
else
sub_print_r(t," ")
end
print()
end