lua 一些简单工具的封装,方便以后查阅

utils = {}--点击事件绑定
function utils.addclickevent(go, func, args)LuaListener.Get(go, args).onClick = func
end--拖拽事件绑定
function utils.addonPointerDown(go, func)--按下DragListener.Get(go).onPointerDown = func
end
function utils.addonPointerUp(go, func)--抬起DragListener.Get(go).onPointerUp = func
end
function utils.addonDrag(go, func)DragListener.Get(go).onDrag = func
end
function utils.addonBeginDrag(go, func)DragListener.Get(go).onBeginDrag = func
end
function utils.addonEndDrag(go, func)DragListener.Get(go).onEndDrag = func
end
function utils.addchangeevent(go, func, id)Util.AddChangeEvent(go, id or 0, func)
end
function utils.addsubmitevent(go, func)Util.AddEditEndEvent(go.gameObject, func)
end-- 拖曳的时候转换坐标用的
-- eventData是拖曳的时候的生成的,类型是UnityEngine.EventSystems.PointerEventData
-- rect_transform是需要转换坐标的的目标rectTransform
-- 最后返回转换后的一个Vector2
function utils.ScreenPointToLocalPointInRectangle(eventData, rect_transform)local Vector2 = Util.ScreenPointToLocalPointInRectangle(eventData, rect_transform)return Vector2
end-- 增加spine骨骼动画完成事件回调
function utils.AddSpineEvent(go, func)Util.AddSpineEvent(go, func)
end-- 以异常捕获的方式执行指定函数
function utils.xpcall(func, ...)if not LOGGABLE thenfunc()returnendlocal ok, msg = xpcall(func, debug.traceback, ...)if not ok thenlogerror(msg)end
end-- 获得指定秒数的日时分秒
function utils.second2DHMS(second)if second <= 0 thenreturn 0, 0, 0, 0endlocal d = math.floor(second / 86400)second = second - d * 86400local h = math.floor(second / 3600)second = second - h * 3600local m = math.floor(second / 60)second = second - m * 60local s = secondreturn d, h, m, s
end--设置子物体在服务体位置
function utils.setsiblingindex(trans, index)trans:SetSiblingIndex(index)
end--设置子物体在父物体的最上层
function utils.settopindex(trans)local childcount = trans.parent.childCounttrans:SetSiblingIndex(childcount)
end--设置指定物体的Postion3D值
function utils.setobjpostion(go, x, y, z)if go thengo.transform.localPosition = Vector3.New(x, y, z)end
end--设置指定物体的激活状态
function utils.setactive(go, ac)if go thenif go.SetActive thengo:SetActive(ac)elsego.gameObject:SetActive(ac)endend
end--判断物体的激活状态,并返回一个bool值
function utils.isactive(go)if go thenreturn go.activeSelfendreturn false
end-- 组件是否可用
function utils.interactable(com,inter)if com thencom.interactable = interend
end--添加子物体到父物体上,并设定一个层级位置
function utils.addchild(parent, prefab, index)if prefab == nil or parent == nil thenlog("Failed to add child!!")return nilendlocal go = GameObject.Instantiate(prefab)go.transform:SetParent(parent.transform)go.transform.localScale = prefab.transform.localScalego.transform.localPosition = prefab.transform.localPositionif index thengo.transform:SetSiblingIndex(index)endreturn go
end-- 实例化一个预设对象,并挂载到父节点
function utils.instantiate(parent, prefab)if prefab == nil or parent == nil thenlog("Failed to instantiate child!!")return nilendreturn GameObject.Instantiate(prefab, parent.transform)
end-- 实例化一个预设新对象
function utils.newobject(prefab)return GameObject.Instantiate(prefab)
end-- 销毁一个对象
function utils.destroy(go)GameObject.Destroy(go)
end-- 设置组件的父组件
function utils.setparent(go,parent)if go thengo.transform:SetParent(parent)end
end-- 销毁指定节点的所有子节点
function utils.clearChilds(go)Util.ClearChild(go.transform, true)
end
-- 隐藏指定节点的所有子节点
function utils.hideChilds(go)Util.ClearChild(go.transform, false)
end
-- 获取指定GameObject对象的指定类型的组件Component
function utils.getcom(go, type)return go.transform:GetComponent(type)
end-- 获取指定GameObject对象的RectTransform组件
function utils.getrect(go)return go.transform:GetComponent("RectTransform")
end-- 获取指定名字的GameObject
function utils.findall(node)return GameObject.Find(node)
end-- 获取所有指定tag的GameObject[]
function utils.findalltag(tag)return GameObject.FindGameObjectsWithTag(tag)
end-- 获取指定名字的节点的transform
function utils.findchild(go, node)if not go or not node thenreturn nilendreturn go.transform:Find(node)
end-- 获取指定名字的节点的gameObject
function utils.findobj(go, node)local child = utils.findchild(go, node)if child thenreturn child.gameObjectendreturn nil
end-- 获取指定名字的节点的指定类型组件Component
function utils.findcom(go, node, type)local obj_trans = utils.findchild(go, node)if not obj_trans thenreturn nilendreturn obj_trans:GetComponent(type)
end-- 获取指定名字的节点的RectTransform组件
function utils.findrect(go, node)return utils.findchild(go, node):GetComponent("RectTransform")
end-- 获取指定名字节点的Image组件
function utils.findimage(go, node)return utils.findcom(go, node, "Image")
end--获取指定的按钮
function utils.findbutton(go, node)return utils.findcom(go, node, "Button")
end--获取指定的开关
function utils.findtoggle(go, node)return utils.findcom(go, node, "Toggle")
end-- 获取指定名字节点的RawImage组件
function utils.findrawimage(go, node)return utils.findcom(go, node, "RawImage")
end-- 获取指定名字节点的Text组件
function utils.findtext(go, node)return utils.findcom(go, node, "Text")
end-- 获取指定名字节点的InputField组件
function utils.findinput(go, node)return utils.findcom(go, node, "InputField")
end-- 获取一个序列帧动画组件
function utils.findani(go, node)return utils.findcom(go, node, "UIImageAnimation")
end-- 获取一个指定组件的CanvasGroup组件
function utils.getcanvas(go)return go:GetComponent("CanvasGroup")
end
-- 加载一个sprite
function utils.loadsprite(module, name)local path = asseturi.getimagepath(module, name)return Asset:LoadSprite(path)
end-- 加载预设
function utils.loadprefab(module, name)local path = asseturi.getassetpath(module, name)return Asset:LoadAsset(path)
end-- 加载并实例化预设
function utils.loadinstobj(module, name, parent)local path = asseturi.getassetpath(module, name)local prefab = Asset:LoadAsset(path)return utils.addchild(parent, prefab)
endutils.imagegray_material = nil
-- 设置图片image为灰色
function utils.setimagegray(_image, _isgray)if not _image thenreturnendlocal material = _image.materialif _isgray thenif not utils.imagegray_material thenutils.imagegray_material = Asset:LoadMaterial("shader/ImageGray_Material.mat")end_image.material = utils.imagegray_materialelseif not _isgray then_image.material = nilend
end-- 设置某文本为灰色,支持文本
function utils.settextgray(_text, _isgray)if _text and _isgray then_text.color = Color.New(117,117,117,255)end
end-- 设置某节点为灰色,支持image和text (需要一个材质球)
function utils.setobjgray(_gameObject, _isgray, _exclude_names)if not _gameObject thenreturnendlocal ishave_exclude = function(_objname)if not _objname or not _exclude_names thenreturn falseendfor k,v in ipairs(_exclude_names) doif v == _objname thenreturn trueendendreturn falseendlocal set_trans_grayset_trans_gray = function (_trans,_isgray)if not _trans thenreturnendlocal image_com = _trans:GetComponent("Image")if not image_com thenimage_com = _trans:GetComponent("RawImage")endif image_com thenif not ishave_exclude(image_com.gameObject.name) thenutils.setimagegray(image_com,_isgray)endelselocal text_com = _trans:GetComponent("Text")if text_com thenif not ishave_exclude(text_com.gameObject.name) thenutils.settextgray(text_com,_isgray)endendend--local childcount = _trans.childCountif childcount == 0 thenreturnendfor i=0,childcount-1 do-- 递归调用set_trans_gray(_trans:GetChild(i),_isgray)endendset_trans_gray(_gameObject.transform, _isgray)
end-- 获取table的长度
function utils.tablelen(_table)if not _table thenreturn 0endlocal len = 0for _, t in pairs(_table) doif t thenlen = len + 1endendreturn len
end-- 获取指定value在table中的索引
function utils.tableobjindex(_table, _obj)if not _table or not _obj thenreturn nilendfor k, v in pairs(_table) doif v == _obj thenreturn kendendreturn nil
end-- 判断两个数组是否相等
function utils.tablecheckequal(_tab1,_tab2)if _tab1 == _tab2 thenreturn trueendif not _tab1 or not _tab2 thenreturn falseendif #_tab1 ~= #_tab2 thenreturn falseendfor k,v in pairs(_tab1) doif _tab2[k] ~= v thenreturn falseendendreturn true
end-- 字符串分割函数
function utils.split(_szFullString, _szSeparator)local _nFindStartIndex = 1local _nSplitIndex = 1local _nSplitArray = {}while true dolocal _nFindLastIndex = string.find(_szFullString, _szSeparator, _nFindStartIndex)if not _nFindLastIndex then_nSplitArray[_nSplitIndex] = string.sub(_szFullString, _nFindStartIndex, string.len(_szFullString))breakend_nSplitArray[_nSplitIndex] = string.sub(_szFullString, _nFindStartIndex, _nFindLastIndex - 1)_nFindStartIndex = _nFindLastIndex + string.len(_szSeparator)_nSplitIndex = _nSplitIndex + 1endreturn _nSplitArray
end--设置子物体到对应父物体
function utils.setuiparent(child, parent)if child and parent thenchild:GetComponent("RectTransform"):SetParent(parent.transform)child:GetComponent("RectTransform").anchoredPosition = Vector3.New(0, 0, 0)end
end--常用颜色
utils.colors = {black = Color.New(0,0,0,1),white = Color.New(1,1,1,1),red = Color.New(1,0,0,1),green = Color.New(0,1,0,1),lucency = Color.New(0,0,0,0)        --透明
}-- 修改文本的颜色
-- 传入参数文本组件,文本颜色,描边颜色
function utils.changeTextColor(text,txtColor,outlineColor)if text and txtColor thentext.color = txtColorlocal outline = utils.getcom(text.gameObject,"Outline")if outline and outlineColor thenoutline.effectColor = outlineColorendelselogerror("changeTextColor Error"..text.name)end
end--通配符替换
function utils.format(fmt, tab)if tab thenreturn string.format(fmt, unpack(tab))elsereturn fmtend
end--表克隆备份
function table_clone(_table)local _temp = {}if table.getn(_table) > 0 thenfor k, v in pairs(_table) dotable.insert(_temp, v)endendreturn _temp
end-- 取指定字符串的子串
function utils.substring(_src,_index,_isnum)local substr = string.sub(tostring(_src), _index)if _isnum thenreturn tonumber(substr)endreturn substr
end-- 字符串倒序
function utils.stringReverse(str)local tmp = {}for i = string.len(str), 1, -3 dolocal ss = string.sub(str, i - 2, i)table.insert(tmp, ss)endreturn table.concat(tmp, "")
end-- 字符串长度,根据需要自行做了一些处理
function utils.stringUtf8Len(_str)local _lenInByte = #_strlocal _width = 0local _count = 1while (_count <= _lenInByte) dolocal curByte = string.byte(_str, _count)local byteCount = 1if curByte > 0 and curByte <= 127 thenbyteCount = 1_width = _width + 1 -- 1字节字符elseif curByte >= 192 and curByte < 223 thenbyteCount = 2_width = _width + 2 -- 双字节字符elseif curByte >= 224 and curByte < 239 thenbyteCount = 3_width = _width + 2 -- 汉字elseif curByte >= 240 and curByte <= 247 thenbyteCount = 4_width = _width + 2 -- 4字节字符endlocal char = string.sub(_str, _count, _count + byteCount - 1)_count = _count + byteCount -- 重置下一字节的索引-- width = width + byteCount                                             -- 字符的个数(长度)endreturn _width
end-- 查找字符串中是否包含指定内容
function utils.isStrContain(str, content)if str == nil or str == "" thenreturn falseendif content == nil or content == "" thenreturn falseendlocal idx, _ = string.find(str, content)-- 没有则idx是nilif not idx thenreturn falseendreturn true
end-- 字符串格式化,去掉不需要的字符
function utils.trim(s)return (s:gsub("^%s*(.-)%s*$", "%1"))
end-- 检查合法字符
function utils.checkName(_newname, _max, _prefix)_max = _max or 7if _newname == nil or _newname == "" thenreturn false, "不能为空字符"endif utils.isStrContain(_newname, " ") thenreturn false, "有空格"end_newname = utils.trim(_newname)local _, _count = string.gsub(_newname, "[\128-\193]", "")local _1, _count1 = string.gsub(_newname, "[%w_]", "")local _total = utils.stringUtf8Len(_newname)if _total > _count + _count1 thenreturn false, "有非法字符"endif _count / 2 + _count1 < 2 thenreturn false, "长度太短"endif _count / 2 + _count1 > _max thenreturn false, "长度太长"endif  pbfilter.isWarningInPutStr(_newname) thenreturn false, "含有敏感字"endreturn true, _newname
endfunction utils.checkInputStr(_newstr, _max)_max = _max or 20local _newstr = utils.trim(_newstr)if  pbfilter.isWarningInPutStr(_newstr) thenreturn false, "含有敏感字"endreturn true, _newstr
end--把数字转K,M,B显示
--1K=一千,1M=一百万,1B=十亿
function utils.number_to_kmb(number)local _number = tonumber(number)local _str = ""if _number>=1000 then_number = math.modf( _number / 1000 )  _str="K"endif _number>=1000 then_number = math.modf( _number / 1000 ) _str="M"endif  _number>=1000 then_number = math.modf( _number / 1000 )_str = "B"end_str = string.format(_number.. _str) return _str
end

结语

最新上传了一个lua 代码基于c#DOTween思路制作的一个animation常用动画库(类似于css animation.css库)。

lua Utils工具(持续更新)相关推荐

  1. 整理Java相关的工具类Utils,持续更新中,建议收藏【目前更新至24】

    文章目录 1.BigDecimalUtil 2.CaptchaUtil 图片验证码工具类 3.CoordinateTransformUtil 坐标系转换工具类 4.DateUtil 日期加减工具类 5 ...

  2. 微生物文献调研网站与工具---持续更新(2022.09.16)

    全文目录 1. 全文说明 2. 文献查询网站 2.1 常用期刊查询官网 1) 高质量的期刊是值得细细品味,期刊官网可以进行长期订阅跟踪(排名不分先后) 2)文献库检索网站 3)科研工作的持续跟进与最新 ...

  3. ( 持续更新,目前含 200+ 工具类 ) DevUtils 是一个 Android 工具库, 主要根据不同功能模块,封装快捷使用的工具类及 API 方法调用。

    DevUtils GitHub About ( 持续更新,目前含 200+ 工具类 ) Roadmap DevUtils 是一个 Android 工具库,主要根据不同功能模块,封装快捷使用的工具类及 ...

  4. 前端常用的 59 个工具类【持续更新】

    #前端常用的 59 个工具类[持续更新] 前言 前端开发有时会处理一部分后台返回的数据,或者根据数据判断做一些处理; 这个时候就非常有必要将一些常用的工具类封装起来; 本文根据常用的一些工具类封装了 ...

  5. (持续更新, 目前含100+工具类) DevUtils 是一个 Android 工具库

    DevUtils Github About (持续更新, 目前含100+工具类) DevUtils 是一个 Android 工具库, 主要根据不同功能模块,封装快捷使用的工具类及 API 方法调用. ...

  6. Java实现动态加载页面_[Java教程]动态加载页面数据的小工具 javascript + jQuery (持续更新)...

    [Java教程]动态加载页面数据的小工具 javascript + jQuery (持续更新) 0 2014-05-07 18:00:06 使用该控件,可以根据url,参数,加载html记录模板(包含 ...

  7. VersionEye开源持续更新工具

    VersionEye开源了帮助更新项目依赖项的同名持续集成工具.该工具提出了"持续更新(continuous updating)"的概念,它可以提供许多软件库的更新通知.许可检查和 ...

  8. android 开发工具类,Android中常用开发工具类—持续更新...

    一.自定义ActionBar public class ActionBarTool { public static void setActionBarLayout(Activity act,Conte ...

  9. cesium面积计算_GitHub - BulletYuan/bulletCesium: GIS可视化——基于Cesiumjs的一些工具类,测量距离、测量面积。持续更新......

    bulletCesium created at 2018.8.13 基于Cesiumjs的一些工具类,持续更新. lastest - 2018.8.24 - bulletCesium-1.1.1 增加 ...

最新文章

  1. org.apache.ibatis.binding.BindingException: Type interface XXX is not known to the MapperRegistry.
  2. NLP深度学习:近期趋势概述
  3. http://nancyfx.org + ASPNETCORE
  4. maven build后Downloading maven-metadata.xml
  5. day06【后台】两套分配
  6. 利用Pattern和Mather来禁止特殊字符的输入
  7. latex 插图排版
  8. LayaAir 2.0 开发 2048 小游戏
  9. paip.C#.net TIMER不起作用在用户控件中
  10. 极路由1S HC5661A 刷入不死u-boot(breed)加刷潘多拉固件教程
  11. 电容或电感的电压_低成本电容电感测量电路
  12. 云计算到底有哪些魅力 云计算就业前景好不好
  13. matlab 小波的分解与重构
  14. 介绍一款搜索引擎(Magi):也就比百度好用一丢丢
  15. 京东接口对接流程(以下举例物流接口):
  16. 百度地图API爬取不同类型POI的详细数据
  17. 淘宝平台自研系统入驻流程
  18. 【微电子】半导体器件物理:1-1半导体材料与晶体结构
  19. Android录音器(可暂停、续录、续播)完整Demo
  20. 笔记12:JQuery

热门文章

  1. 基本磁场计算公式的简单推导
  2. springdatajpa配置多数据源
  3. 云端存储服务企业就在企业云盘
  4. LightGBM算法
  5. 顺序查找算法C语言实现
  6. python pct_change_在pct_change()和缺失值之前重新采样
  7. JavaScript页面后退或关闭
  8. form表单提交List集合
  9. 值得前端工程师学习的团队沟通话术
  10. 锁屏界面 google账号解锁