日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

FGUI手势案例代码

發布時間:2023/12/9 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 FGUI手势案例代码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

記錄一下fgui的幾種手勢的代碼實例
下面是以左上角為中心點的縮放

local worldMapView = {} local this = worldMapViewlocal comps local controllocal doubleClickDist = 0 --雙指初始距離 local singleClickpos = 0 --單指初始位置 local singleClickDist = 0 --單指移動位置差 local singleClickMapPos = nil --單擊時map的位置 local mapPosSetting = {} --地圖移動范圍 local SwipeGestureEvent = nil --單指手勢事件 local PinchGestureEvent = nil --雙指手勢事件 local curMapScale = 1 --當前地圖放縮倍率 local mouseWheelRate = 0.05 --滾輪縮放比率------------------------------------------------------------------------------------------------------------------------------------------------------ local SetMap = function(_, item)local tempControl = {item:GetChild("mapBtn01"),item:GetChild("mapBtn02"),item:GetChild("mapBtn03"),item:GetChild("mapBtn04"),item:GetChild("mapBtn05"),item:GetChild("mapBtn06"),item:GetChild("mapBtn07"),}for index, mapItem in pairs(tempControl) domapItem.enabled = (index == 1)if (index == 1) thenthis.ui:TouchClick(mapItem, function()uiMgr.openUI(Constants.ViewName.mainHud)this.ui:Close()end)endend endlocal SetMapScale = function(scaleVal)curMapScale = scaleValmapPosSetting.xMin = GRoot.inst.width - control.mapLayer.width * curMapScalemapPosSetting.yMin = GRoot.inst.height - control.mapLayer.height * curMapScale endlocal MapLayerScale = function(add)local scaleVal = control.mapLayer.scaleX + addscaleVal = (scaleVal > mapPosSetting.scaleMax) and mapPosSetting.scaleMax or scaleValscaleVal = (scaleVal < mapPosSetting.scaleMin) and mapPosSetting.scaleMin or scaleValcontrol.mapLayer:SetScale(scaleVal, scaleVal)SetMapScale(scaleVal) end--雙指事件move local DoubleClickMoveEvent = function(context)MapLayerScale(context.sender.delta) end--單指事件begin local SingleClickBeginEvent = function(context)singleClickpos = context.inputEvent.positionsingleClickMapPos = control.mapLayer.position end--單指事件move local SingleClickMoveEvent = function(context)local tempPos = context.inputEvent.position - singleClickpos + singleClickMapPostempPos.x = (tempPos.x > mapPosSetting.xMax) and mapPosSetting.xMax or tempPos.xtempPos.x = (tempPos.x < mapPosSetting.xMin) and mapPosSetting.xMin or tempPos.xtempPos.y = (tempPos.y > mapPosSetting.yMax) and mapPosSetting.yMax or tempPos.ytempPos.y = (tempPos.y < mapPosSetting.yMin) and mapPosSetting.yMin or tempPos.ycontrol.mapLayer.position = tempPoscontrol.mapLayer:InvalidateBatchingState() end--------------------------------------------------------------------------- --初始化節點 local InitControl = function()control = {backBtn = comps.backBtn,mapLayer = comps.mapLayer,}mapPosSetting = {xMin = GRoot.inst.width - control.mapLayer.width * curMapScale,xMax = 0,yMin = GRoot.inst.height - control.mapLayer.height * curMapScale,yMax = 0,scaleMax = control.mapLayer.width / 2500,scaleMin = GRoot.inst.height / control.mapLayer.height,}SetMapScale(curMapScale)end--注冊點擊事件 local BindBtnEvent = function()--部隊配置this.ui:TouchClick(control.backBtn, function()uiMgr.openUI(Constants.ViewName.options)this.ui:Close()end)--單指手勢SwipeGestureEvent = this.ui:SwipeGesture(control.mapLayer, function(context)SingleClickBeginEvent(context)end, function(context)SingleClickMoveEvent(context)end, function()end)--雙指手勢PinchGestureEvent = this.ui:PinchGesture(GRoot.inst, function(context)end, function(context)DoubleClickMoveEvent(context)end, function()end)--滾輪事件if UnityEngine.Application.platform == UnityEngine.RuntimePlatform.WindowsEditor orUnityEngine.Application.platform == UnityEngine.RuntimePlatform.WindowsPlayer thenthis.ui:OnMouseWheelEvent(control.mapLayer, function(context)local delta = context.inputEvent.mouseWheelDeltalocal val = (delta < 0) and mouseWheelRate or (mouseWheelRate * -1)MapLayerScale(val)end)end--測試-- curMapScale = mapPosSetting.scaleMax-- SetMapScale(curMapScale)-- control.mapLayer:SetScale(mapPosSetting.scaleMax, mapPosSetting.scaleMax) end--初始化ui local InitUI = function()SetMap(nil, control.mapLayer) end--------------------------------------------------------------------------- --onUpdate function this.onUpdate() end--OnInit方法名自動生成 請勿修改 function worldMapView.OnInit(root)--自動生成 不可刪除this.ui = BaseUI:New(root)helper.logWarn("FairyGUI Lua => worldMapView onInit");comps = this.ui.compscontrol = {}InitControl()BindBtnEvent() end--注冊事件方法名自動生成 請勿修改 function worldMapView.OnRegisterEvent()helper.logWarn("FairyGUI Lua => worldMapView onRegisterEvent"); end --RemoveAllEvent方法名自動生成 請勿修改 function worldMapView.RemoveAllEvent()--移除事件綁定helper.logWarn("FairyGUI Lua => worldMapView removeAllEvent"); end--OnEnter方法名自動生成 請勿修改 function worldMapView.OnEnter(...)local args = {...}-----------------------------獲取并初始化組件this.OnRegisterEvent()helper.logWarn("FairyGUI Lua => worldMapView onEnter");InitUI() end --OnExit方法名自動生成 請勿修改 function worldMapView.OnExit()--關閉一個UIthis.RemoveAllEvent()helper.logWarn("FairyGUI Lua => worldMapView OnExit") end--OnExitAll方法名自動生成 請勿修改 function worldMapView.OnExitAll()--關閉所有UIhelper.logWarn("FairyGUI Lua => worldMapView OnExitAll") end--OnDestroy方法名自動生成 請勿修改 function worldMapView.OnDestroy()helper.logWarn("FairyGUI Lua => worldMapView OnDestroy")this.ui = nilif (SwipeGestureEvent ~= nil) thenSwipeGestureEvent:Dispose()endif (PinchGestureEvent ~= nil) thenPinchGestureEvent:Dispose()end end--方法名自動生成 請勿修改 return worldMapView

這個是手勢的一些接口

-- 長按 手勢 function BaseUI:LongPressGesture(gobject, begincb, movecb,endcb,params)local longPress = LongPressGesture.New(gobject)if params thenlongPress.once = params.once or falselongPress.interval = params.interval or 0.3--第一次 派發 事件的 觸發間隔(長按 開始 響應的時間)longPress.trigger = params.trigger or 1.5--手指按住后,移動超出此半徑范圍則手勢停止。longPress.holdRangeRadius = params.holdRangeRadius or 50longPress.itemIndex = params.itemIndex or 0end--開始的回調if begincb thenlongPress.onBegin:Add(begincb)end-- 每隔 多久 觸發的回調if movecb thenlongPress.onAction:Add(movecb)end-- 結束的 回調if endcb thenlongPress.onEnd:Add(endcb) endreturn longPressend-- 單指 手勢 function BaseUI:SwipeGesture(gobject, begincb, movecb, endcb)local swipeGesture = SwipeGesture.New(gobject)if begincb thenswipeGesture.onBegin:Add(begincb)endif movecb thenswipeGesture.onMove:Add(movecb)endif endcb thenswipeGesture.onEnd:Add(endcb) endreturn swipeGestureend-- 雙指 手勢 function BaseUI:PinchGesture(gobject, begincb, movecb, endcb)local pinchGesture = PinchGesture.New(gobject)if begincb thenpinchGesture.onBegin:Add(begincb)endif movecb thenpinchGesture.onAction:Add(movecb)endif endcb thenpinchGesture.onEnd:Add(endcb) endreturn pinchGestureend

新增縮放以雙指中心及鼠標點為中心的縮放邏輯

local worldMapView = {} local this = worldMapViewlocal comps local controllocal doubleClickDist = 0 --雙指初始距離 local singleClickpos = 0 --單指初始位置 local singleClickDist = 0 --單指移動位置差 local singleClickMapPos = nil --單擊時map的位置 local mapSetting = {} --地圖移動范圍 local SwipeGestureEvent = nil --單指手勢事件 local PinchGestureEvent = nil --雙指手勢事件 local curMapScale = 1 --當前地圖放縮倍率 local mouseWheelRate = 0.01 --滾輪縮放比率 local doubleClickCenterPos = nil --雙指事件兩指中心位置 local curUnLockMapId = 1 --當前解鎖的最新地圖Id------------------------------------------------------------------------------------------------------------------------------------------------------ local SetMap = function()for index, mapItem in pairs(control.mapItemList) domapItem.enabled = (index <= curUnLockMapId)if (index <= curUnLockMapId) thenthis.ui:TouchClick(mapItem, function()uiMgr.openUI(Constants.ViewName.mainHud)this.ui:Close()end)endend endlocal SetMapScale = function(scaleVal)curMapScale = scaleValmapSetting.xMin = GRoot.inst.width - control.mapLayer.width * curMapScalemapSetting.yMin = GRoot.inst.height - control.mapLayer.height * curMapScale endlocal SetMapPos = function(tempPos)tempPos.x = (tempPos.x > mapSetting.xMax) and mapSetting.xMax or tempPos.xtempPos.x = (tempPos.x < mapSetting.xMin) and mapSetting.xMin or tempPos.xtempPos.y = (tempPos.y > mapSetting.yMax) and mapSetting.yMax or tempPos.ytempPos.y = (tempPos.y < mapSetting.yMin) and mapSetting.yMin or tempPos.ycontrol.mapLayer.position = tempPos endlocal MapLayerScale = function(add, mousePos)local scaleVal = control.mapLayer.scaleX + addscaleVal = (scaleVal > mapSetting.scaleMax) and mapSetting.scaleMax or scaleValscaleVal = (scaleVal < mapSetting.scaleMin) and mapSetting.scaleMin or scaleVallocal ratio = (add > 0) and -1 or 1local addWidth = math.abs(mapSetting.orginWidth * (scaleVal - curMapScale))local addHeight = math.abs(mapSetting.orginHeight * (scaleVal - curMapScale))local mapPos = control.mapLayer.positionmapPos.x = control.mapLayer.x + (mousePos.x / mapSetting.orginWidth) * ratio * addWidthmapPos.y = control.mapLayer.y + (mousePos.y / mapSetting.orginHeight) * ratio * addHeightSetMapScale(scaleVal)SetMapPos(mapPos)control.mapLayer:SetScale(scaleVal, scaleVal) end--雙指事件begin local DoubleClickBeginEvent = function(context)local pos1 = control.mapLayer:GlobalToLocal(Stage.inst:GetTouchPosition(0))local pos2 = control.mapLayer:GlobalToLocal(Stage.inst:GetTouchPosition(1))doubleClickCenterPos = (pos1 + pos2) / 2 end--雙指事件move local DoubleClickMoveEvent = function(context)MapLayerScale(context.sender.delta, doubleClickCenterPos) end--單指事件begin local SingleClickBeginEvent = function(context)singleClickpos = context.inputEvent.positionsingleClickMapPos = control.mapLayer.position end--單指事件move local SingleClickMoveEvent = function(context)local tempPos = context.inputEvent.position - singleClickpos + singleClickMapPosSetMapPos(tempPos)control.mapLayer:InvalidateBatchingState() end--------------------------------------------------------------------------- --初始化節點 local InitControl = function()control = {backBtn = comps.backBtn,mapLayer = comps.mapLayer,mapItemList = {comps.mapLayer:GetChild("mapBtn01"),comps.mapLayer:GetChild("mapBtn02"),comps.mapLayer:GetChild("mapBtn03"),comps.mapLayer:GetChild("mapBtn04"),comps.mapLayer:GetChild("mapBtn05"),comps.mapLayer:GetChild("mapBtn06"),comps.mapLayer:GetChild("mapBtn07"),},}mapSetting = {xMin = GRoot.inst.width - control.mapLayer.width * curMapScale,xMax = 0,yMin = GRoot.inst.height - control.mapLayer.height * curMapScale,yMax = 0,scaleMax = control.mapLayer.width / 2500,scaleMin = GRoot.inst.height / control.mapLayer.height,orginWidth = control.mapLayer.width,orginHeight = control.mapLayer.height,}SetMapScale(curMapScale)end--注冊點擊事件 local BindBtnEvent = function()--部隊配置this.ui:TouchClick(control.backBtn, function()uiMgr.openUI(Constants.ViewName.options)this.ui:Close()end)--單指手勢SwipeGestureEvent = this.ui:SwipeGesture(control.mapLayer, function(context)SingleClickBeginEvent(context)end, function(context)SingleClickMoveEvent(context)end, function()end)--雙指手勢PinchGestureEvent = this.ui:PinchGesture(GRoot.inst, function(context)DoubleClickBeginEvent(context)end, function(context)DoubleClickMoveEvent(context)end, function()end)--滾輪事件if UnityEngine.Application.platform == UnityEngine.RuntimePlatform.WindowsEditor orUnityEngine.Application.platform == UnityEngine.RuntimePlatform.WindowsPlayer thenthis.ui:OnMouseWheelEvent(control.mapLayer, function(context)local mousePos = control.mapLayer:GlobalToLocal(context.inputEvent.position)local delta = context.inputEvent.mouseWheelDeltalocal val = (delta < 0) and mouseWheelRate or (mouseWheelRate * -1)MapLayerScale(val, mousePos)end)endend--初始化數據 local InitData = function()curUnLockMapId = 1 end--初始化ui local InitUI = function()SetMap()--鏡頭鎖定解鎖地圖local tempPos = control.mapLayer.positiontempPos.x = GRoot.inst.width / 2 - control.mapItemList[curUnLockMapId].x * curMapScaletempPos.y = GRoot.inst.height / 2 - control.mapItemList[curUnLockMapId].y * curMapScaleSetMapPos(tempPos) end--------------------------------------------------------------------------- --onUpdate function this.onUpdate() end--OnInit方法名自動生成 請勿修改 function worldMapView.OnInit(root)--自動生成 不可刪除this.ui = BaseUI:New(root)helper.logWarn("FairyGUI Lua => worldMapView onInit");comps = this.ui.compscontrol = {}InitControl()BindBtnEvent() end--注冊事件方法名自動生成 請勿修改 function worldMapView.OnRegisterEvent()helper.logWarn("FairyGUI Lua => worldMapView onRegisterEvent"); end --RemoveAllEvent方法名自動生成 請勿修改 function worldMapView.RemoveAllEvent()--移除事件綁定helper.logWarn("FairyGUI Lua => worldMapView removeAllEvent"); end--OnEnter方法名自動生成 請勿修改 function worldMapView.OnEnter(...)local args = {...}-----------------------------獲取并初始化組件this.OnRegisterEvent()helper.logWarn("FairyGUI Lua => worldMapView onEnter");InitData()InitUI() end --OnExit方法名自動生成 請勿修改 function worldMapView.OnExit()--關閉一個UIthis.RemoveAllEvent()helper.logWarn("FairyGUI Lua => worldMapView OnExit") end--OnExitAll方法名自動生成 請勿修改 function worldMapView.OnExitAll()--關閉所有UIhelper.logWarn("FairyGUI Lua => worldMapView OnExitAll") end--OnDestroy方法名自動生成 請勿修改 function worldMapView.OnDestroy()helper.logWarn("FairyGUI Lua => worldMapView OnDestroy")this.ui = nilif (SwipeGestureEvent ~= nil) thenSwipeGestureEvent:Dispose()endif (PinchGestureEvent ~= nil) thenPinchGestureEvent:Dispose()end end--方法名自動生成 請勿修改 return worldMapView

總結

以上是生活随笔為你收集整理的FGUI手势案例代码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。