[收集記錄Unity|HTML|Python...]小業務功能實現。來不及解釋了,先造個輪子後開車

臉嫩(。・ω・。),請輕碾

  • 不定期更新。收集記錄日常使用到的小業務功能實例代碼,來源爲自己創作、網上查找、博主提供等。記錄爲主,直接貼代碼,不做過多的註解。
  • 從評論區中收集整理。請在評論區留下你們要實現的小功能,說不定有大佬知道怎麼實現呢;亦可放輪子出來遛倆圈,直接貼上或有趣或實用的功能函數代碼。Come on,show me your code!
  • 不限於Unity、HTML、Python。

歡迎各位大佬交流指正,互通有無。本人小萌新一枚,技術及學識水平都很一般,防ETC!!!
(((┏(; ̄▽ ̄)┛裝完逼就跑

防ETC,自動擡槓。濤姐看場,心裏不慌。


Unity

一、界面相關

1、獲取圓形排列物體的位置 (lua)

  • 網上找不到實現這個功能的方法,自己通過畫圖推演後,得出了這個代碼
  • 根據物體數量等信息,計算出圓形排列物體的位置。如:抽獎轉盤上的道具排列顯示
-- 計算圓盤上道具的擺放位置
-- 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

2、unity ngui的scrollview滑動到目標項操作 (lua)

  • 這是我目前爲止,計算較準確、通用性較好的一版處理
-- 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

3、unity 屏幕座標&世界座標相互轉換

--世界座標轉屏幕座標:
Vector3 screenPos = Camera.main.WorldToScreenPoint(pos)
--屏幕座標轉世界座標:
Vector3 worldPos = Camera.main.ScreenToWorldPoint(pos)

二、數值處理

1、超過1萬的數值按精度格式化顯示 (lua)

  • 可指定保留幾位小數
-- 獲取格式化數字,保留小數後幾位;默認不四捨五入;默認保留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

2、將時間戳以自定義格式顯示 (lua)

--顯示爲:“剩餘時間: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

HTML


Python


Java


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章