源战役客户端
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

848 行
23 KiB

  1. local GameObject = GameObject
  2. local Util = Util
  3. local LuaEventListener = LuaEventListener
  4. local LuaClickListener = LuaClickListener
  5. local LuaDragListener = LuaDragListener
  6. local LuaDragExtendListener = LuaDragExtendListener
  7. local WordManager = WordManager
  8. --设置position位置
  9. function SetGlobalPosition(transform, x, y, z)
  10. if transform then
  11. Util.SetPosition(transform, x, y, z)
  12. end
  13. end
  14. function SetGlobalPosition3D(transform, x, y, z)
  15. if transform then
  16. Util.SetPosition(transform, x, z, y)
  17. end
  18. end
  19. function SetGlobalPositionX(transform, x)
  20. if transform then
  21. x = x or 0
  22. Util.SetPositionX(transform, x)
  23. end
  24. end
  25. function SetGlobalPositionY(transform, y)
  26. if transform then
  27. y = y or 0
  28. Util.SetPositionY(transform, y)
  29. end
  30. end
  31. function SetGlobalPositionZ(transform, z)
  32. if not IsNull(transform) then
  33. z = z or 0
  34. Util.SetPositionZ(transform, z)
  35. end
  36. end
  37. function GetGlobalPosition( transform )
  38. if not transform then return end
  39. return Util.GetPosition(transform)
  40. end
  41. function GetGlobalPositionXY( transform )
  42. if not transform then return end
  43. return transform:GetLocalPosXY()
  44. end
  45. function GetGlobalPositionX(transform)
  46. return Util.GetPositionX(transform)
  47. end
  48. function GetGlobalPositionY(transform)
  49. return Util.GetPositionY(transform)
  50. end
  51. function GetGlobalPositionZ(transform)
  52. return Util.GetPositionZ(transform)
  53. end
  54. --设置localPosition位置
  55. function SetLocalPosition(transform, x, y, z)
  56. if transform then
  57. x = x or 0
  58. y = y or x
  59. z = z or x
  60. Util.SetLocalPosition(transform, x, y, z)
  61. end
  62. end
  63. function SetLocalPositionX(transform, x)
  64. if transform then
  65. x = x or 0
  66. Util.SetLocalPositionX(transform, x)
  67. end
  68. end
  69. function SetLocalPositionY(transform, y)
  70. if transform then
  71. y = y or 0
  72. Util.SetLocalPositionY(transform, y)
  73. end
  74. end
  75. function SetLocalPositionZ(transform, z)
  76. if transform then
  77. z = z or 0
  78. Util.SetLocalPositionZ(transform, z)
  79. end
  80. end
  81. function GetAnchoredPosition( transform )
  82. if not transform then return end
  83. return Util.GetAnchoredPosition(transform)
  84. end
  85. function SetAnchoredPosition( transform, x, y )
  86. if not transform then return end
  87. return Util.SetAnchoredPosition(transform, x, y)
  88. end
  89. function SetAnchoredPositionX( transform, x )
  90. if not transform then return end
  91. local old_x, old_y = GetAnchoredPosition(transform)
  92. return Util.SetAnchoredPosition(transform, x, old_y)
  93. end
  94. function SetAnchoredPositionY( transform, y )
  95. if not transform then return end
  96. local old_x, old_y = GetAnchoredPosition(transform)
  97. return Util.SetAnchoredPosition(transform, old_x, y)
  98. end
  99. function GetLocalPosition( transform )
  100. if not transform then return end
  101. return Util.GetLocalPosition(transform)
  102. end
  103. function GetLocalPositionX(transform)
  104. return Util.GetLocalPositionX(transform)
  105. end
  106. function GetLocalPositionY(transform)
  107. return Util.GetLocalPositionY(transform)
  108. end
  109. function GetLocalPositionZ(transform)
  110. return Util.GetLocalPositionZ(transform)
  111. end
  112. --设置localscale
  113. function SetLocalScale(transform, x, y, z)
  114. if transform then
  115. x = x or 1
  116. y = y or x
  117. z = z or x
  118. Util.SetLocalScale(transform, x, y, z)
  119. end
  120. end
  121. function GetLocalScale(transform)
  122. if transform then
  123. return Util.GetLocalScale(transform)
  124. end
  125. end
  126. --获取包围盒的y值
  127. function GetRenderBoundsSize(render)
  128. if render then
  129. return Util.GetRenderBoundsSize(render)
  130. end
  131. end
  132. --设置localRotation
  133. function SetLocalRotation(transform, x, y, z, w)
  134. if transform then
  135. x = x or 0
  136. y = y or x
  137. z = z or x
  138. w = w or 1
  139. Util.SetLocalRotation(transform, x, y, z, w)
  140. end
  141. end
  142. function GetLocalRotation(transform)
  143. if transform then
  144. return Util.GetLocalRotation(transform)
  145. end
  146. end
  147. --实际是调用Rotate()方法,在原有角度基础上,继续旋转
  148. function SetRotate(transform, x, y, z)
  149. if transform then
  150. Util.SetRotate(transform, x, y, z)
  151. end
  152. end
  153. function SetSizeDelta(transform, x, y)
  154. if transform then
  155. Util.SetSizeDelta(transform, x, y)
  156. end
  157. end
  158. function SetSizeDeltaX(transform, x)
  159. if transform then
  160. Util.SetSizeDeltaX(transform, x)
  161. end
  162. end
  163. function SetSizeDeltaY(transform, y)
  164. if transform then
  165. Util.SetSizeDeltaY(transform, y)
  166. end
  167. end
  168. function GetSizeDeltaXY(transform)
  169. if transform then
  170. return transform:GetDeltaSizeXY()
  171. end
  172. end
  173. function GetSizeDeltaX(transform)
  174. if transform then
  175. return Util.GetSizeDeltaX(transform)
  176. end
  177. end
  178. function GetSizeDeltaY(transform)
  179. if transform then
  180. return Util.GetSizeDeltaY(transform)
  181. end
  182. end
  183. function SetTMPSharedMaterial(tmp_text, mat_type)
  184. if tmp_text then
  185. local temp_mat = ShaderTools.GetTMPSharedMaterial( mat_type )
  186. if temp_mat then
  187. tmp_text.fontSharedMaterial = temp_mat
  188. end
  189. end
  190. end
  191. function SetBgBySprite( self, img, bg_res, is_auto_size, loaded_call_back )
  192. if not img then return end
  193. img.gameObject:SetActive(false)
  194. local on_loaded = function ( )
  195. img.gameObject:SetActive(true)
  196. if loaded_call_back then
  197. loaded_call_back()
  198. end
  199. end
  200. lua_resM:setOutsideImageSprite(self, img, GameResPath.GetViewBigBg(bg_res), is_auto_size, on_loaded)
  201. end
  202. function SetBg( self, img, bg_res, is_auto_size, loaded_call_back )
  203. if not img then return end
  204. img.gameObject:SetActive(false)
  205. local on_loaded = function ( )
  206. img.gameObject:SetActive(true)
  207. if loaded_call_back then
  208. loaded_call_back()
  209. end
  210. end
  211. lua_resM:setOutsideRawImage(self, img, GameResPath.GetViewBigBg(bg_res), is_auto_size, on_loaded)
  212. end
  213. --添加点击事件
  214. function AddClickEvent(target, call_back, use_sound, support_up_event)
  215. if not IsNull(target) then
  216. use_sound = use_sound or LuaSoundManager.SOUND_UI.CLICK
  217. if use_sound < 100 then
  218. use_sound = LuaSoundManager.SOUND_UI.CLICK
  219. end
  220. --[[
  221. support_up_event false;
  222. ;
  223. itempiontup, item需要排除
  224. ]]
  225. local temp_listener = LuaClickListener.Get(target)
  226. if support_up_event == nil then
  227. local is_need_scale = target:GetComponent("ExtendButtonScale") or target:GetComponent("ButtonExtend")
  228. if is_need_scale then
  229. support_up_event = true
  230. end
  231. end
  232. if support_up_event then
  233. temp_listener.IsSupporUpEvent = true
  234. end
  235. if use_sound then
  236. local function call_back_2(target,...)
  237. GlobalUserOperateTime = Time.time
  238. lua_soundM:PlayUIEffectSound(use_sound)
  239. call_back(target,...)
  240. end
  241. temp_listener.onClick = call_back_2
  242. else
  243. local function call_back_1( target,... )
  244. GlobalUserOperateTime = Time.time
  245. call_back(target,...)
  246. end
  247. temp_listener.onClick = call_back_1
  248. end
  249. end
  250. end
  251. -- 限制点击频率
  252. function AddClickEventHz(deltatime, target, call_back, use_sound)
  253. if target then
  254. use_sound = use_sound or LuaSoundManager.SOUND_UI.CLICK
  255. if use_sound < 100 then
  256. use_sound = LuaSoundManager.SOUND_UI.CLICK
  257. end
  258. -------------------------
  259. local callback = call_back
  260. if use_sound then
  261. callback = function (target,...)
  262. lua_soundM:PlayUIEffectSound(use_sound)
  263. call_back(target,...)
  264. end
  265. end
  266. local value = true
  267. local function delay_fun()
  268. value = true
  269. end
  270. local function call_back_2(target,...)
  271. if value then
  272. value = false
  273. callback(target,...)
  274. setTimeout(delay_fun,deltatime)
  275. end
  276. end
  277. LuaClickListener.Get(target).onClick = call_back_2
  278. end
  279. end
  280. function RemoveClickEvent(target)
  281. if target then
  282. LuaClickListener.Remove(target)
  283. end
  284. end
  285. --[[
  286. 穿
  287. @param is_over: 穿
  288. @param overSameFunc: true穿透后只触发传过来的回调函数 fasle穿透所有用AddOverClickEvent监听的ui
  289. ]]
  290. function AddOverClickEvent(target,call_back,use_sound,is_over,overSameFunc)
  291. if target then
  292. local listen = LuaOverClickListener.Get(target)
  293. if use_sound then
  294. function call_back_2(target,...)
  295. GlobalEventSystem:Fire(EventName.PLAY_UI_EFFECT_SOUND,use_sound)
  296. call_back(target,...)
  297. end
  298. listen.onClick = call_back_2
  299. else
  300. listen.onClick = call_back
  301. end
  302. listen.isOver = is_over
  303. listen.overSameFunc = overSameFunc
  304. end
  305. end
  306. --添加按下事件
  307. function AddDownEvent(target,call_back,use_sound)
  308. if target then
  309. if use_sound then
  310. local function call_back_2(target,...)
  311. EventSystem.Fire(GlobalEventSystem,EventName.PLAY_UI_EFFECT_SOUND,use_sound)
  312. call_back(target,...)
  313. end
  314. LuaEventListener.Get(target).onDown = call_back_2
  315. else
  316. LuaEventListener.Get(target).onDown = call_back
  317. end
  318. end
  319. end
  320. function RemoveEvent(target)
  321. if target then
  322. LuaEventListener.Remove(target)
  323. end
  324. end
  325. --添加进入事件
  326. function AddEnterEvent(target,call_back)
  327. if target then
  328. LuaEventListener.Get(target).onEnter = call_back
  329. end
  330. end
  331. --添加离开事件
  332. function AddExitEvent(target,call_back)
  333. if target then
  334. LuaEventListener.Get(target).onExit = call_back
  335. end
  336. end
  337. --添加松开事件
  338. function AddUpEvent(target,call_back)
  339. if target then
  340. LuaEventListener.Get(target).onUp = call_back
  341. end
  342. end
  343. --添加拖拽事件
  344. function AddDragEvent(target,call_back)
  345. if target then
  346. LuaDragListener.Get(target).onDrag = call_back
  347. end
  348. end
  349. --添加拖拽开始事件
  350. function AddDragBeginEvent(target,call_back)
  351. if target then
  352. LuaDragListener.Get(target).onDragBegin = call_back
  353. end
  354. end
  355. --添加拖拽结束事件
  356. function AddDragEndEvent(target,call_back)
  357. if target then
  358. LuaDragListener.Get(target).onDragEnd = call_back
  359. end
  360. end
  361. --添加额外拖拽的触发事件
  362. function LuaDragExtendTriggeListener(target,call_back)
  363. if target then
  364. LuaDragExtendListener.Get(target).onDragTrigger = call_back
  365. end
  366. end
  367. --添加额外拖拽的组件
  368. function GetLuaDragExtendListener(target)
  369. if target then
  370. return LuaDragExtendListener.Get(target)
  371. end
  372. end
  373. function RemoveDragEvent(target)
  374. if target then
  375. LuaDragListener.Remove(target)
  376. end
  377. end
  378. --添加Text首行缩进
  379. function AddSpace(num,str)
  380. if str ~= "" then
  381. return Util.AddSpace(num,WordManager:changeWords(str))
  382. else
  383. return WordManager:changeWords(str)
  384. end
  385. end
  386. --下列接口会尝试多种设置的,如果你已经知道你的节点是什么类型的就别用下列接口了
  387. -- function GetLocalPositionX( transform )
  388. -- if not transform then return end
  389. -- return GetLocalPositionX(transform)
  390. -- end
  391. -- function GetLocalPositionY( transform )
  392. -- if not transform then return end
  393. -- return GetLocalPositionY(transform)
  394. -- end
  395. -- function GetLocalPositionZ( transform )
  396. -- if not transform then return end
  397. -- return GetLocalPositionZ(transform)
  398. -- end
  399. -- function SetLocalPosition( transform, x, y, z )
  400. -- if not transform then return end
  401. -- SetLocalPositionX(transform, x)
  402. -- SetLocalPositionY(transform, y)
  403. -- SetLocalPositionZ(transform, z)
  404. -- end
  405. function SetVisible( obj, is_show )
  406. if not obj then return end
  407. if obj.SetActive then
  408. obj:SetActive(is_show)
  409. elseif obj.SetVisible then
  410. obj:SetVisible(is_show)
  411. end
  412. end
  413. --上列接口会尝试多种设置的,如果你已经知道你的节点是什么类型的就别用上列接口了
  414. -- 利用角度转换实现显隐
  415. function SetVisibleByRotate( transform, is_show )
  416. if not transform then return end
  417. if is_show then
  418. transform.localRotation = Quaternion.Euler(0,0,0)
  419. else
  420. transform.localRotation = Quaternion.Euler(90,90,0)
  421. end
  422. end
  423. UpdateVisibleJuryTable = {}
  424. UpdateVisibleJuryTableForValue = {}
  425. function InitForUIHelper( )
  426. setmetatable(UpdateVisibleJuryTable, {__mode = "k"})
  427. setmetatable(UpdateVisibleJuryTableForValue, {__mode = "v"})
  428. end
  429. --你显示隐藏前都要给我一个理由,我会综合考虑,只会在没有任何理由隐藏时我才会真正地显示出来
  430. function UpdateVisibleByJury( obj, is_show, reason )
  431. if not obj then return end
  432. local tab_str = tostring(obj)
  433. UpdateVisibleJuryTableForValue[tab_str] = obj
  434. if not UpdateVisibleJuryTable[obj] then
  435. local jury = Jury.New()
  436. --当陪审团的投票结果变更时触发
  437. local on_jury_change = function ( )
  438. --之所以用UpdateVisibleJuryTableForValue弱表是因为直接引用obj的话将影响到obj的gc,因为Jury等着obj释放时跟着自动释放,但Jury引用了本函数,而本函数又引用obj的话就循环引用了(想依赖弱引用做自动回收是会有这个问题的)
  439. if UpdateVisibleJuryTableForValue[tab_str] then
  440. --没人投票的话就说明可以显示啦
  441. -- print('Cat:UIHelper.lua[obj] jury:IsNoneVote()', jury:IsNoneVote())
  442. SetVisible(UpdateVisibleJuryTableForValue[tab_str], jury:IsNoneVote())
  443. end
  444. end
  445. jury:CallBackOnResultChange(on_jury_change)
  446. UpdateVisibleJuryTable[obj] = jury
  447. end
  448. if is_show then
  449. UpdateVisibleJuryTable[obj]:UnVote(reason)
  450. else
  451. --想隐藏就投票
  452. UpdateVisibleJuryTable[obj]:Vote(reason)
  453. end
  454. end
  455. --滚动到目标点,让其尽量显示在滚动容器的中间
  456. function ScrollToCenter( Content, item, item_width, size )--最后一个是容器 size 的大小
  457. -- print("huangcong: [318] Content, item, item_width: ",Content, item, item_width,size)
  458. --画布 item item_width scrollSize
  459. item_width = item_width or 80--默认把节点的高度当作80
  460. local scroll_size = size
  461. local scroll_real_size = Content.sizeDelta
  462. local item_real_pos = nil
  463. item_real_pos = item:GetPosition()
  464. local new_fit_x = 0
  465. local condition1 = item_real_pos.x - scroll_size.x/2 --第一部分是判断左边极限
  466. local condition2 = item_real_pos.x + scroll_size.x/2 - scroll_real_size.x--右边极限
  467. if condition1 > 0 and condition2 < 0 then
  468. new_fit_x = -(item_real_pos.x - scroll_size.x/2) - item_width/2
  469. elseif condition1 <= 0 then
  470. new_fit_x = 0
  471. elseif condition2 >= 0 then
  472. new_fit_x = -(scroll_real_size.x - scroll_size.x)+1
  473. end
  474. print("huangcong:UIHelper [423]: ",new_fit_x,item_real_pos.x,scroll_size.x)
  475. SetAnchoredPosition(Content.transform,new_fit_x,0)
  476. end
  477. --ScrollView滚到最底时触发,注意AddDragEndEvent事件只是拉滚动窗口动画结束时触发
  478. function BindScrollViewEndEvent(scroll_view_obj, scroll_rect , drag_end_call_back)
  479. local on_scroll_end = function ( )
  480. if scroll_rect.verticalNormalizedPosition <= 0 then
  481. if drag_end_call_back then
  482. drag_end_call_back()
  483. end
  484. end
  485. end
  486. AddDragEndEvent(scroll_view_obj, on_scroll_end)
  487. end
  488. function SetImageGray( image, isGray )
  489. if not image then return end
  490. if isGray then
  491. -- image.color = Color(148/255,148/255,148/255)
  492. image.material = Resources.Load("material/Gray")
  493. else
  494. -- image.color = ColorUtilValue.WHITE
  495. image.material = nil
  496. end
  497. end
  498. function SetTextGray( text, isGray, color )
  499. if not text then return end
  500. if isGray then
  501. text.color = Color(148/255,148/255,148/255)
  502. else
  503. text.color = color or ColorUtilValue.WHITE
  504. end
  505. end
  506. function SetButtonGray( image, outline, isGray )
  507. if not image then return end
  508. if isGray then
  509. -- image.color = Color(148/255,148/255,148/255)
  510. image.material = Resources.Load("material/Gray")
  511. if outline then
  512. outline.enabled = false
  513. end
  514. else
  515. -- image.color = ColorUtilValue.WHITE
  516. image.material = nil
  517. if outline then
  518. outline.enabled = true
  519. end
  520. end
  521. end
  522. function SetChildrenGray( parent, is_gray, ignore_dic )
  523. local images = parent:GetComponentsInChildren(typeof(UnityEngine.UI.Image))
  524. for i=0,images.Length-1 do
  525. if not ignore_dic or not ignore_dic[images[i]] then
  526. SetImageGray(images[i], is_gray)
  527. end
  528. end
  529. end
  530. local find = string.find
  531. local gsub = string.gsub
  532. local Split = Split
  533. G_ComponentMapForGetChildren = {
  534. img = typeof(UnityEngine.UI.Image),
  535. txt = typeof(UnityEngine.UI.Text),
  536. tog = typeof(UnityEngine.UI.Toggle),
  537. imgex = typeof(LuaFramework.ImageExtend),
  538. outline = typeof(UnityEngine.UI.Outline),
  539. raw = typeof(UnityEngine.UI.RawImage),
  540. rawex = typeof(LuaFramework.RawImageExtend),
  541. scroll = typeof(UnityEngine.UI.ScrollRect),
  542. input = typeof(UnityEngine.UI.InputField),
  543. shadow = typeof(UnityEngine.UI.Shadow),
  544. adtxt = typeof(LuaFramework.AdaptiveText),
  545. tmp = typeof(TMPro.TextMeshProUGUI),
  546. cg = typeof(UnityEngine.CanvasGroup),
  547. tmpInput = typeof(TMPro.TMP_InputField),
  548. }
  549. function GetChildren( self, names )
  550. assert(self.transform, "UIHelper:GetChildren() cannot find transform!")
  551. local find_child = self.transform.Find
  552. for i=1,#names do
  553. local name_parts = Split(names[i], ":")
  554. local full_name = name_parts[1]
  555. local short_name = full_name
  556. if short_name and find(short_name,"/") then
  557. short_name = gsub(short_name,".+/","")
  558. end
  559. assert(self[short_name] == nil, short_name .. " already exists")
  560. if short_name then
  561. self[short_name] = find_child(self.transform, full_name)
  562. end
  563. assert(self[short_name], "cannot find child : "..short_name)
  564. for j=2,#name_parts do
  565. if name_parts[j] == "obj" then
  566. self[short_name.."_"..name_parts[j]] = self[short_name].gameObject
  567. elseif G_ComponentMapForGetChildren[name_parts[j]] then
  568. local component = self[short_name]:GetComponent(G_ComponentMapForGetChildren[name_parts[j]])
  569. -- if name_parts[j] == "txt" then
  570. -- --兼容tmp
  571. -- component = component or self[short_name]:GetComponent(G_ComponentMapForGetChildren["tmp"])
  572. -- end
  573. if component==nil then
  574. print(component, "cannot find component "..name_parts[j].." in child "..short_name)
  575. end
  576. if name_parts[j] == "adtxt" then -- 自动文本兼容txt
  577. self[short_name.."_txt"] = component
  578. else
  579. self[short_name.."_"..name_parts[j]] = component
  580. end
  581. else
  582. assert(false, "cannot find this component short name : "..name_parts[j])
  583. end
  584. end
  585. end
  586. end
  587. function RemoveChildren(parent)
  588. if not parent then return end
  589. for i=0,parent.childCount-1 do
  590. GameObject.Destroy(parent:GetChild(i).gameObject)
  591. end
  592. parent:DetachChildren()
  593. end
  594. function SetParent( transform, parent )
  595. if transform then
  596. transform:SetParent(parent)
  597. SetLocalPosition(transform, 1, 1, 1)
  598. SetLocalScale(transform, 1, 1, 1)
  599. end
  600. end
  601. --返回对齐方式字符串
  602. function AlignTypeToStr( alignment )
  603. local vert_align = "Upper"
  604. if alignment == UnityEngine.TextAnchor.UpperLeft
  605. or alignment == UnityEngine.TextAnchor.UpperCenter
  606. or alignment == UnityEngine.TextAnchor.UpperRight then
  607. vert_align = "Upper"
  608. elseif alignment == UnityEngine.TextAnchor.MiddleLeft
  609. or alignment == UnityEngine.TextAnchor.MiddleCenter
  610. or alignment == UnityEngine.TextAnchor.MiddleRight then
  611. vert_align = "Middle"
  612. elseif alignment == UnityEngine.TextAnchor.LowerLeft
  613. or alignment == UnityEngine.TextAnchor.LowerCenter
  614. or alignment == UnityEngine.TextAnchor.LowerRight then
  615. vert_align = "Lower"
  616. end
  617. local hori_align = "Left"
  618. if alignment == UnityEngine.TextAnchor.UpperLeft
  619. or alignment == UnityEngine.TextAnchor.MiddleLeft
  620. or alignment == UnityEngine.TextAnchor.LowerLeft then
  621. hori_align = "Left"
  622. elseif alignment == UnityEngine.TextAnchor.UpperCenter
  623. or alignment == UnityEngine.TextAnchor.MiddleCenter
  624. or alignment == UnityEngine.TextAnchor.LowerCenter then
  625. hori_align = "Center"
  626. elseif alignment == UnityEngine.TextAnchor.UpperRight
  627. or alignment == UnityEngine.TextAnchor.MiddleRight
  628. or alignment == UnityEngine.TextAnchor.LowerRight then
  629. hori_align = "Right"
  630. end
  631. return hori_align, vert_align
  632. end
  633. --获取背景图设置为全屏之后的宽度的宽度(以y值为基础比例,去铺满屏幕)
  634. function GetWideScreenSize( )
  635. local size_x, size_y = 1560, 720 --背景图默认尺寸
  636. local scale = size_y/ScreenHeight
  637. if size_y < ScreenHeight then
  638. scale = ScreenHeight/size_y
  639. end
  640. local width = scale*size_x
  641. return width, ScreenHeight
  642. end
  643. function ChangeParentAndKeepTransform( transform, parent )
  644. local abs_pos_x, abs_pos_y, abs_pos_z = GetGlobalPosition(transform)
  645. transform:SetParent(parent)
  646. SetGlobalPosition(transform, abs_pos_x, abs_pos_y, abs_pos_z)
  647. end
  648. --加载图片完成后显示
  649. function SetImgAndSetShow( ref_tar,node,abName,resName,setNativeSize, call_back )
  650. local raw_img = node:GetComponent("RawImage")
  651. local img = node:GetComponent("Image")
  652. local gameObject = node.gameObject
  653. if (img or raw_img) and gameObject then
  654. local function visible_call_back( )
  655. gameObject:SetActive(true)
  656. end
  657. if abName then
  658. lua_resM:setImageSprite(ref_tar, raw_img or img, abName, resName,setNativeSize,call_back or visible_call_back)
  659. else
  660. if not string.find( resName, ".png" ) then
  661. resName = resName .. ".png"
  662. end
  663. if raw_img then
  664. lua_resM:setOutsideRawImage(ref_tar, raw_img, resName, setNativeSize, call_back or visible_call_back)
  665. else
  666. lua_resM:setOutsideImageSprite(ref_tar, img, resName, setNativeSize, call_back or visible_call_back)
  667. end
  668. end
  669. end
  670. end
  671. --scroll_view的轴点需要是(0,1) 水平排列奖励列表等东西
  672. function AnchorItemListH( item_list, scroll_view, item_width, item_space_x, off_x, is_no_resize_view )
  673. item_space_x = item_space_x or 0
  674. off_x = off_x or 0
  675. local count = item_list.__count__ and item_list.__count__ or (#item_list)
  676. local scroll_width,_ = GetSizeDeltaXY(scroll_view)
  677. --计算开始显示的位置
  678. local size = count*(item_width+item_space_x)-item_space_x
  679. local start_x = ( scroll_width - size )/2
  680. if start_x < 0 then
  681. start_x = 0
  682. end
  683. for i,item in ipairs(item_list) do
  684. local x = (i-1) * (item_width+item_space_x) + start_x + off_x
  685. item:SetPosition(x, 0)
  686. end
  687. if not is_no_resize_view then
  688. SetSizeDelta(scroll_view,start_x+count*(item_width+item_space_x)-item_space_x, 100)
  689. end
  690. end
  691. --横向方向对齐,直接传节点(X轴锚点需要是0)),不是传组件
  692. function ItemSetHorizontal( off_x, ... )
  693. local node_list = {...}
  694. local off_x = off_x or 0--节点间的间隔
  695. local pos_x = 0
  696. local function get_node_is_label( node )
  697. return node:GetComponent("TMPro.TextMeshProUGUI") or node:GetComponent("Text")
  698. end
  699. for i,v in ipairs(node_list) do
  700. lb_component = get_node_is_label(v)
  701. if i == 1 then
  702. pos_x = GetAnchoredPosition( v.transform )
  703. else
  704. SetAnchoredPositionX( v.transform, pos_x )
  705. end
  706. pos_x = lb_component and (lb_component.preferredWidth + pos_x) or (pos_x + GetSizeDeltaX(v.transform))
  707. pos_x = off_x + pos_x
  708. end
  709. end
  710. --垂直方向对齐,直接传节点,不是传组件
  711. --(Y轴锚点需要是1,而且文本垂直方向要Upper)
  712. function ItemSetVerticle( off_y, ... )
  713. local node_list = {...}
  714. local off_y = off_y or 0--节点间的间隔
  715. local pos_y = 0
  716. local _ = false--无用参数
  717. local function get_node_is_label( node )
  718. return node:GetComponent("Text")
  719. end
  720. for i,v in ipairs(node_list) do
  721. lb_component = get_node_is_label(v)
  722. if i == 1 then
  723. _,pos_y = GetAnchoredPosition( v.transform )
  724. else
  725. SetAnchoredPositionY( v.transform, pos_y )
  726. end
  727. pos_y = lb_component and (lb_component.preferredHeight - pos_y) or (GetSizeDeltaY(v.transform) - pos_y)
  728. pos_y = off_y - pos_y
  729. end
  730. end
  731. --获取图片大小和维度, 获取 N维 图片UV数据UI
  732. function GetImageSplitUVRectInfo( size_x, size_y, row, col)
  733. local list = {}
  734. local cell_x = size_x / row
  735. local cell_y = size_y / col
  736. local uv_cell_x = 1 / row
  737. local uv_cell_y = 1 / col
  738. local uv_cell_w = uv_cell_x
  739. local uv_cell_h = uv_cell_y
  740. for i=1,row do
  741. for ii=1,col do
  742. local d = {}
  743. d.cell_x = cell_x
  744. d.cell_y = cell_y
  745. d.uv_cell_x = (i - 1)* uv_cell_x
  746. d.uv_cell_y = (ii - 1)* uv_cell_y
  747. d.uv_cell_w = uv_cell_w
  748. d.uv_cell_h = uv_cell_h
  749. table.insert(list, d)
  750. end
  751. end
  752. return list, cell_x, cell_y
  753. end