miduo_client/Assets/ManagedResources/~Lua/Common/functions.lua

2011 lines
61 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

-- Lua中的继承
function Inherit(tbParent, tbChild)
if not tbParent then
local o = tbChild or {}
setmetatable(o, { __index = o })
return o
else
local tb = tbChild or {}
tbParent.__index = tbParent
--local super_mt = getmetatable(tbParent)
setmetatable(tb, tbParent)
tb.super = setmetatable({}, tbParent)
return tb
end
end
function RandomList(arr)
if not arr or #arr <= 1 then
return
end
local index
for i = #arr, 1, -1 do
index = math.random(1, i)
arr[i], arr[index] = arr[index], arr[i]
end
end
--查找对象--
function find(str)
return GameObject.Find(str)
end
function destroy(obj)
GameObject.DestroyImmediate(obj)
end
function newObject(prefab)
return GameObject.Instantiate(prefab)
end
--适配
function screenAdapte(go)
--if Screen.width > 1080 or Screen.height > 1920 then
-- local scale = math.max(Screen.width / 1080, Screen.height / 1920)
-- go.transform.localScale = Vector3.one * scale
--end
end
function effectAdapte(go)
local scale = Screen.width / Screen.height / 1080 * 1920
--local scale = Screen.width / 1080
--local scale = 1920 / Screen.height
--Log("scale:"..scale)
local v3 = go.transform.localScale
if scale <= 1 then
Util.SetParticleScale(go, scale)
end
go.transform.localScale = v3
end
--创建面板--
function createPanel(name)
PanelManager:CreatePanel(name)
end
function child(str)
return transform:FindChild(str)
end
function subGet(childNode, typeName)
return child(childNode):GetComponent(typeName)
end
function findPanel(str)
local obj = find(str)
if obj == nil then
error(str .. " is null")
return nil
end
return obj:GetComponent("BaseLua")
end
function ReloadFile(file_path)
package.loaded[file_path] = nil -- 消除载入记录
return require(file_path) -- 重新加载lua文件
end
function UnLoadLuaFiles(config)
for i, v in ipairs(config) do
package.loaded[v] = nil
package.preload[v] = nil
end
end
function LoadLuaFiles(config)
for i, v in ipairs(config) do
require(v)
end
end
--播放指定音效,在同一时刻点击音效不播放
function PlaySoundWithoutClick(sound)
SoundManager.PlaySound(sound)
Framework.PlayClickSoundThisTime = false
end
-- 连续弹出文字
-- 1 -- 使用正常Tip
-- 2 -- 使用带颜色的
function PopupText(strTable, delayTime, type)
local index = 1
local timer
timer = Timer.New(function()
if index == #strTable + 1 then
timer:Stop()
else
if type == 1 then
PopupTipPanel.ShowTip(strTable[index])
elseif type == 2 then
PopupTipPanel.ShowColorTip(strTable[index].name, strTable[index].icon, strTable[index].num)
end
index = index + 1
end
end, delayTime, #strTable * delayTime * 2)
timer:Start()
end
function Util_SetHeadImage(spLoader, url, image, isSelf)
if image == nil then
return
end
if url == nil then
Util_SetToDefaultHeadImage(spLoader, image)
return
end
if url == "" or url == "/0" then
Util_SetToDefaultHeadImage(spLoader, image)
return
end
imageDownloadMgr:SetImage_Image(url, image, isSelf)
end
function AddParticleSortLayer(go,layer)
if layer==0 then
return
end
local list=go.transform:GetComponentsInChildren(typeof(UnityEngine.Transform)):ToTable()
for key, value in pairs(list) do
local aaa=value
local render=aaa:GetComponent(typeof(UnityEngine.Renderer))
if render then
render.sortingOrder=render.sortingOrder+layer
end
local canvas=aaa:GetComponent(typeof(UnityEngine.Canvas))
if canvas then
canvas.sortingOrder=canvas.sortingOrder+layer
end
end
end
function Util_SetToDefaultHeadImage(spLoader, image)
if image == nil then
return
end
local bundleName = "normal_asset"
local assetName = "Avatar"
local sprite = spLoader:LoadSprite("Platform", assetName)
image.sprite = sprite
end
function TextHelper_Get24HourTimeStr(time)
if time < 10 then
return "0" .. time
else
return time
end
end
function PrintWanNum(num)
num = tonumber(num)
if num >= 100000000 then
return string.format("%.1f", num / 100000000) .. Language[11028]
elseif num >= 1000000 then
return tostring(math.floor(num / 10000)) .. Language[10037]
else
return tostring(num)
end
end
function PrintWanNum2(num)
num = tonumber(num)
if num >= 100000000 then
return string.format("%.1f", num / 100000000) .. Language[11028]
elseif num >= 100000 then
return tostring(math.floor(num / 10000)) .. Language[10037]
--return tostring(math.floor(num / 1000)/10) .. "万"
else
return tostring(num)
end
end
function PrintWanNum3(num)
num = tonumber(num)
if num >= 100000000 then
return string.format("%.2f", num / 100000000) .. Language[11028]
elseif num >= 100000 then
return string.format("%.2f",num / 10000) .. Language[10037]
else
return tostring(num)
end
end
--判断两个时间在同一天
function Util_Check_insameday(time1, time2)
local date1 = os.date("*t", time1)
local date2 = os.date("*t", time2)
if date1.year == date2.year and date1.month == date2.month and date1.day == date2.day then
return true
end
return false
end
--判断两个时间在同一个月
function Util_Check_insamemonth(time1, time2)
local date1 = os.date("*t", time1)
local date2 = os.date("*t", time2)
if date1.year == date2.year and date1.month == date2.month then
return true
end
return false
end
--判断两个时间在同一星期
function Util_Check_insameweek(time1, time2)
local week_second = 7 * 24 * 3600
local time_zero = os.time { year = 2015, month = 1, day = 5, hour = 0, min = 0, sec = 0 }
local time1_tran = time1 - time_zero
local time2_tran = time2 - time_zero
local week1 = math.floor(time1_tran / week_second)
local week2 = math.floor(time2_tran / week_second)
if week1 == week2 then
return true
end
return false
end
--秒转换成文字对应时间
function GetTimeStrBySeconds(_seconds)
return os.date(Language[10774], _seconds)
end
--秒转换成文字对应时间 只有分秒
function GetTimeMaoHaoStrBySeconds(_seconds)
return os.date("%M:%S", _seconds)
end
--秒转换成文字对应时间只有月日时
function GetTimeMonthDayMinBySeconds(_seconds)
return os.date(Language[12284], _seconds)
end
function PrintTable(root)
local cache = { [root] = "." }
local function _dump(t, space, name)
if type(t) == "table" then
local temp = {}
for k, v in pairs(t) do
local key = tostring(k)
if cache[v] then
tinsert(temp, "+" .. key .. " {" .. cache[v] .. "}")
elseif type(v) == "table" then
local new_key = name .. "." .. key
cache[v] = new_key
tinsert(temp, "+" .. key .. _dump(v, space .. (next(t, k) and "|" or " ") .. srep(" ", #key), new_key))
else
tinsert(temp, "+" .. key .. " [" .. tostring(v) .. "]")
end
end
return tconcat(temp, "\n" .. space)
end
end
print(_dump(root, "", ""))
end
local isPcall = true
--用于查找错误用
function MyPCall(func, ...)
if not isPcall then
func()
return
end
local flag, msg = pcall(func)
if not flag then
LogError(msg)
local args = {...}
if #args > 0 then
local s = ""
for k, v in ipairs(args) do
s = s .. "|" .. tostring(v)
end
LogRed("error params"..s)
end
end
end
function PlayUIAnims(gameObject, callback)
local anims = gameObject:GetComponentsInChildren(typeof(PlayFlyAnim))
if anims.Length > 0 then
for i = 0, anims.Length - 1 do
anims[i]:PlayAnim(false, callback)
end
end
end
function PlayUIAnimBacks(gameObject, callback)
local anims = gameObject:GetComponentsInChildren(typeof(PlayFlyAnim))
if anims.Length > 0 then
for i = 0, anims.Length - 1 do
anims[i]:PlayHideAnim(callback)
end
end
end
function PlayUIAnim(gameObject, callback)
local anim = gameObject:GetComponent(typeof(PlayFlyAnim))
if anim then
anim:PlayAnim(false, callback)
end
end
function SecTorPlayAnim(prefabList,_scale)
local scale = 0.05
if _scale then
scale = _scale
end
for i, node in ipairs(prefabList) do
Timer.New(function ()
node.gameObject:SetActive(true)
PlayUIAnim(node.gameObject)
end,scale*(i-1)):Start()
end
end
function SecTorPlayAnimByScroll(scroll,_scale)
local scale = 0.05
if _scale then
scale = _scale
end
-- scroll:ForeachItemGO(function (index, go)
-- Timer.New(function ()
-- go.gameObject:SetActive(true)
-- PlayUIAnim(go.gameObject)
-- end, scale*(index-1)):Start()
-- end)
end
function PlayUIAnimBack(gameObject, callback)
--local anim = gameObject:GetComponent(typeof(PlayFlyAnim))
local anim = gameObject:GetComponent("PlayFlyAnim")
if anim then
anim:PlayHideAnim(callback)
end
end
--地图uv坐标
function Map_UV2Pos(u, v)
return u * 256 + v
end
--地图uv坐标
function Map_Pos2UV(pos)
return math.floor(pos / 256), pos % 256
end
-- 公会地图坐标精细化处理
function GuildMap_UV2Pos(u, v)
-- return u * 256 + v
local _u = math.floor(u *100)
local _v = math.floor(v *100)
return _u * 25600 + _v
end
function GuildMap_Pos2UV(pos)
-- return math.floor(pos / 256), pos % 256
local _u = math.floor(pos/25600)/100
local _v = pos%25600/100
return math.round(_u), math.round(_v), _u, _v
end
--把英雄星级父对象和星级传过来 type 1 第6-11个预设 type 2 第12-16个预设
function SetHeroStars(spLoader, starGrid, star, type,_starSize,_scale,_pivot,rotation)
local starSize = Vector2(35,35)
if _starSize then
starSize = _starSize
end
local scale = -17.78
if _scale then
scale = _scale
end
local pivot = Vector2.zero
if _pivot then
pivot = _pivot
end
local starPre
if Util.GetGameObject(starGrid, "starGrid(Clone)") then
starPre = Util.GetGameObject(starGrid, "starGrid(Clone)")
else
starPre = poolManager:LoadAsset("starGrid", PoolManager.AssetType.GameObject)
end
local size = starPre:GetComponent("RectTransform").sizeDelta
size.y = starSize.y
starPre.transform.sizeDelta = size
starPre.transform:SetParent(starGrid.transform)
starPre:GetComponent("RectTransform").localPosition = Vector3.zero
starPre:GetComponent("RectTransform").localScale = Vector3.New(1,1,1)
starPre.transform:SetAsFirstSibling()
starPre:GetComponent("LayoutGroup").spacing = scale
starPre:GetComponent("RectTransform").pivot = pivot
if star < 6 then
for i = 1, 17 do
if i <= star then
starPre.transform:GetChild(i - 1):GetComponent("Image").sprite = spLoader:LoadSprite(GetHeroStarImage[1])
starPre.transform:GetChild(i - 1):GetComponent("RectTransform").sizeDelta = starSize
if rotation then
starPre.transform:GetChild(i - 1):GetComponent("RectTransform").rotation = Quaternion.Euler(rotation)
end
starPre.transform:GetChild(i - 1).gameObject:SetActive(true)
else
starPre.transform:GetChild(i - 1).gameObject:SetActive(false)
end
end
elseif star > 5 and star < 10 then
for i = 1, 17 do
if i <= star - 5 then
starPre.transform:GetChild(i - 1):GetComponent("Image").sprite = spLoader:LoadSprite(GetHeroStarImage[2])
starPre.transform:GetChild(i - 1):GetComponent("RectTransform").sizeDelta = starSize
if rotation then
starPre.transform:GetChild(i - 1):GetComponent("RectTransform").rotation = Quaternion.Euler(rotation)
end
starPre.transform:GetChild(i - 1).gameObject:SetActive(true)
else
starPre.transform:GetChild(i - 1).gameObject:SetActive(false)
end
end
elseif star > 9 then
if type and type == 1 then
for i = 1, 17 do
if i == star - 4 then
starPre.transform:GetChild(i - 1).gameObject:SetActive(true)
else
starPre.transform:GetChild(i - 1).gameObject:SetActive(false)
end
end
elseif type and type == 2 then
for i = 1, 17 do
if i > 11 and i == star + 2 then
starPre.transform:GetChild(i - 1).gameObject:SetActive(true)
else
starPre.transform:GetChild(i - 1).gameObject:SetActive(false)
end
end
else
for i = 1, 17 do
if i == star - 4 then
starPre.transform:GetChild(i - 1).gameObject:SetActive(true)
else
starPre.transform:GetChild(i - 1).gameObject:SetActive(false)
end
end
end
end
end
function SetHeroBg(spLoader, bg,cardBg,star,quality)
local hong = Util.GetGameObject(bg,"Effect_UI_yansekuang_HongSe")
local bai = Util.GetGameObject(bg,"Effect_UI_yansekuang_BaiSe")
if star <= 5 then
bg:GetComponent("Image").sprite = spLoader:LoadSprite(GetHeroCardStarImage[1])
cardBg:GetComponent("Image").sprite = spLoader:LoadSprite(GetHeroCardQuantityImage[star])
if hong then
hong.gameObject:SetActive(false)
end
if bai then
bai.gameObject:SetActive(false)
end
elseif star <= 10 then
bg:GetComponent("Image").sprite = spLoader:LoadSprite(GetHeroCardStarImage[2])
cardBg:GetComponent("Image").sprite = spLoader:LoadSprite(GetHeroCardQuantityImage[10])
if hong then
hong.gameObject:SetActive(false)
end
if bai then
bai.gameObject:SetActive(false)
end
else
bg:GetComponent("Image").sprite = spLoader:LoadSprite(GetHeroCardStarImage[3])
cardBg:GetComponent("Image").sprite = spLoader:LoadSprite(GetHeroCardQuantityImage[11])
if hong then
hong.gameObject:SetActive(false)
end
if bai then
bai.gameObject:SetActive(false)
end
end
end
--把英雄星级父对象和星级传过来
local star2index = {
[1] = {3},
[2] = {2, 4},
[3] = {2, 3, 4},
[4] = {1, 2, 4, 5},
[5] = {1, 2, 3, 4, 5}
}
function SetCardStars(starGrid, star)
for i = 1, 15 do
starGrid.transform:GetChild(i - 1).gameObject:SetActive(false)
end
if star < 6 then
for _, i in ipairs(star2index[star]) do
starGrid.transform:GetChild(i - 1).gameObject:SetActive(true)
end
elseif star > 5 and star < 10 then
for _, i in ipairs(star2index[star-5]) do
starGrid.transform:GetChild(5 + i - 1).gameObject:SetActive(true)
end
elseif star > 9 then
starGrid.transform:GetChild(star).gameObject:SetActive(true)
if star > 10 then
Util.GetGameObject(starGrid.transform:GetChild(star),"star").gameObject:SetActive(false)
end
end
end
-- 在给定节点下加载预设, 返回实例化的预设
function newObjToParent(prefab, parent)
local go = newObject(prefab)
go.transform:SetParent(parent.transform)
go.transform.localScale = Vector3.one
go.transform.localPosition = Vector3.zero
go:SetActive(true)
return go
end
-- 清除节点下所有的子节点
function ClearChild(parent)
Util.ClearChild(parent.transform)
end
-- 加载一个商店使用的item
function AddShopItem(parent, itemId, shopType)
local item = SubUIManager.Open(SubUIConfig.ShopItemView, parent.transform)
item:OnOpen(itemId, shopType)
return item
end
-- 根据ID返回物品的Icon配置数据在artResourceConfig中
function SetIcon(spLoader, id)
if not id or id == 0 then
Log("设置的物品Icon不存在")
return
end
local itemConfig = ConfigManager.GetConfig(ConfigName.ItemConfig)
local icon = nil
local resId = itemConfig[id].ResourceID
if not resId then
Log("资源ID不存在")
return
end
local resPath = GetResourcePath(resId)
if not resPath then
Log("尚未有此类美术资源!!")
return
end
icon = spLoader:LoadSprite(resPath)
return icon
end
function SetFrame(spLoader, id)
if not id or id == 0 then
Log("设置的物品Icon不存在")
return
end
local itemConfig = ConfigManager.GetConfig(ConfigName.ItemConfig)
local icon = nil
local resId = itemConfig[id].Quantity
if not resId then
Log("资源ID不存在")
return
end
icon = spLoader:LoadSprite(GetQuantityImageByquality(resId))
return icon
end
--属性表ID转换表
function GetProIndexByProId(proId)
local proIndex = 0
if proId == 1 then
-- 最大生命
proIndex = 3
elseif proId == 2 then
-- 攻击力
proIndex = 4
elseif proId == 3 then
-- 护甲
proIndex = 5
elseif proId == 4 then
-- 魔抗
proIndex = 6
elseif proId == 5 then
-- 速度
proIndex = 7
elseif proId == 6 or proId == 67 or proId == 68 then
-- 当前生命值
proIndex = 2
elseif proId == 51 then
-- 伤害加成
proIndex = 8
elseif proId == 52 then
-- 伤害减免
proIndex = 9
elseif proId == 53 then
-- 效果命中
proIndex = 10
elseif proId == 54 then
-- 效果抵抗
proIndex = 11
elseif proId == 55 then
-- 暴击率
proIndex = 12
elseif proId == 56 then
-- 暴伤
proIndex = 13
elseif proId == 57 then
-- 回复率
proIndex = 14
elseif proId == 101 then
-- 火焰伤害
proIndex = 15
elseif proId == 102 then
-- 狂风伤害
proIndex = 16
elseif proId == 103 then
-- 碧水伤害
proIndex = 17
elseif proId == 104 then
-- 大地伤害
proIndex = 18
elseif proId == 105 then
-- 神圣伤害
elseif proId == 106 then
-- 黑暗伤害
elseif proId == 107 then
-- 火焰抗性
elseif proId == 108 then
-- 狂风抗性
elseif proId == 109 then
-- 碧水抗性
elseif proId == 110 then
-- 大地抗性
elseif proId == 111 then
-- 神圣抗性
elseif proId == 112 then
-- 黑暗抗性
end
return proIndex
end
--逐渐显示对应文字内容,若在此期间点击屏幕,则立刻显示完文字内容
function ShowText(go, str, duration, callBack)
local text = go:GetComponent("Text")
text.text = ""
local tween = text:DOText(str, duration)
local isClick = false
tween:OnUpdate(function()
if Input.GetMouseButtonDown(0) then
isClick = true
end
if isClick then
if tween then
tween:Kill()
go:GetComponent("Text").text = str
if callBack then
callBack()
end
end
end
end)
tween:OnComplete(callBack)
end
-- 将秒转换成分:秒格式返回
function SetTimeFormation(seconds)
local str = ""
local ten_minute = math.modf(seconds / 600)
local minute = math.modf(seconds / 60) % 10
local ten_second = math.modf(seconds / 10) % 6
local second = seconds % 10
str = ten_minute .. minute .. " : " .. ten_second .. second
return str
end
function Dump(data, showMetatable, lastCount)
if type(data) == "table" then
--Format
local count = lastCount or 0
count = count + 1
local B2 = ""
for i = 1, count do
B2 = B2 .. " "
end
Log(B2 .. "{\n")
--Metatable
if showMetatable then
local blank = " "
for i = 1, count do
blank = blank .. " "
end
local mt = getmetatable(data)
Log(blank .. "\"__metatable\" = ")
Dump(mt, showMetatable, count)
end
--Key
for key, value in pairs(data) do
local blank = " "
for i = 1, count do
blank = blank .. " "
end
if type(key) == "string" then
Log(blank .. "\"" .. key .. "\" = " .. GetStr(value))
elseif type(key) == "number" then
Log(blank .. "[" .. key .. "] = " .. GetStr(value))
else
Log(blank .. string.format("%s", key) .. GetStr(value))
end
Dump(value, showMetatable, count)
end
--Format
local B0 = " "
for i = 1, lastCount or 0 do
B0 = B0 .. " "
end
Log(B0 .. "}")
end
--Format
if not lastCount then
Log("\n")
end
end
function GetStr(data)
local str = ""
if type(data) ~= "table" then
--Value
if type(data) == "string" then
str = ("\"" .. data .. "\"")
elseif data == nil then
str = "nil"
else
str = (string.format("%s", data))
end
end
return str
end
--通过item稀有度读取背景框
function GetQuantityImageByquality(quality,star)
if star then--and star > 5
if star == 1 then
return "r_characterbg_gray"
elseif star == 2 then
return "r_characterbg_green"
elseif star == 3 then
return "r_characterbg_blue"
elseif star == 4 then
return "r_characterbg_purple"
elseif star == 5 then
return "r_characterbg_goden"
elseif star >= 6 and star <=10 then
return "r_characterbg_gules"
elseif star > 10 then
return "t_tongyong_topkaung"
end
else
if quality == 0 or quality == 1 then
return "r_characterbg_gray"
elseif quality == 2 then
return "r_characterbg_green"
elseif quality == 3 then
return "r_characterbg_blue"
elseif quality == 4 then
return "r_characterbg_purple"
elseif quality == 5 then
return "r_characterbg_goden"
elseif quality == 6 then
return "r_characterbg_gules"
elseif quality >= 7 then
return "t_tongyong_topkaung"
end
end
end
--通过item稀有度读取背景框
function GetQuantityImageByqualityPoint(quality)
if quality == 1 then
return "r_hunyin_zise"
elseif quality == 2 then
return "r_hunyin_zise"
elseif quality == 3 then
return "r_hunyin_zise"
elseif quality == 4 then
return "r_hunyin_zise"
elseif quality == 5 then
return "r_hunyin_chengse"
elseif quality == 6 then
return "r_hunyin_hongse"
elseif quality == 7 then
return "r_hunyin_caise"
else
return "r_hunyin_caise"
end
end
--通过item稀有度读取背景框
function GetQuantityStrByquality(quality)
if quality == 1 then
return Language[10181]
elseif quality == 2 then
return Language[10180]
elseif quality == 3 then
return Language[10179]
elseif quality == 4 then
return Language[10178]
elseif quality == 5 then --原橙色改为金色
return Language[10176]
elseif quality == 6 then
return Language[10177]
elseif quality == 7 then
return Language[10176]
else
return Language[10181]
end
end
--通过Hero稀有度读取碎片遮罩(小)
function GetHeroChipQuantityImageByquality(quality)
if quality == 1 then
return "PieceMask_white"
elseif quality == 2 then
return "PieceMask_green"
elseif quality == 3 then
return "PieceMask_blue"
elseif quality == 4 then
return "PieceMask_purple"
elseif quality == 5 then
return "PieceMask_goden"
elseif quality == 6 then
return "PieceMask_red"
elseif quality == 7 then
return "PieceMask_Platinum_01"
else
return "PieceMask_white"
end
end
--通过Hero稀有度读取背景框(小)
function GetHeroQuantityImageByquality(quality,star)
if star then--and star > 5
if star == 1 then
return "r_characterbg_gray"
elseif star == 2 then
return "r_characterbg_green"
elseif star == 3 then
return "r_characterbg_blue"
elseif star == 4 then
return "r_characterbg_purple"
elseif star == 5 then
return "r_characterbg_goden"
elseif star >= 6 and star <=10 then
return "r_characterbg_gules"
elseif star > 10 then
return "t_tongyong_topkaung"
end
else
if quality == 0 or quality == 1 then
return "r_characterbg_gray"
elseif quality == 2 then
return "r_characterbg_green"
elseif quality == 3 then
return "r_characterbg_blue"
elseif quality == 4 then
return "r_characterbg_purple"
elseif quality == 5 then
return "r_characterbg_goden"
elseif quality == 6 then
return "r_characterbg_gules"
elseif quality >= 7 then
return "t_tongyong_topkaung"
end
end
end
--通过Hero稀有度读取背景框(卡库)外框
function GetHeroCardQuantityWaiImageByquality(quality)
if quality <= 3 then
return "r_hero_lankuang"
elseif quality == 4 then
return "r_hero_zikuang"
elseif quality == 5 then
return "r_hero_huangkuang"
elseif quality > 5 then
return "r_hero_huangkuang"
end
end
--通过Hero或异妖稀有度读取资质框
function GetQuantityImage(spLoader, quality)
local sprite
if quality<11 then
sprite=spLoader:LoadSprite(AptitudeQualityFrame[1])
elseif quality>=11 and quality<13 then
sprite=spLoader:LoadSprite(AptitudeQualityFrame[2])
elseif quality>=13 and quality<15 then
sprite=spLoader:LoadSprite(AptitudeQualityFrame[3])
elseif quality>=15 and quality<17 then
sprite=spLoader:LoadSprite(AptitudeQualityFrame[4])
elseif quality>=17 and quality<19 then
sprite=spLoader:LoadSprite(AptitudeQualityFrame[5])
end
return sprite
end
--通过装备位置获得装备位置类型字符串
function GetEquipPosStrByEquipPosNum(_index)
if _index == 1 then
return Language[10391]
elseif _index == 2 then
return Language[12041]
elseif _index == 3 then
return Language[10393]
elseif _index == 4 then
return Language[12042]
elseif _index == 5 then
return Language[12043]
elseif _index == 6 then
return Language[12044]
end
end
--通过职业获取职业字符串
function GetJobStrByJobNum(_index)
if _index == 0 then
return Language[12045]
elseif _index == 1 then
return Language[11971]
elseif _index == 2 then
return Language[11972]
elseif _index == 3 then
return Language[11973]
elseif _index == 4 then
return Language[11974]
elseif _index == 5 then
return Language[11975]
end
end
--通过职业获取职业字符串
function GetJobSpriteStrByJobNum(_index)
if _index == 1 then
return "r_hero_huo 1_zh"
elseif _index == 2 then
return "r_hero_feng 1_zh"
elseif _index == 3 then
return "r_hero_shui 1_zh"
elseif _index == 4 then
return "r_hero_dadi 1_zh"
elseif _index == 5 then
return "r_hero_dadi 1_zh"
elseif _index == 6 then
return "r_hero_dadi 1_zh"
else
return "r_hero_huo 1_zh"
end
end
--角色定位ID 获取角色定位背景图片
function GetHeroPosBgStr(_i)
if _i==1 then
return "r_hero_roudundi"
elseif _i==2 then
return "r_hero_shuchudi"
elseif _i==3 then
return "r_hero_kongzhidi"
elseif _i==4 then
return "r_hero_fuzhudi"
end
end
--根据角色定位Id 获取角色定位图
function GetHeroPosStr(_i)
if _i==1 then
return "r_hero_roudunbiao"
elseif _i==2 then
return "r_hero_shuchubiao"
elseif _i==3 then
return "r_hero_konghzibiao"
elseif _i==4 then
return "r_hero_fuzhubiao"
end
end
--获取技能类型 data 当前技能数据
function GetSkillType(data)
local SkillIconType={"r_hero_pu_zh","r_hero_jue_zh","r_hero_bei_zh"}
if data.skillConfig.Type == SkillType.Pu then
return SkillIconType[SkillType.Pu]--普技
elseif data.skillConfig.Type == SkillType.Jue then
return SkillIconType[SkillType.Jue]--绝技
elseif data.skillConfig.Type == SkillType.Bei then
return SkillIconType[SkillType.Bei]--被动技
end
end
--通过角色属性获取角色属性图标
function GetProStrImageByProNum(_index)
if _index == 1 then
return "r_hero_huo 1_zh"
elseif _index == 2 then
return "r_hero_feng 1_zh"
elseif _index == 3 then
return "r_hero_shui 1_zh"
elseif _index == 4 then
return "r_hero_dadi 1_zh"
elseif _index == 5 then
return "r_hero_dadi 1_zh"
elseif _index == 6 then
return "r_hero_dadi 1_zh"
else
return "z_icon_01_zh"
end
end
--通过装备位置获得装备位置类型字符串
function GetQuaStringByEquipQua(_Qua)
--
if _Qua == 1 then
return Language[12046]
elseif _Qua == 2 then
return Language[12047]
elseif _Qua == 3 then
return Language[12048]
elseif _Qua == 4 then
return Language[12049]
elseif _Qua == 5 then
return Language[12050]
elseif _Qua == 6 then
return Language[12051]
elseif _Qua == 7 then
return Language[12052]
end
end
--通过item稀有度获取改颜色的文字
function GetStringByEquipQua(_Qua, _Str)
if _Qua == 1 then
return string.format("<color=#FCF5D3FF>%s</color>", _Str)
elseif _Qua == 2 then
return string.format("<color=#529764FF>%s</color>", _Str)
elseif _Qua == 3 then
return string.format("<color=#6398c9FF>%s</color>", _Str)
elseif _Qua == 4 then
return string.format("<color=#9358bdFF>%s</color>", _Str)
elseif _Qua == 5 then
return string.format("<color=#fcb24eFF>%s</color>", _Str)
elseif _Qua == 6 then
return string.format("<color=#C66366FF>%s</color>", _Str)
elseif _Qua == 7 then
return string.format("<color=#FFEDA1FF>%s</color>", _Str)
end
end
--冒险通过区域序号取得区域名称美术字图标
function GetAreaNameIconByAreaNumer(index)
if index == 1 then
return "r_guaji_qiyuanzhidi"
elseif index == 2 then
return "r_guaji_yiwangzhilu"
elseif index == 3 then
return "r_guaji_houhuizhishi"
elseif index == 4 then
return "r_guaji_zuifashengdian"
elseif index == 5 then
return "r_guaji_qidaoshengsuo"
elseif index == 6 then
return "r_guaji_youjieyehuo"
elseif index == 7 then
return "r_guaji_wushengmishi"
elseif index == 8 then
return "r_guaji_wanshenghuanghun"
end
end
--- N钟N时N天
function GetLeftTimeStrByDeltaTime(deltaTime)
if deltaTime > 86400 then
return math.floor(deltaTime / 86400) .. Language[10017]
end
if deltaTime > 3600 then
return math.floor(deltaTime / 3600) .. Language[12053]
end
if deltaTime > 60 then
return math.floor(deltaTime / 60) .. Language[12054]
end
return math.floor(deltaTime)..Language[10316]
end
function GetLeftTimeStrByDeltaTime2(second)
local day = math.floor(second / (24 * 3600))
local minute = math.floor(second / 60) % 60
local sec = math.floor(second % 60)
local hour = math.floor(math.floor(second - day * 24 * 3600 - sec - minute * 60) / 3600)
if second > 86400 then
return day .. Language[10017]..hour ..Language[10970]
end
if second > 3600 then
return hour..Language[10970]..minute .. Language[12054]
end
return minute .. Language[12054] .. sec .. Language[10316]
end
--- 获取经历的时间字符串
--- 刚刚N分钟前N小时前N天前
function GetDeltaTimeStrByDeltaTime(deltaTime)
if deltaTime > 86400 then
return math.floor(deltaTime / 86400) .. Language[10823]
end
if deltaTime > 3600 then
return math.floor(deltaTime / 3600) .. Language[10822]
end
if deltaTime > 60 then
return math.floor(deltaTime / 60) .. Language[10821]
end
return Language[10820]
end
function GetDeltaTimeStr(timestamp)
local curTimeStemp = GetTimeStamp()
local deltaTime = curTimeStemp - timestamp
return GetDeltaTimeStrByDeltaTime(deltaTime)
end
--- 计算公式 a*x^3 + b*x^2 + c*x +d
--- 1、 CalculateCostCount(x, a, b, c, d)
--- 2、 CalculateCostCount(x, array)
--- array 为按顺序存储a, b, c, d值的数组
---
function CalculateCostCount(x, ...)
if not x then
return
end
local args = { ... }
if type(args[1]) == "table" then
args = args[1]
end
if type(args[1]) ~= "number" then
return
end
local a = args[1]
local b = args[2]
local c = args[3]
local d = args[4]
local cost = math.pow(x, 3) * a + math.pow(x, 2) * b + x * c + d
return cost
end
--- 获取当前时间戳
function GetTimeStamp()
return PlayerManager.serverTime
end
--- 获取当前时间到下一个n点的时间长度
--- 获取当前时间到一天中某时的剩余时间24小时制
function CalculateSecondsNowTo_N_OClock(n)
local curTimeStemp = GetTimeStamp()
--- 标准时间戳从1970年1月1日8点开始加上八个小时的秒数使其从0点开始
--- 8*60*60 = 28800
--- 24*60*60 = 86400
local todayPassSeconds = (curTimeStemp + 28800) % 86400
local targetSeconds = n * 3600 --- 60*60 = 3600
if todayPassSeconds <= targetSeconds then
return targetSeconds - todayPassSeconds
else
-- 如果已经过去了,则计算到第二天这个时间点需要的秒数
return targetSeconds + 86400 - todayPassSeconds
end
end
--获取某个时间戳对应的星期几
--目标日期 = ALLDays % 7 + 基准星期(1970年1月1日那天是星期四)
WeekNumList = {
[0] = 4,
[1] = 5,
[2] = 6,
[3] = 7,
[4] = 1,
[5] = 2,
[6] = 3,
}
function GetTimeStampCorrespondingWeekNum(second)
local WeekNum = 0
local day = math.floor(second / (24 * 3600))
WeekNum = (day % 7)-- + 4
return WeekNumList[WeekNum]
end
function TimeToFelaxible(second)--大于一天用多少天多少小时小于一天用000000
if second <= 86400 then
if not second or second < 0 then
return "00:00:00"
end
local _sec = second % 60
local allMin = math.floor(second / 60)
local _min = allMin % 60
local _hour = math.floor(allMin / 60)
return string.format("%02d:%02d:%02d", _hour, _min, _sec), _hour, _min, _sec
elseif second > 86400 then
local day = math.floor(second / (24 * 3600))
local minute = math.floor(second / 60) % 60
local sec = second % 60
local hour = math.floor(math.floor(second - day * 24 * 3600 - sec - minute * 60) / 3600)
return string.format(Language[10487],day, hour)
end
end
--- 将一段时间转换为天时分秒
function TimeToDHMS(second)
local day = math.floor(second / (24 * 3600))
local minute = math.floor(second / 60) % 60
local sec = second % 60
local hour = math.floor(math.floor(second - day * 24 * 3600 - sec - minute * 60) / 3600)
return string.format(Language[10585],day, hour, minute, sec)
end
--- 将一段时间转换为天时分
function TimeToDHM(second)
local day = math.floor(second / (24 * 3600))
local minute = math.floor(second / 60) % 60
local sec = second % 60
local hour = math.floor(math.floor(second - day * 24 * 3600 - sec - minute * 60) / 3600)
return string.format(Language[11390],day, hour, minute)
end
--- 将一段时间转换为天时
function TimeToDH(second)
local day = math.floor(second / (24 * 3600))
local minute = math.floor(second / 60) % 60
local sec = second % 60
local hour = math.floor(math.floor(second - day * 24 * 3600 - sec - minute * 60) / 3600)
return string.format(Language[10487],day, hour)
end
--- 将一段时间转换为时
function TimeToH(second)
local day = math.floor(second / (24 * 3600))
local minute = math.floor(second / 60) % 60
local sec = second % 60
local hour = math.floor(math.floor(second - day * 24 * 3600 - sec - minute * 60) / 3600)
return string.format(Language[11704], hour)
end
--- 将一段时间转换为时分秒
function TimeToHMS(t)
if not t or t < 0 then
return "00:00:00"
end
local _sec = t % 60
local allMin = math.floor(t / 60)
local _min = allMin % 60
local _hour = math.floor(allMin / 60)
return string.format("%02d:%02d:%02d", _hour, _min, _sec), _hour, _min, _sec
end
--- 将一段时间转换为时分
function TimeToHM(t)
if not t or t < 0 then
return "00:00:00"
end
local _sec = t % 60
local allMin = math.floor(t / 60)
local _min = allMin % 60
local _hour = math.floor(allMin / 60)
return string.format("%02d:%02d", _hour, _min)
end
--- 将一段时间转换为分秒
function TimeToMS(t)
if not t or t < 0 then
return "00:00"
end
local _sec = t % 60
local _min = math.floor(t / 60)
return string.format("%02d:%02d", _min, _sec)
end
--- 获取今天某时间点时间戳
function Today_N_OClockTimeStamp(n)
local currentTime = math.floor(GetTimeStamp())
local tab = os.date("*t", currentTime)
tab.hour = n
tab.min = 0
tab.sec = 0
local N_TimeStamp = os.time(tab)
return N_TimeStamp
end
----- 时效类时间(服务器按照每天凌晨的5点为起始的一天)
function GetTimePass(timeValue)
local tab = os.date("*t", timeValue)
tab.hour = 0
tab.min = 0
tab.sec = 0
local tab2 = os.date("*t", timeValue)
tab2.hour = 0
tab2.min = 0
tab2.sec = 0
local timeStartSum = os.time(tab2)
--0点
local forwardDay
local zeroTimeStamp = os.time(tab)
if timeValue > zeroTimeStamp and timeValue < timeStartSum then
forwardDay = 1
else
forwardDay = 0
end
local dayPass = math.ceil((GetTimeStamp() - timeStartSum) / 86400)
return (dayPass + forwardDay) > 7 and 7 or (dayPass + forwardDay)
end
---时间格式化接口
function GetTimeShow(data)
local year = math.floor(os.date("%Y", data))
local month = math.floor(os.date("%m", data))
local day = math.floor(os.date("%d", data))
local time = year .. "." .. month .. "." .. day
return time
end
---obj必须是userdata(c#对象)
function IsNull(obj)
if not obj then
return true
end
if ServerConfigManager.IsSettingActive(ServerConfigManager.SettingConfig.IS_NULL) then
return obj:IsNull()
else
return obj == nil or obj:Equals(nil)
end
end
------- 红点相关 ----------
-- 绑定红点物体
function BindRedPointObject(rpType, rpObj)
RedpotManager.BindObject(rpType, rpObj)
end
-- 清除红点上绑定的所有物体
function ClearRedPointObject(rpType, rpObj)
RedpotManager.ClearObject(rpType, rpObj)
end
-- 强制改变红点状态(不建议使用)
function ChangeRedPointStatus(rpType, state)
RedpotManager.SetRedPointStatus(rpType, state)
end
-- 重置服务器红点状态到隐藏状态
function ResetServerRedPointStatus(rpType)
RedpotManager.SetServerRedPointStatus(rpType, RedPointStatus.Hide)
end
-- 检测红点显示
function CheckRedPointStatus(rpType)
RedpotManager.CheckRedPointStatus(rpType)
end
-------------------------
--统用属性展示
function GetPropertyFormatStr(type, value)
if type == 1 then
return value
else
if value % 100 == 0 then
return string.format("%d%%", value / 100)
else
return string.format("%.2f%%", value / 100)
end
end
end
--统用属性展示
function GetPropertyFormatStrOne(type, value)
if type == 1 then
return value
else
if value % 100 == 0 then
return string.format("%d%%", value / 100)
else
return string.format("%.1f%%", value / 100)
end
end
end
--装备专属属性展示
function GetEquipPropertyFormatStr(type, value)
if type == 1 then
return value
else
if value / 100 > 1 then
return string.format("%d%%", value / 100)
else
return string.format("%d%%", 1)
end
end
end
-- 将秒转换成显示使用的时间
function FormatSecond(second)
local day = math.floor(second / (24 * 3600))
local minute = math.floor(second / 60) % 60
local sec = second % 60
local hour = math.floor(math.floor(second - day * 24 * 3600 - sec - minute * 60) / 3600)
return string.format(Language[12055], day, hour, minute, sec)
end
-- 将时间戳转换为用于显示的日期字符串
function TimeStampToDateStr(timestamp)
local date = os.date("*t", timestamp)
local cdate = os.date("*t", GetTimeStamp())
if date.year == cdate.year and date.month == cdate.month and date.day == cdate.day then
return string.format("%02d:%02d", date.hour, date.min)
end
return string.format(Language[12056], date.year, date.month, date.day, date.hour, date.min)
end
function TimeStampToDateStr2(timestamp)
local date = os.date("*t", timestamp)
local cdate = os.date("*t", GetTimeStamp())
return string.format(Language[12056], date.year, date.month, date.day, date.hour, date.min)
end
-- 将时间转换为年月日
function TimeStampToDateStr2(timestamp)
local date = os.date("*t", timestamp)
local cdate = os.date("*t", GetTimeStamp())
return date.year..date.month..date.day
end
-- 将时间戳转换为用于显示的日期字符串(时分秒)
function TimeStampToDateStr3(second)
local minute = math.floor(second / 60) % 60
local sec = second % 60
local hour = math.floor(math.floor(second - sec - minute * 60) / 3600)
return string.format("%02d:%02d:%02d", hour, minute, sec)
end
--- 根据id获取资源
function GetResourcePath(id)
return GetLanguageStrById(ConfigManager.GetConfigData(ConfigName.ArtResourcesConfig, id).Name)
end
--- 根据ItemId获取资源名称
function GetSpriteNameByItemId(id)
local artId = ConfigManager.GetConfigData(ConfigName.ItemConfig, id).ResourceID
return GetLanguageStrById(ConfigManager.GetConfigData(ConfigName.ArtResourcesConfig, artId).Name)
end
--- 获取玩家头像资源
function GetPlayerHeadSprite(spLoader, headId)
if headId == 0 then headId = 71000 end
local head = ConfigManager.GetConfigData(ConfigName.ItemConfig, headId)
return spLoader:LoadSprite(GetResourcePath(head.ResourceID))
end
--- 获取玩家头像框资源
function GetPlayerHeadFrameSprite(spLoader, frameId)
if frameId == 0 then frameId = 80000 end
local frame = ConfigManager.GetConfigData(ConfigName.ItemConfig, frameId)
return spLoader:LoadSprite(GetResourcePath(frame.ResourceID))
end
--- 获取玩家头像框资源
function GetPlayerHeadFrameEffect(frameId)
if frameId == 80004 then
return "UI_Effect_TouXiang_Lanlong", 1.1, Vector3.New(-8, -5, 0)
elseif frameId == 80005 then
return "UI_Effect_TouXiang_QingLong", 1.15, Vector3.New(-9, 5, 0)
elseif frameId == 80006 then
return "UI_Effect_TouXiang_BaiHu", 1.15, Vector3.New(-3, -8, 0)
end
end
--是否弹出快捷购买面板
function PopQuickPurchasePanel(type, needNum)
local ownNumber = BagManager.GetItemCountById(type)
if ownNumber >= needNum then
return false
else
UIManager.OpenPanel(UIName.QuickPurchasePanel, { type = type })
return true
end
end
----- SDK数据上报
----- @param context
---- {
---- type, -- required, 调用时机
---- }
function SubmitExtraData(context)
if not AppConst.isSDKLogin then
return
end
local params = SDK.SDKSubmitExtraDataArgs.New()
params.dataType = context.type
params.serverID = tonumber(PlayerManager.serverInfo.server_id)
params.serverName = PlayerManager.serverInfo.name
params.zoneID = tostring(PlayerManager.serverInfo.server_id)
params.zoneName = PlayerManager.serverInfo.name
params.roleID = tostring(PlayerManager.uid)
params.roleName = PlayerManager.nickName
params.roleLevel = tostring(PlayerManager.level)
params.guildID = PlayerManager.familyId
params.Vip = tostring(VipManager.GetVipLevel())
params.moneyNum = BagManager.GetItemCountById(16)
params.roleCreateTime = DateUtils.GetDateTime(PlayerManager.userCreateTime)
params.roleLevelUpTime = context.roleLevelUpTime and DateUtils.GetDateTime(context.roleLevelUpTime) or ""
SDKMgr:SubmitExtraData(params)
end
-- context = {
-- title = "",
-- content = "",
-- confirmCallback = function() end,
-- cancelCallback = function() end,
-- confirmText = "确认",
-- cancelText = "取消",
-- type, -- 单双按钮
-- extra --额外操作
-- }
function ShowConfirmPanel(context)
UIManager.OpenPanel(UIName.CommonConfirmPanel, context)
end
-- 检测某一面板关闭再抛相应的事件
function CallBackOnPanelClose(uiPanel, func)
if UIManager.IsOpen(uiPanel) then
local triggerCallBack
triggerCallBack = function (panelType, panel)
if panelType == uiPanel then
if func then func() end
Game.GlobalEvent:RemoveEvent(GameEvent.UI.OnClose, triggerCallBack)
end
end
Game.GlobalEvent:AddEvent(GameEvent.UI.OnClose, triggerCallBack)
else
if func then func() end
end
end
-- 检测某一面板打开再抛相应的事件
function CallBackOnPanelOpen(uiPanel, func)
if not UIManager.IsOpen(uiPanel) then
local triggerCallBack
triggerCallBack = function (panelType, panel)
if panelType == uiPanel then
if func then func() end
Game.GlobalEvent:RemoveEvent(GameEvent.UI.OnOpen, triggerCallBack)
end
end
Game.GlobalEvent:AddEvent(GameEvent.UI.OnOpen, triggerCallBack)
else
if func then func() end
end
end
-- 通过地图坐标设置物体在UI平面的位置
function SetObjPosByUV(pos)
local u, v = Map_Pos2UV(pos)
local v2 = RectTransformUtility.WorldToScreenPoint(TileMapView.GetCamera(), TileMapView.GetLiveTilePos(u, v))
v2.x = (v2.x)/Screen.width*UIManager.UIWidth - UIManager.Offset.Left
v2.y = (v2.y)/Screen.height*UIManager.UIHeight - UIManager.Offset.Bottom
return v2
end
--获取技能描述
function GetSkillConfigDesc(cfg)
if cfg.DescColor then
local ss = {}
for i=1, #cfg.DescColor do
local str
if cfg.DescColor[i] == 1 then
str=string.format("<color=#4c805eFF>%s</color>", GetLanguageStrById(cfg.DescValue[i]))
elseif cfg.DescColor[i] == 2 then
str=string.format("<color=#b4595eFF>%s</color>", GetLanguageStrById(cfg.DescValue[i]))
else
str=cfg.DescValue[i]
end
ss[i] = str
end
return string.format(GetLanguageStrById(cfg.Desc), unpack(ss))
end
return GetLanguageStrById(cfg.Desc)
end
-- 设置排名所需要的数字框
function SetRankNumFrame(spLoader, rankNum)
local rankNumRes = {
[1] = "r_Dungeon_001",
[2] = "r_Dungeon_002",
[3] = "r_Dungeon_003",
[4] = "r_hero_zhuangbeidi",
}
local resPath = rankNum > 3 and rankNumRes[4] or rankNumRes[rankNum]
local icon = spLoader:LoadSprite(resPath)
return icon
end
function SetSoulEffect(quality,root)
local effect1 = Util.GetGameObject(root,"UI_Effect_Kuang_JinSe")
local effect2 = Util.GetGameObject(root,"UI_Effect_Kuang_HongSe")
local effect3 = Util.GetGameObject(root,"UI_effect_WuCai_Kuang")
if quality then
effect1:SetActive(quality == 5)
effect2:SetActive(quality == 6)
effect3:SetActive(quality == 7)
else
effect1:SetActive(false)
effect2:SetActive(false)
effect3:SetActive(false)
end
end
--- 本方法适用滚动条中 生成item的使用 主要适用每条滚动条item数量不定的滚动条中
--- itemview重设复用 1根节点 2生成到父节点 3item容器 4容器上限 5缩放 6层级 7根据数据类型生成 8不定参数据...
--- type=true 针对表数据为一维数据 type=false 针对表数据为二维数据
function ResetItemView(root,rewardRoot,itemList,max,scale,sortingOrder,type,...)
local args={...}
local data1=args[1]
local data2=args[2]
if itemList[root] then -- 存在
for i = 1, max do
itemList[root][i].gameObject:SetActive(false)
end
if type then
itemList[root][1]:OnOpen(false, {data1,data2},scale,false,false,false,sortingOrder)
itemList[root][1].gameObject:SetActive(true)
else
for i = 1, #data1 do
if itemList[root][i] then
itemList[root][i]:OnOpen(false, {data1[i][1],data1[i][2]},scale,false,false,false,sortingOrder)
itemList[root][i]:Reset({data1[i][1],data1[i][2]},ItemType.Hero,{false,true,true,true})
itemList[root][i].gameObject:SetActive(true)
end
end
end
else -- 不存在 新建
itemList[root]={}
for i = 1, max do
itemList[root][i] = SubUIManager.Open(SubUIConfig.ItemView, rewardRoot)
itemList[root][i].gameObject:SetActive(false)
end
if type then
itemList[root][1]:OnOpen(false, {data1,data2},scale,false,false,false,sortingOrder)
itemList[root][1].gameObject:SetActive(true)
else
for i = 1, #data1 do
itemList[root][i]:OnOpen(false, {data1[i][1],data1[i][2]},scale,false,false,false,sortingOrder)
itemList[root][i]:Reset({data1[i][1],data1[i][2]},ItemType.Hero,{false,true,true,true})
itemList[root][i].gameObject:SetActive(true)
end
end
end
end
--计算字符串长度
function LengthString(inputstr)
-- 计算字符串宽度
-- 可以计算出字符宽度,用于显示使用
if not inputstr or inputstr == "" then
return
end
local lenInByte = #inputstr
local width = 0
local i = 1
while (i<=lenInByte)
do
local curByte = string.byte(inputstr, i)
local byteCount = 1;
if curByte>0 and curByte<=127 then
byteCount = 1 --1字节字符
elseif curByte>=192 and curByte<223 then
byteCount = 2 --双字节字符
elseif curByte>=224 and curByte<239 then
byteCount = 3 --汉字
elseif curByte>=240 and curByte<=247 then
byteCount = 4 --4字节字符
end
local char = string.sub(inputstr, i, i+byteCount-1)
-- print(char)
i = i + byteCount -- 重置下一字节的索引
width = width + 1 -- 字符的个数(长度)
end
return width
end
--都截取长度
function SubString(inputstr,num)
local num = num and num or 0
local str = ""
if not inputstr or inputstr == "" then
return ""
end
local lenInByte = #inputstr
local width = 0
local i = 1
while (i<=lenInByte)
do
local curByte = string.byte(inputstr, i)
local byteCount = 1;
if curByte>0 and curByte<=127 then
byteCount = 1 --1字节字符
elseif curByte>=192 and curByte<223 then
byteCount = 2 --双字节字符
elseif curByte>=224 and curByte<239 then
byteCount = 3 --汉字
elseif curByte>=240 and curByte<=247 then
byteCount = 4 --4字节字符
end
local char = string.sub(inputstr, i, i+byteCount-1)
-- print(char)
str = str..char
i = i + byteCount -- 重置下一字节的索引
width = width + 1 -- 字符的个数(长度)
if width == num then
return (str)
end
end
return str
end
function FixableString(inputstr,num)
if LengthString(inputstr) > num then
return SubString(inputstr,num-1).."..."
else
return inputstr
end
end
--只有英文截取长度
function SubString2(inputstr,num)
if GetCurLanguage() == 0 then
return inputstr
end
if not inputstr or inputstr == "" then
return ""
end
local num = num and num or 0
local str = ""
local lenInByte = #inputstr
local width = 0
local i = 1
while (i<=lenInByte)
do
local curByte = string.byte(inputstr, i)
local byteCount = 1;
if curByte>0 and curByte<=127 then
byteCount = 1 --1字节字符
elseif curByte>=192 and curByte<223 then
byteCount = 2 --双字节字符
elseif curByte>=224 and curByte<239 then
byteCount = 3 --汉字
elseif curByte>=240 and curByte<=247 then
byteCount = 4 --4字节字符
end
local char = string.sub(inputstr, i, i+byteCount-1)
-- print(char)
str = str..char
i = i + byteCount -- 重置下一字节的索引
width = width + 1 -- 字符的个数(长度)
if width == num then
if lenInByte > num then
str = str .. "..."
end
return (str)
end
end
if lenInByte > num then
str = str .. "..."
end
return str
end
function StringConvertToTable(inputstr)
local str = {}
local lenInByte = #inputstr
local i = 1
while (i<=lenInByte)
do
local curByte = string.byte(inputstr, i)
local byteCount = 1;
if curByte>0 and curByte<=127 then
byteCount = 1 --1字节字符
elseif curByte>=192 and curByte<223 then
byteCount = 2 --双字节字符
elseif curByte>=224 and curByte<239 then
byteCount = 3 --汉字
elseif curByte>=240 and curByte<=247 then
byteCount = 4 --4字节字符
end
local char = string.sub(inputstr, i, i+byteCount-1)
-- print(char)
table.insert(str,char)
i = i + byteCount
end
return str
end
function CreatNumberPrefab(inputstr,itemList)
local tempNumList = StringConvertToTable(inputstr)
for i = 1, #itemList do
-- grid.transform:GetChild(i - 1).gameObject:SetActive(false)
if i <= #tempNumList then
itemList[i].text = tempNumList[i]
itemList[i].gameObject:SetActive(true)
else
itemList[i].gameObject:SetActive(false)
end
end
end
--本地文本显示统一调用此接口
function GetLanguageStrById(zhStr)
if languageDic[zhStr] then
if GetCurLanguage() == 0 then
return languageDic[zhStr].zh
elseif GetCurLanguage() == 1 then
return languageDic[zhStr].en
elseif GetCurLanguage() == 2 then
return languageDic[zhStr].vi
else
return languageDic[zhStr].zh
end
end
return zhStr
end
-- 0:中文 1英文
function GetCurLanguage()
if PlayerPrefs.HasKey("language") then
return PlayerPrefs.GetInt("language")
else
return 0
end
end
-- 文字大小比例缩放
FontSizeScaleToZh = {
[0] = 1,
[1] = 0.9,
[2] = 0.8
}
function LanguageFontSizeFilter(text, size)
local l = GetCurLanguage()
text.fontSize = math.floor(size * FontSizeScaleToZh[l])
end
function SetHeroIcon(spLoader, heroData,icon,heroConfig)
-- LogGreen("heroData.skinId:"..tostring(heroData.skinId))
if not heroData.skinId or heroData.skinId == 0 then
icon.sprite = spLoader:LoadSprite(GetResourcePath(heroConfig.Icon))
else
local heroSkinconfig = ConfigManager.GetConfigDataByKey(ConfigName.HeroSkin,"Type",heroData.skinId)
icon.sprite = spLoader:LoadSprite(GetResourcePath(heroSkinconfig.Icon))
end
end
function SetTextVerTial(textCom,vec,alin,posType,nameNum)
if GetCurLanguage() == 0 then
return
end
local textComp = textCom:GetComponent("Text")
textComp.horizontalOverflow = 1
local go = nil
if textCom.transform.parent.name ~= "VerticalTextParent" then
go = GameObject.New()
go.name = "VerticalTextParent"
--LogGreen(tostring(go.name))
go.transform:SetParent(textCom.transform.parent.transform)
go:SetActive(true)
local rectCom = go:GetComponent("RectTransform") or go:AddComponent(typeof(UnityEngine.RectTransform))
rectCom.anchorMin = Vector2.New(0,1)
rectCom.anchorMax = Vector2.New(0,1)
rectCom.localScale = Vector3.one
local weight = textComp.fontSize
rectCom.sizeDelta = Vector2.New(weight,weight)
local pos = rectCom.localPosition
pos.x = weight
pos.y = -weight/2
pos.z = 0
rectCom.anchoredPosition3D = pos
textCom.transform:SetParent(go.transform)
textCom:GetComponent("RectTransform").localPosition = vec
if posType then
textCom:GetComponent("RectTransform").anchoredPosition3D = vec
end
if not alin then
textComp.alignment = "MiddleLeft"
else
textComp.alignment = alin
end
rectCom.rotation = Quaternion.Euler(Vector3.New(0,0,-90))
end
if nameNum then
textComp.text = SubString2(textComp.text,nameNum)
end
end
function SetEnglishActive(go)
if GetCurLanguage() == 0 then
return
end
go.gameObject:SetActive(false)
end
function GetEquipSuitStr(Star,num)
return string.format(Language[12057],num,QualityNameDef[ ConfigManager.GetConfigDataByKey(ConfigName.EquipConfig,"Star",Star).Quality],ConfigManager.GetConfigData(ConfigName.EquipStarsConfig,Star).Stars)
end
-- 绑定扫光特效的资源
function BindLightFlash(light, target)
local meshrender = light.gameObject:GetComponent("MeshRenderer")
local m = meshrender.material
local texture = target.gameObject:GetComponent("Image").mainTexture
m:SetTexture("_Mask", texture)
end
function GetCutOutStr(_str,_num)
if not _str then
return ""
end
local str=""
if LengthString(_str) > _num + 1 then
str=SubString(_str,_num) .. "..."
else
str = _str
end
return str
end
function JingJiShouWeiToEn(_str)
if not _str or _str == "" then
return ""
end
-- --LogGreen("input _str:".._str)
-- if _str == "竞技守卫" then
return GetLanguageStrById(_str)
-- end
-- --LogGreen("return str:".._str)
-- return _str
end
-- 忽略字符串头部的空白字符
function ltrim(input)
return (string.gsub(input, "^[ \t\n\r]+", ""))
end
-- 忽略字符串尾部的空白字符
function rtrim(input)
return (string.gsub(input, "[ \t\n\r]+$", ""))
end
-- 忽略字符串首尾的空白字符
function trim(input)
return (string.gsub(input, "^%s*(.-)%s*$", "%1"))
end
function GetPlayerRoleSingleConFig()
return ConfigManager.GetConfigDataByKey(ConfigName.PlayerRole,"Role",0)
end
-- 网络url转码
function decodeURI(s)
if not s then
return ""
end
s = string.gsub(s, '%%(%x%x)', function(h) return string.char(tonumber(h, 16)) end)
return s
end
function encodeURI(s)
if not s then
return ""
end
s = string.gsub(s, "([^%w%.%- ])", function(c) return string.format("%%%02X", string.byte(c)) end)
return string.gsub(s, " ", "+")
end
-- 通过浏览器打开网页
function OpenWeb(url)
if url then
if ServerConfigManager.IsSettingActive(ServerConfigManager.SettingConfig.INNER_WEB_CONTROL) then
SDKMgr:OpenWeb(url)
else
UnityEngine.Application.OpenURL(url)
end
end
end