[收集记录Unity|HTML|Python...]小业务功能实现。来不及解释了,先造个轮子后开车

2 篇文章 0 订阅

脸嫩(。・ω・。),请轻碾

  • 不定期更新。收集记录日常使用到的小业务功能实例代码,来源为自己创作、网上查找、博主提供等。记录为主,直接贴代码,不做过多的注解。
  • 从评论区中收集整理。请在评论区留下你们要实现的小功能,说不定有大佬知道怎么实现呢;亦可放轮子出来遛俩圈,直接贴上或有趣或实用的功能函数代码。Come on,show me your code!
  • 不限于Unity、HTML、Python。

欢迎各位大佬交流指正,互通有无。本人小萌新一枚,技术及学识水平都很一般,防ETC!!!
(((┏(; ̄▽ ̄)┛装完逼就跑

防ETC,自动抬杠。涛姐看场,心里不慌。


Unity

一、界面相关

1、获取圆形排列物体的位置

1.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
1.2 使用transform.RotateAround实现(c#)
  • 终于发现了官方的这个函数方法 – 2021.04.28更新记录
  • public void RotateAround(Vector3 point, Vector3 axis, float angle);
    • point:要围绕的点
    • axiw:要围绕那个点的哪条轴转
    • angel:旋转的角度
      在这里插入图片描述在这里插入图片描述

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

3、复制表

function DeepCopy(obj)
    local InTable = {}
    local function Func(obj)
        if type(obj) ~= "table" then
            return obj
        end
        local NewTable = {}
        InTable[obj] = NewTable 
        for k,v in pairs(obj) do 
            NewTable[Func(k)] = Func(v)
        end
        return setmetatable(NewTable, getmetatable(obj))
    end
    return Func(obj)
end

HTML


Python


Java


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值