1. Add drag start, drag, drag end events to Mahjong
C#
public class GAHelper
{
#region 添加3D和2D的UI事件
/// <summary>
/// 添加3D和2D的UI事件
/// </summary>
/// <param name="go"></param>
/// <param name="type">事件类型</param>
/// <param name="func"></param>
public static void AddUIEvent(GameObject go, EventTriggerType type, LuaFunction func)
{
if (go != null && func != null)
{
EventTrigger trigger = go.GetComponent<EventTrigger>();
if (trigger == null)
{
trigger = go.AddComponent<EventTrigger>();
}
EventTrigger.Entry entry = new EventTrigger.Entry();
entry.eventID = type;
entry.callback.AddListener((BaseEventData) =>
{
PointerEventData data = BaseEventData as PointerEventData;
func.Call(go,data);
});
trigger.triggers.Add(entry);
}
else
{
Debug.LogError("AddBeginDrug的go或func为Null");
}
}
#endregion
}
take
function HandCardController.Awake()
--添加拖拽事件
GAHelper.AddUIEvent(go,UnityEngine.EventSystems.BeginDrag,HandCardController.OnBeginMouseDrag)--添加拖拽开始事件
GAHelper.AddUIEvent(go,UnityEngine.EventSystems.Drag,HandCardController.OnMouseDrag)--添加拖拽中事件
GAHelper.AddUIEvent(go,UnityEngine.EventSystems.EndDrag,HandCardController.OnEndMouseDrag)--添加拖拽结束事件
end
--拖拽开始
function HandCardController.OnBeginMouseDrag(go,evDate)
this.IsInDrag=true --是否在拖拽中
end
--鼠标拖拽中
function HandCardController.OnMouseDrag(go,evDate )
local pos=this.HandCamera:WorldToScreenPoint(obj.transform.position)
go.transform.position=this.HandCamera:ScreenToWorldPoint(Vector3.New(Input.mousePosition.x,Input.mousePosition.y,pos.z))
--this.HandCamera是照射3D麻将的摄像机
end
--鼠标拖拽结束
function HandCardController.OnEndMouseDrag(go,evDate )
local Pos=this:GetShouPaiPosList(EU_CardOnwer.Bottom,nil)--手牌位置集合,固定的
local id=this.listcard:IndexOf(go)--得到这个对象在手牌位置集合中的索引
if (this.isCanPutCard == true) then--如果能出牌
local posx=obj.transform.localPosition.x
local posz=obj.transform.localPosition.z
--确定能出牌的区域
if(posx>0.02 and posx<0.33 and posz>0.045 and posz<0.1)then
this:SelfPutCard(obj);
else--出了出牌区域
if(Pos[id]~=nil)then
obj.transform.localPosition = Pos[id]
else--如果这个对象在手帕位置集合没有,就是在摸牌位置的牌
if(this.cobj==obj)then--判读摸牌位置的牌是否为空
obj.transform.localPosition=this.lastPos--设置死的位置
end
end
end
else --不能出牌
if(Pos[id]~=nil)then
obj.transform.localPosition = Pos[id]
else
if(this.cobj==obj)then
obj.transform.localPosition=this.lastPos
end
end
end
--重置麻将的Card对象上的属性
this.IsInDrag=false --是否在拖拽中
obj.transform:GetComponent("RWXMajhong.Card").isMouse=false
obj.transform:GetComponent("RWXMajhong.Card").isJump=false
end