Finger Touch 是一種處理手勢識別的插件,在遊戲開發中常常會用到,這裏我先簡單介紹一下Finger Touch(若有錯誤 歡迎指正) :this
#region Event
// Fired when a finger begins touching the screen (LeanFinger = The current finger)
public static System.Action<CheapFinger> OnFingerDown;
// Fired when a finger stops touching the screen (LeanFinger = The current finger)
public static System.Action<CheapFinger> OnFingerUp;
// Fired when a finger taps the screen (this is when a finger begins and stops touching the screen within the 'TapThreshold' time) (LeanFinger = The current finger)
public static System.Action<CheapFinger> OnFingerTap;
// Fired when a finger swipes the screen (this is when a finger begins and stops touching the screen within the 'TapThreshold' time, and also moves more than the 'SwipeThreshold' distance) (LeanFinger = The current finger)
public static System.Action<CheapFinger> OnFingerSwipe;
// Fired when a finger begins being held on the screen (this is when a finger has been set for longer than the 'HeldThreshold' time) (LeanFinger = The current finger)
public static System.Action<CheapFinger> OnFingerHeldDown;插件
// Fired when at least one finger taps the screen (int = Highest Finger Count)
public static System.Action<int> OnMultiTap;對象
// add: pinch/drag start/end event
public static System.Action OnPinchStart;
public static System.Action OnPinchEnd;
public static System.Action<CheapFinger> OnDragStart;
public static System.Action OnDragEnd;
#endregion繼承
Finger touch 中 事件 有以上 按下 、擡起、敲擊、扭轉、長按、多指敲擊、拖拽等遊戲
其次介紹一下:TouchScreen (自定義類)事件
TouchScreen 中 在 作 4件事ip
第一:監測手勢觸發點遊戲開發
第二: 將屏幕觸發點轉化爲射線開發
第三:經過射線檢測是否觸發到某個objstring
第四:將射線傳遞給Lua
簡要代碼 以下:
public void Tick(float deltaTime)
{
if (Input.touchCount > 0 && (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(0).phase == TouchPhase.Stationary))
{
_currentTouchTime += deltaTime;
if (_currentTouchTime > MIN_TIME)
{
_currentTouchTime = 0.0f;
CallLuaFunction(ON_TOUCH_MOVE);
}
}
#if UNITY_STANDALONE_WIN
else if(Input.GetMouseButton(0))
{
_currentTouchTime += deltaTime;
if (_currentTouchTime > MIN_TIME)
{
_currentTouchTime = 0.0f;
CallLuaFunction(ON_TOUCH_MOVE);
}
}
#endif
else
{
_currentTouchTime = 0.0f;
}
}
private void CallLuaFunction(string functionName)
{
if (string.IsNullOrEmpty(functionName))
{
return;
}
var ray = rayCamera.ScreenPointToRay(Input.mousePosition);
var hitInfo = GetRayCastHit ();
if (hitInfo != null && hitInfo.Length > 0)
{
if (!HitUI(Input.mousePosition))
{
LuaScriptMgr.Instance.CallLuaFunction("TouchAgent." + functionName, hitInfo);
}
}
}
CheapTouch 繼承MonoBehavior ,CheapTouch 實現OnEnable、Update方法;
咱們要在OnEnable 和 update 裏作什麼處理呢?這個就是須要和Lua 邏輯 關聯起來
OnEnable 中 設置當前實例,也就是當CheapTouch 掛到遊戲某個對象上時 實例化 該 腳本。
Update 中作二件事:
1.TouchScreen 實例傳給Lua
2.更新各類操做和事件 如:
UpdateFingers();
UpdateMultiTap();
UpdateGestures();
UpdateEvents();
UpdateEvent();處理事件 包括 點擊 拖拽 扭轉 等等並調用Lua
例如點擊 事件:
private void RaiseFingerDownEvent(CheapFinger finger)
{
finger.SetCurUIGameObject();
fingerType = FINGER_DOWN;
CallLuaEventMethod(finger);
if (OnFingerDown != null) { OnFingerDown(finger); } }