脸嫩(。・ω・。),请轻碾
欢迎各位大佬交流指正,互通有无。本人小萌新一枚,技术及学识水平都很一般,防ETC!!!
(((┏(; ̄▽ ̄)┛装完逼就跑
-- 计算圆盘上道具的摆放位置
-- 1.圆半径 2.圆心坐标(可空) 3.道具数量 4.道具概率表
function CalcRotaryPlateItemPos( pRadius, pCircCntr, pAwdNum, pAwdRateTbl )
if (pAwdNum == nil and pAwdRateTbl == nil) or pRadius == nil then return end
local circleCenter = pCircCntr and pCircCntr or {0,0}
-- 获取道具权重
local rateTbl = {}
local ttlRate = 0
if pAwdRateTbl and next(pAwdRateTbl) then
rateTbl = pAwdRateTbl
ttlRate = table.nums(pAwdRateTbl)
else
for i=1,pAwdNum do
table.insert(rateTbl, 1)
end
ttlRate = pAwdNum
end
local passTtlAngle = 0 -- 在此之前道具权重的所占的角度
local tgtPosTbl = {}
local posAngleTbl = {} -- 道具所处位置的角度表
local zPos = nil
for k,v in pairs(rateTbl) do
local zCurRateAnlge = v / ttlRate * 360 -- 当前道具权重占的角度
local zCurRateHalfAngle = zCurRateAnlge / 2 -- 当前道具权重占的1/2角度
local zCurPassTtlAngle = 0 -- 当前道具权重占的1/2角度与passTtlAngle的和
if passTtlAngle == 0 then -- 特殊位置,圆心与此道具位置方向作坐标轴的y轴
zPos = { circleCenter[1], circleCenter[2] + pRadius}
passTtlAngle = zCurRateHalfAngle
else
-- 道具是处于权重圆饼块的角平分线上的,因此计算道具所处角度,需权重占的1/2角度
zCurPassTtlAngle = passTtlAngle + zCurRateHalfAngle
passTtlAngle = passTtlAngle + zCurRateAnlge
-- 计算坐标,先处理特殊坐标
if zCurPassTtlAngle == 90 then
zPos = { circleCenter[1] + pRadius , circleCenter[2] }
elseif zCurPassTtlAngle == 180 then
zPos = { circleCenter[1], circleCenter[2] - pRadius }
elseif zCurPassTtlAngle == 270 then
zPos = { circleCenter[1] - pRadius , circleCenter[2] }
else
-- 获取正负号:通过数学定律,以角度为值,函数sin可计算出x轴的正负,cos则为y轴的
local sinVl = math.sin(math.rad(zCurPassTtlAngle))
local cosVl = math.cos(math.rad(zCurPassTtlAngle))
local plusMinusX = 1
local plusMinusY = 1
if sinVl ~= 0 then
plusMinusX = sinVl / math.abs(sinVl)
end
if cosVl ~= 0 then
plusMinusY = cosVl / math.abs(cosVl)
end
local zTgtAngle = 0
-- 通过道具位置点作线垂直于y轴,使用正余弦函数计算,观察所得不同区间用于最终计算的角度值的获取方式不一样
if (zCurPassTtlAngle > 90 and zCurPassTtlAngle < 180) or ( zCurPassTtlAngle > 270 and zCurPassTtlAngle < 360 ) then
zTgtAngle = 90 - (zCurPassTtlAngle % 90)
else
zTgtAngle = zCurPassTtlAngle % 90
end
local zTgtLenX = math.sin( math.rad( zTgtAngle ) ) * pRadius
local zTgtLenY = math.cos( math.rad( zTgtAngle ) ) * pRadius
zPos = { circleCenter[1] + zTgtLenX * plusMinusX, circleCenter[2] + zTgtLenY * plusMinusY }
end
end
table.insert(tgtPosTbl, zPos)
table.insert(posAngleTbl, zCurPassTtlAngle)
end
return tgtPosTbl, posAngleTbl
end
-- scrollView 上下滑动到目标项
-- 1.目标序号 2.scrollview游戏体 3.内容最大数量 4.滑动组件初始Y轴 5.网格游戏体 6.可视数量
function UpDownScrollTo( pIdx, pScrVGo, pMaxNum, pScrVInitHgt, pGridGo, pViewNum )
if pIdx == nil or Slua.IsNull(pScrVGo) then return end
if pIdx == 0 then
pIdx = 1
end
local showNum = pViewNum and pViewNum or 4
local initHeight = pScrVInitHgt and pScrVInitHgt or 0
local itemHeight = 0
local pnlHgt = pScrVGo:GetComponent("UIPanel").height
if not Slua.IsNull(pGridGo) then
itemHeight = pGridGo:GetComponent("UIGrid").cellHeight
showNum = math.floor( pnlHgt/ itemHeight)
end
local sprPanel = pScrVGo.transform:GetComponent("SpringPanel")
if sprPanel == nil then
sprPanel = pScrVGo:AddComponent("SpringPanel")
end
local numFlowUi = (pMaxNum-showNum) <= 0 and 0 or pMaxNum-showNum -- 被clip掉的选项总数
local deltaVl = pnlHgt - showNum * itemHeight
local maxCanMove = numFlowUi * itemHeight + initHeight - deltaVl -- 最大可上拉的位置
maxCanMove = maxCanMove <= 0 and 0 or maxCanMove
local tgtX = pScrVGo.transform.localPosition.x
local tgtY = math.min((initHeight + (pIdx-1)*itemHeight), maxCanMove)
sprPanel.Begin(pScrVGo.gameObject, Vector3(tgtX, tgtY, 0), 10)
end
--世界坐标转屏幕坐标:
Vector3 screenPos = Camera.main.WorldToScreenPoint(pos)
--屏幕坐标转世界坐标:
Vector3 worldPos = Camera.main.ScreenToWorldPoint(pos)
-- 获取格式化数字,保留小数后几位;默认不四舍五入;默认保留1位小数
-- 1.pNum数字,2.zPrecision精度,小数点后几位,3.isHalfAdjust是否四舍五入,
-- 4.isNguiChar用于字体图集显示
function GetNumFormat( pNum,zPrecision,isHalfAdjust,isNguiChar )
local zDecimalRate = zPrecision and zPrecision or 1 -- 小数位,最后需要处理的位数
local zFirstRate = 0 -- 第一步需要去掉的位数,可作四舍五入处理
pNum = tonumber(pNum)
if pNum >= 100000000 then
zFirstRate = (8 - zDecimalRate) <= 0 and 1 or (8 - zDecimalRate)
local resultVl = 0
if isHalfAdjust then
resultVl = math.floor(pNum/10^zFirstRate + 0.5)/10^zDecimalRate
else
resultVl = math.floor(pNum/10^zFirstRate)/10^zDecimalRate
end
local _,zDecimal = math.modf(resultVl)
local strUnit = isNguiChar and "Y" or "亿"
if zDecimal == 0 then
return string.format("%d%s",resultVl,strUnit)
else
local formatStr = "%."..zDecimalRate.."f"..strUnit
return string.format(formatStr,resultVl)
end
elseif pNum >= 10000 then
zFirstRate = (4 - zDecimalRate) <= 0 and 1 or (4 - zDecimalRate)
local resultVl = 0
if isHalfAdjust then
resultVl = math.floor(pNum/10^zFirstRate + 0.5)/10^zDecimalRate
else
resultVl = math.floor(pNum/10^zFirstRate)/10^zDecimalRate
end
local _,zDecimal = math.modf(resultVl)
local strUnit = isNguiChar and "W" or "万"
if zDecimal == 0 then
return string.format("%d%s",resultVl,strUnit)
else
local formatStr = "%."..zDecimalRate.."f"..strUnit
return string.format(formatStr,resultVl)
end
else
return pNum
end
end
--显示为:“剩余时间:X天XX小时XX分钟”
--或者 :“剩余时间:X天XX小时XX分钟XX秒”
function GetFormatTime( time, showSec, isShortDesc )
local zDay = math.floor(time / 3600 / 24)
local zHour = math.floor((time - zDay*24 * 3600) / 3600)
local zMinute = 0
local strSec = ""
if showSec then
zMinute = math.floor((time - zDay*24 * 3600 - zHour*3600) / 60)
local zSecond = time - zDay*24 * 3600 - zHour * 3600 - zMinute * 60
if zSecond >= 0 then strSec = (zSecond).."秒" end
else -- 只显示到分钟的,应向上取整
zMinute = math.ceil((time - zDay*24 * 3600 - zHour*3600) / 60)
end
local zText = ""
if zDay > 0 then
zText = table.concat( {zText,(zDay),"天"} )
if (zDay > 0 and zHour == 0) or (zHour > 0) then
zText = table.concat( {zText,(zHour),(isShortDesc and "时" or "小时")} )
end
if zMinute >= 0 then
zText = table.concat( {zText,(zMinute),(isShortDesc and "分" or "分钟") } )
end
else
if zHour > 0 then
zText = table.concat( {zText,(zHour),(isShortDesc and "时" or "小时") } )
end
if zMinute > 0 then
zText = table.concat( {zText,(zMinute),(isShortDesc and "分" or "分钟") } )
end
end
zText = table.concat( {zText,strSec} )
return zText
end