|
xlua 添加拖拽事件
- [CSharpCallLua]
- //声明回调的lua方法
- public delegate void LuaFuncWithPointerEventDataDelegate (BaseEventData eventData);
- //提供为gameobject对象添加拖动的回调
- public static void AddDragLiseter(GameObject gameObject
- ,LuaFuncWithPointerEventDataDelegate beginDragCallback
- ,LuaFuncWithPointerEventDataDelegate dragCallback
- ,LuaFuncWithPointerEventDataDelegate endDragCallback)
- {
- //添加组件
- EventTrigger eventTrigger = gameObject.GetOrAddComponent<EventTrigger>();
-
- //创建队列
- eventTrigger.triggers = new List<EventTrigger.Entry>();
- //初始化队列
- EventTrigger.Entry entry = new EventTrigger.Entry
- {
- eventID = EventTriggerType.BeginDrag,//事件类型
- callback = new EventTrigger.TriggerEvent() //创建回调函数
- };
-
- //添加回调函数
- entry.callback.AddListener((data)=> {
- beginDragCallback(data);
- });
- EventTrigger.Entry entry2 = new EventTrigger.Entry
- {
- eventID = EventTriggerType.Drag,//事件类型
- callback = new EventTrigger.TriggerEvent() //创建回调函数
- };
-
- //添加回调函数
- entry2.callback.AddListener((data)=> {
- dragCallback(data);
- });
- EventTrigger.Entry entry3 = new EventTrigger.Entry
- {
- eventID = EventTriggerType.EndDrag,//事件类型
- callback = new EventTrigger.TriggerEvent() //创建回调函数
- };
-
- //添加回调函数
- entry3.callback.AddListener((data)=> {
- endDragCallback(data);
- });
-
- //对事件系统的队列添加事件队列
- eventTrigger.triggers.Add(entry);
- eventTrigger.triggers.Add(entry2);
- eventTrigger.triggers.Add(entry3);
- }
复制代码- --添加拖拽事件
- LuaUtility.AddDragLiseter(cardItem.go,function(event)
- -- Log("beginDragCallback")
- self:_ItemOnBeginDrag(index,cardItem,event)
- end,function(event)
- -- Log("dragCallback")
- self:_ItemOnDrag(index,cardItem,event)
- end,function(event)
- -- Log("endDragCallback")
- self:_ItemOnEndDrag(index,cardItem,event)
- end)
- --在初始化item的时候,可以设置下tag,方便后续的射线检测
- cardItem.go.tag = "Card"
复制代码- --拖拽事件 开始
- function Ctrl:_ItemOnBeginDrag(index,cardItem,event)
- if cardItem.parent ~= self.panel.transform then
- self._dragBeginParentTransform = cardItem.parentTransform
- self._dragBeginPos = cardItem.transform.position
- self._dragBeginMousePos = Input.mousePosition
- cardItem.transform:SetParent(self.panel.transform)
- end
- end
- --拖拽事件 拖动中,需要在prefab中,关闭其他组件的raycastTarget,只留一个被检测到的
- function Ctrl:_ItemOnDrag(index,cardItem,event)
- cardItem.transform.position = self._dragBeginPos + (Input.mousePosition-self._dragBeginMousePos)
- if cardItem.transform:GetComponent("Image").raycastTarget then
- cardItem.transform:GetComponent("Image").raycastTarget = false;
- end
- end
- --拖拽事件 结束
- function Ctrl:_ItemOnEndDrag(index,cardItem,event)
- if event then
- local endGo = event.pointerCurrentRaycast.gameObject
- if endGo.tag == "Card" then --如果是可以交换的,这个在初始化中设置tag
- for swpaIndex, item in ipairs(self.cardItems) do
- if item.go == endGo then
- --进行换位置
- self:_SwapCardItemData(index,swpaIndex)
- end
- end
- -- else --其他任何情况,物体回归原始位置
-
- end
- --需要将raycastTarget重置为TRUE,方便下次被检测到
- cardItem.transform:GetComponent("Image").raycastTarget = true;
- cardItem.transform:SetParent(self._dragBeginParentTransform)
- cardItem.transform.position = self._dragBeginPos
- self._dragBeginParentTransform = nil
- self._dragBeginPos = nil
- self._dragBeginMousePos = nil
- end
- end
复制代码 到这里基本就完成了。需要注意的 item中的其他组件的关闭raycastTarget属性,只留一个被检测到的就可以了 |
|