local GameObject = GameObject local Util = Util local LuaEventListener = LuaEventListener local LuaClickListener = LuaClickListener local LuaDragListener = LuaDragListener local LuaDragExtendListener = LuaDragExtendListener local WordManager = WordManager --设置position位置 function SetGlobalPosition(transform, x, y, z) if transform then Util.SetPosition(transform, x, y, z) end end function SetGlobalPosition3D(transform, x, y, z) if transform then Util.SetPosition(transform, x, z, y) end end function SetGlobalPositionX(transform, x) if transform then x = x or 0 Util.SetPositionX(transform, x) end end function SetGlobalPositionY(transform, y) if transform then y = y or 0 Util.SetPositionY(transform, y) end end function SetGlobalPositionZ(transform, z) if not IsNull(transform) then z = z or 0 Util.SetPositionZ(transform, z) end end function GetGlobalPosition( transform ) if not transform then return end return Util.GetPosition(transform) end function GetGlobalPositionXY( transform ) if not transform then return end return transform:GetLocalPosXY() end function GetGlobalPositionX(transform) return Util.GetPositionX(transform) end function GetGlobalPositionY(transform) return Util.GetPositionY(transform) end function GetGlobalPositionZ(transform) return Util.GetPositionZ(transform) end --设置localPosition位置 function SetLocalPosition(transform, x, y, z) if transform then x = x or 0 y = y or x z = z or x Util.SetLocalPosition(transform, x, y, z) end end function SetLocalPositionX(transform, x) if transform then x = x or 0 Util.SetLocalPositionX(transform, x) end end function SetLocalPositionY(transform, y) if transform then y = y or 0 Util.SetLocalPositionY(transform, y) end end function SetLocalPositionZ(transform, z) if transform then z = z or 0 Util.SetLocalPositionZ(transform, z) end end function GetAnchoredPosition( transform ) if not transform then return end return Util.GetAnchoredPosition(transform) end function SetAnchoredPosition( transform, x, y ) if not transform then return end return Util.SetAnchoredPosition(transform, x, y) end function SetAnchoredPositionX( transform, x ) if not transform then return end local old_x, old_y = GetAnchoredPosition(transform) return Util.SetAnchoredPosition(transform, x, old_y) end function SetAnchoredPositionY( transform, y ) if not transform then return end local old_x, old_y = GetAnchoredPosition(transform) return Util.SetAnchoredPosition(transform, old_x, y) end function GetLocalPosition( transform ) if not transform then return end return Util.GetLocalPosition(transform) end function GetLocalPositionX(transform) return Util.GetLocalPositionX(transform) end function GetLocalPositionY(transform) return Util.GetLocalPositionY(transform) end function GetLocalPositionZ(transform) return Util.GetLocalPositionZ(transform) end --设置localscale function SetLocalScale(transform, x, y, z) if transform then x = x or 1 y = y or x z = z or x Util.SetLocalScale(transform, x, y, z) end end function GetLocalScale(transform) if transform then return Util.GetLocalScale(transform) end end --获取包围盒的y值 function GetRenderBoundsSize(render) if render then return Util.GetRenderBoundsSize(render) end end --设置localRotation function SetLocalRotation(transform, x, y, z, w) if transform then x = x or 0 y = y or x z = z or x w = w or 1 Util.SetLocalRotation(transform, x, y, z, w) end end function GetLocalRotation(transform) if transform then return Util.GetLocalRotation(transform) end end --实际是调用Rotate()方法,在原有角度基础上,继续旋转 function SetRotate(transform, x, y, z) if transform then Util.SetRotate(transform, x, y, z) end end function SetSizeDelta(transform, x, y) if transform then Util.SetSizeDelta(transform, x, y) end end function SetSizeDeltaX(transform, x) if transform then Util.SetSizeDeltaX(transform, x) end end function SetSizeDeltaY(transform, y) if transform then Util.SetSizeDeltaY(transform, y) end end function GetSizeDeltaXY(transform) if transform then return transform:GetDeltaSizeXY() end end function GetSizeDeltaX(transform) if transform then return Util.GetSizeDeltaX(transform) end end function GetSizeDeltaY(transform) if transform then return Util.GetSizeDeltaY(transform) end end function SetTMPSharedMaterial(tmp_text, mat_type) if tmp_text then local temp_mat = ShaderTools.GetTMPSharedMaterial( mat_type ) if temp_mat then tmp_text.fontSharedMaterial = temp_mat end end end function SetBgBySprite( self, img, bg_res, is_auto_size, loaded_call_back ) if not img then return end img.gameObject:SetActive(false) local on_loaded = function ( ) img.gameObject:SetActive(true) if loaded_call_back then loaded_call_back() end end lua_resM:setOutsideImageSprite(self, img, GameResPath.GetViewBigBg(bg_res), is_auto_size, on_loaded) end function SetBg( self, img, bg_res, is_auto_size, loaded_call_back ) if not img then return end img.gameObject:SetActive(false) local on_loaded = function ( ) img.gameObject:SetActive(true) if loaded_call_back then loaded_call_back() end end lua_resM:setOutsideRawImage(self, img, GameResPath.GetViewBigBg(bg_res), is_auto_size, on_loaded) end --添加点击事件 function AddClickEvent(target, call_back, use_sound, support_up_event) if not IsNull(target) then use_sound = use_sound or LuaSoundManager.SOUND_UI.CLICK if use_sound < 100 then use_sound = LuaSoundManager.SOUND_UI.CLICK end --[[ support_up_event 按钮抬起时执行回调,默认为false; 基于点击按钮边缘缩放后无响应的优化,需要侦听抬起事件,只在需要缩放的按钮上面开启; 因为滚动容器中的item,在滑动时也会触发piontup, 所以滚动容器下的item需要排除; ]] local temp_listener = LuaClickListener.Get(target) if support_up_event == nil then local is_need_scale = target:GetComponent("ExtendButtonScale") or target:GetComponent("ButtonExtend") if is_need_scale then support_up_event = true end end if support_up_event then temp_listener.IsSupporUpEvent = true end if use_sound then local function call_back_2(target,...) GlobalUserOperateTime = Time.time lua_soundM:PlayUIEffectSound(use_sound) call_back(target,...) end temp_listener.onClick = call_back_2 else local function call_back_1( target,... ) GlobalUserOperateTime = Time.time call_back(target,...) end temp_listener.onClick = call_back_1 end end end -- 限制点击频率 function AddClickEventHz(deltatime, target, call_back, use_sound) if target then use_sound = use_sound or LuaSoundManager.SOUND_UI.CLICK if use_sound < 100 then use_sound = LuaSoundManager.SOUND_UI.CLICK end ------------------------- local callback = call_back if use_sound then callback = function (target,...) lua_soundM:PlayUIEffectSound(use_sound) call_back(target,...) end end local value = true local function delay_fun() value = true end local function call_back_2(target,...) if value then value = false callback(target,...) setTimeout(delay_fun,deltatime) end end LuaClickListener.Get(target).onClick = call_back_2 end end function RemoveClickEvent(target) if target then LuaClickListener.Remove(target) end end --[[ 添加点击穿透事件 @param is_over: 是否点击穿透 @param overSameFunc: true穿透后只触发传过来的回调函数 fasle穿透所有用AddOverClickEvent监听的ui ]] function AddOverClickEvent(target,call_back,use_sound,is_over,overSameFunc) if target then local listen = LuaOverClickListener.Get(target) if use_sound then function call_back_2(target,...) GlobalEventSystem:Fire(EventName.PLAY_UI_EFFECT_SOUND,use_sound) call_back(target,...) end listen.onClick = call_back_2 else listen.onClick = call_back end listen.isOver = is_over listen.overSameFunc = overSameFunc end end --添加按下事件 function AddDownEvent(target,call_back,use_sound) if target then if use_sound then local function call_back_2(target,...) EventSystem.Fire(GlobalEventSystem,EventName.PLAY_UI_EFFECT_SOUND,use_sound) call_back(target,...) end LuaEventListener.Get(target).onDown = call_back_2 else LuaEventListener.Get(target).onDown = call_back end end end function RemoveEvent(target) if target then LuaEventListener.Remove(target) end end --添加进入事件 function AddEnterEvent(target,call_back) if target then LuaEventListener.Get(target).onEnter = call_back end end --添加离开事件 function AddExitEvent(target,call_back) if target then LuaEventListener.Get(target).onExit = call_back end end --添加松开事件 function AddUpEvent(target,call_back) if target then LuaEventListener.Get(target).onUp = call_back end end --添加拖拽事件 function AddDragEvent(target,call_back) if target then LuaDragListener.Get(target).onDrag = call_back end end --添加拖拽开始事件 function AddDragBeginEvent(target,call_back) if target then LuaDragListener.Get(target).onDragBegin = call_back end end --添加拖拽结束事件 function AddDragEndEvent(target,call_back) if target then LuaDragListener.Get(target).onDragEnd = call_back end end --添加额外拖拽的触发事件 function LuaDragExtendTriggeListener(target,call_back) if target then LuaDragExtendListener.Get(target).onDragTrigger = call_back end end --添加额外拖拽的组件 function GetLuaDragExtendListener(target) if target then return LuaDragExtendListener.Get(target) end end function RemoveDragEvent(target) if target then LuaDragListener.Remove(target) end end --添加Text首行缩进 function AddSpace(num,str) if str ~= "" then return Util.AddSpace(num,WordManager:changeWords(str)) else return WordManager:changeWords(str) end end --下列接口会尝试多种设置的,如果你已经知道你的节点是什么类型的就别用下列接口了 -- function GetLocalPositionX( transform ) -- if not transform then return end -- return GetLocalPositionX(transform) -- end -- function GetLocalPositionY( transform ) -- if not transform then return end -- return GetLocalPositionY(transform) -- end -- function GetLocalPositionZ( transform ) -- if not transform then return end -- return GetLocalPositionZ(transform) -- end -- function SetLocalPosition( transform, x, y, z ) -- if not transform then return end -- SetLocalPositionX(transform, x) -- SetLocalPositionY(transform, y) -- SetLocalPositionZ(transform, z) -- end function SetVisible( obj, is_show ) if not obj then return end if obj.SetActive then obj:SetActive(is_show) elseif obj.SetVisible then obj:SetVisible(is_show) end end --上列接口会尝试多种设置的,如果你已经知道你的节点是什么类型的就别用上列接口了 -- 利用角度转换实现显隐 function SetVisibleByRotate( transform, is_show ) if not transform then return end if is_show then transform.localRotation = Quaternion.Euler(0,0,0) else transform.localRotation = Quaternion.Euler(90,90,0) end end UpdateVisibleJuryTable = {} UpdateVisibleJuryTableForValue = {} function InitForUIHelper( ) setmetatable(UpdateVisibleJuryTable, {__mode = "k"}) setmetatable(UpdateVisibleJuryTableForValue, {__mode = "v"}) end --你显示隐藏前都要给我一个理由,我会综合考虑,只会在没有任何理由隐藏时我才会真正地显示出来 function UpdateVisibleByJury( obj, is_show, reason ) if not obj then return end local tab_str = tostring(obj) UpdateVisibleJuryTableForValue[tab_str] = obj if not UpdateVisibleJuryTable[obj] then local jury = Jury.New() --当陪审团的投票结果变更时触发 local on_jury_change = function ( ) --之所以用UpdateVisibleJuryTableForValue弱表是因为直接引用obj的话将影响到obj的gc,因为Jury等着obj释放时跟着自动释放,但Jury引用了本函数,而本函数又引用obj的话就循环引用了(想依赖弱引用做自动回收是会有这个问题的) if UpdateVisibleJuryTableForValue[tab_str] then --没人投票的话就说明可以显示啦 -- print('Cat:UIHelper.lua[obj] jury:IsNoneVote()', jury:IsNoneVote()) SetVisible(UpdateVisibleJuryTableForValue[tab_str], jury:IsNoneVote()) end end jury:CallBackOnResultChange(on_jury_change) UpdateVisibleJuryTable[obj] = jury end if is_show then UpdateVisibleJuryTable[obj]:UnVote(reason) else --想隐藏就投票 UpdateVisibleJuryTable[obj]:Vote(reason) end end --滚动到目标点,让其尽量显示在滚动容器的中间 function ScrollToCenter( Content, item, item_width, size )--最后一个是容器 size 的大小 -- print("huangcong: [318] Content, item, item_width: ",Content, item, item_width,size) --画布 item item_width scrollSize item_width = item_width or 80--默认把节点的高度当作80 local scroll_size = size local scroll_real_size = Content.sizeDelta local item_real_pos = nil item_real_pos = item:GetPosition() local new_fit_x = 0 local condition1 = item_real_pos.x - scroll_size.x/2 --第一部分是判断左边极限 local condition2 = item_real_pos.x + scroll_size.x/2 - scroll_real_size.x--右边极限 if condition1 > 0 and condition2 < 0 then new_fit_x = -(item_real_pos.x - scroll_size.x/2) - item_width/2 elseif condition1 <= 0 then new_fit_x = 0 elseif condition2 >= 0 then new_fit_x = -(scroll_real_size.x - scroll_size.x)+1 end print("huangcong:UIHelper [423]: ",new_fit_x,item_real_pos.x,scroll_size.x) SetAnchoredPosition(Content.transform,new_fit_x,0) end --ScrollView滚到最底时触发,注意AddDragEndEvent事件只是拉滚动窗口动画结束时触发 function BindScrollViewEndEvent(scroll_view_obj, scroll_rect , drag_end_call_back) local on_scroll_end = function ( ) if scroll_rect.verticalNormalizedPosition <= 0 then if drag_end_call_back then drag_end_call_back() end end end AddDragEndEvent(scroll_view_obj, on_scroll_end) end function SetImageGray( image, isGray ) if not image then return end if isGray then -- image.color = Color(148/255,148/255,148/255) image.material = Resources.Load("material/Gray") else -- image.color = ColorUtilValue.WHITE image.material = nil end end function SetTextGray( text, isGray, color ) if not text then return end if isGray then text.color = Color(148/255,148/255,148/255) else text.color = color or ColorUtilValue.WHITE end end function SetButtonGray( image, outline, isGray ) if not image then return end if isGray then -- image.color = Color(148/255,148/255,148/255) image.material = Resources.Load("material/Gray") if outline then outline.enabled = false end else -- image.color = ColorUtilValue.WHITE image.material = nil if outline then outline.enabled = true end end end function SetChildrenGray( parent, is_gray, ignore_dic ) local images = parent:GetComponentsInChildren(typeof(UnityEngine.UI.Image)) for i=0,images.Length-1 do if not ignore_dic or not ignore_dic[images[i]] then SetImageGray(images[i], is_gray) end end end local find = string.find local gsub = string.gsub local Split = Split G_ComponentMapForGetChildren = { img = typeof(UnityEngine.UI.Image), txt = typeof(UnityEngine.UI.Text), tog = typeof(UnityEngine.UI.Toggle), imgex = typeof(LuaFramework.ImageExtend), outline = typeof(UnityEngine.UI.Outline), raw = typeof(UnityEngine.UI.RawImage), rawex = typeof(LuaFramework.RawImageExtend), scroll = typeof(UnityEngine.UI.ScrollRect), input = typeof(UnityEngine.UI.InputField), shadow = typeof(UnityEngine.UI.Shadow), adtxt = typeof(LuaFramework.AdaptiveText), tmp = typeof(TMPro.TextMeshProUGUI), cg = typeof(UnityEngine.CanvasGroup), tmpInput = typeof(TMPro.TMP_InputField), } function GetChildren( self, names ) assert(self.transform, "UIHelper:GetChildren() cannot find transform!") local find_child = self.transform.Find for i=1,#names do local name_parts = Split(names[i], ":") local full_name = name_parts[1] local short_name = full_name if short_name and find(short_name,"/") then short_name = gsub(short_name,".+/","") end assert(self[short_name] == nil, short_name .. " already exists") if short_name then self[short_name] = find_child(self.transform, full_name) end assert(self[short_name], "cannot find child : "..short_name) for j=2,#name_parts do if name_parts[j] == "obj" then self[short_name.."_"..name_parts[j]] = self[short_name].gameObject elseif G_ComponentMapForGetChildren[name_parts[j]] then local component = self[short_name]:GetComponent(G_ComponentMapForGetChildren[name_parts[j]]) -- if name_parts[j] == "txt" then -- --兼容tmp -- component = component or self[short_name]:GetComponent(G_ComponentMapForGetChildren["tmp"]) -- end if component==nil then print(component, "cannot find component "..name_parts[j].." in child "..short_name) end if name_parts[j] == "adtxt" then -- 自动文本兼容txt self[short_name.."_txt"] = component else self[short_name.."_"..name_parts[j]] = component end else assert(false, "cannot find this component short name : "..name_parts[j]) end end end end function RemoveChildren(parent) if not parent then return end for i=0,parent.childCount-1 do GameObject.Destroy(parent:GetChild(i).gameObject) end parent:DetachChildren() end function SetParent( transform, parent ) if transform then transform:SetParent(parent) SetLocalPosition(transform, 1, 1, 1) SetLocalScale(transform, 1, 1, 1) end end --返回对齐方式字符串 function AlignTypeToStr( alignment ) local vert_align = "Upper" if alignment == UnityEngine.TextAnchor.UpperLeft or alignment == UnityEngine.TextAnchor.UpperCenter or alignment == UnityEngine.TextAnchor.UpperRight then vert_align = "Upper" elseif alignment == UnityEngine.TextAnchor.MiddleLeft or alignment == UnityEngine.TextAnchor.MiddleCenter or alignment == UnityEngine.TextAnchor.MiddleRight then vert_align = "Middle" elseif alignment == UnityEngine.TextAnchor.LowerLeft or alignment == UnityEngine.TextAnchor.LowerCenter or alignment == UnityEngine.TextAnchor.LowerRight then vert_align = "Lower" end local hori_align = "Left" if alignment == UnityEngine.TextAnchor.UpperLeft or alignment == UnityEngine.TextAnchor.MiddleLeft or alignment == UnityEngine.TextAnchor.LowerLeft then hori_align = "Left" elseif alignment == UnityEngine.TextAnchor.UpperCenter or alignment == UnityEngine.TextAnchor.MiddleCenter or alignment == UnityEngine.TextAnchor.LowerCenter then hori_align = "Center" elseif alignment == UnityEngine.TextAnchor.UpperRight or alignment == UnityEngine.TextAnchor.MiddleRight or alignment == UnityEngine.TextAnchor.LowerRight then hori_align = "Right" end return hori_align, vert_align end --获取背景图设置为全屏之后的宽度的宽度(以y值为基础比例,去铺满屏幕) function GetWideScreenSize( ) local size_x, size_y = 1560, 720 --背景图默认尺寸 local scale = size_y/ScreenHeight if size_y < ScreenHeight then scale = ScreenHeight/size_y end local width = scale*size_x return width, ScreenHeight end function ChangeParentAndKeepTransform( transform, parent ) local abs_pos_x, abs_pos_y, abs_pos_z = GetGlobalPosition(transform) transform:SetParent(parent) SetGlobalPosition(transform, abs_pos_x, abs_pos_y, abs_pos_z) end --加载图片完成后显示 function SetImgAndSetShow( ref_tar,node,abName,resName,setNativeSize, call_back ) local raw_img = node:GetComponent("RawImage") local img = node:GetComponent("Image") local gameObject = node.gameObject if (img or raw_img) and gameObject then local function visible_call_back( ) gameObject:SetActive(true) end if abName then lua_resM:setImageSprite(ref_tar, raw_img or img, abName, resName,setNativeSize,call_back or visible_call_back) else if not string.find( resName, ".png" ) then resName = resName .. ".png" end if raw_img then lua_resM:setOutsideRawImage(ref_tar, raw_img, resName, setNativeSize, call_back or visible_call_back) else lua_resM:setOutsideImageSprite(ref_tar, img, resName, setNativeSize, call_back or visible_call_back) end end end end --scroll_view的轴点需要是(0,1) 水平排列奖励列表等东西 function AnchorItemListH( item_list, scroll_view, item_width, item_space_x, off_x, is_no_resize_view ) item_space_x = item_space_x or 0 off_x = off_x or 0 local count = item_list.__count__ and item_list.__count__ or (#item_list) local scroll_width,_ = GetSizeDeltaXY(scroll_view) --计算开始显示的位置 local size = count*(item_width+item_space_x)-item_space_x local start_x = ( scroll_width - size )/2 if start_x < 0 then start_x = 0 end for i,item in ipairs(item_list) do local x = (i-1) * (item_width+item_space_x) + start_x + off_x item:SetPosition(x, 0) end if not is_no_resize_view then SetSizeDelta(scroll_view,start_x+count*(item_width+item_space_x)-item_space_x, 100) end end --横向方向对齐,直接传节点(X轴锚点需要是0)),不是传组件 function ItemSetHorizontal( off_x, ... ) local node_list = {...} local off_x = off_x or 0--节点间的间隔 local pos_x = 0 local function get_node_is_label( node ) return node:GetComponent("TMPro.TextMeshProUGUI") or node:GetComponent("Text") end for i,v in ipairs(node_list) do lb_component = get_node_is_label(v) if i == 1 then pos_x = GetAnchoredPosition( v.transform ) else SetAnchoredPositionX( v.transform, pos_x ) end pos_x = lb_component and (lb_component.preferredWidth + pos_x) or (pos_x + GetSizeDeltaX(v.transform)) pos_x = off_x + pos_x end end --垂直方向对齐,直接传节点,不是传组件 --(Y轴锚点需要是1,而且文本垂直方向要Upper) function ItemSetVerticle( off_y, ... ) local node_list = {...} local off_y = off_y or 0--节点间的间隔 local pos_y = 0 local _ = false--无用参数 local function get_node_is_label( node ) return node:GetComponent("Text") end for i,v in ipairs(node_list) do lb_component = get_node_is_label(v) if i == 1 then _,pos_y = GetAnchoredPosition( v.transform ) else SetAnchoredPositionY( v.transform, pos_y ) end pos_y = lb_component and (lb_component.preferredHeight - pos_y) or (GetSizeDeltaY(v.transform) - pos_y) pos_y = off_y - pos_y end end --获取图片大小和维度, 获取 N维 图片UV数据UI function GetImageSplitUVRectInfo( size_x, size_y, row, col) local list = {} local cell_x = size_x / row local cell_y = size_y / col local uv_cell_x = 1 / row local uv_cell_y = 1 / col local uv_cell_w = uv_cell_x local uv_cell_h = uv_cell_y for i=1,row do for ii=1,col do local d = {} d.cell_x = cell_x d.cell_y = cell_y d.uv_cell_x = (i - 1)* uv_cell_x d.uv_cell_y = (ii - 1)* uv_cell_y d.uv_cell_w = uv_cell_w d.uv_cell_h = uv_cell_h table.insert(list, d) end end return list, cell_x, cell_y end