Assets/Watson/Scripts/Utilities/TouchEventManager.cs
/// <summary>
/// Registers the tap event. 
/// 3d Colliders is the first priority
/// 2d Collider is the second priority 
/// 2d UI Event System is the third priority
/// If object has 3d collider, and 2d collider and an UI element. 
/// Touch is checking by priority order. 
/// </summary>
/// <returns><c>true</c>, if tap event was registered, <c>false</c> otherwise.</returns>
/// <param name="gameObjectToTouch">Game object to touch.</param>
/// <param name="callback">Callback.</param>
/// <param name="SortingLayer">Sorting layer.</param>
/// <param name="isTapInside">If set to <c>true</c> is tap inside.</param>
/// <param name="layerMask">Layer mask.</param>
Registers the tap event.
3d Colliders is the first priority
2d Collider is the second priority
2d UI Event System is the third priority
If object has 3d collider, and 2d collider and an UI element.
Touch is checking by priority order.
Registers the tap event . 3d Colliders is the first priority 2d Collider is the second priority 2d UI Event System is the third priority If object has 3d collider and 2d collider and an UI element . Touch is checking by priority order .
public bool RegisterTapEvent(GameObject gameObjectToTouch, string callback, int SortingLayer = 0, bool isTapInside = true, LayerMask layerMask = default(LayerMask))
        {
            bool success = false;
            if (gameObjectToTouch != null)
            {
                if (!string.IsNullOrEmpty(callback))
                {
                    Collider[] colliderList = gameObjectToTouch.GetComponentsInChildren<Collider>();
                    if (colliderList != null && colliderList.Length > 0)
                    {
                        foreach (Collider itemCollider in colliderList)
                        {
                            int layerMaskAsKey = (layerMask != default(LayerMask)) ? layerMask.value : (1 << gameObjectToTouch.layer);
                            #if ENABLE_DEBUGGING
                            Log.Debug("TouchEventManager", "RegisterTapEvent for 3D. itemCollider: {0}, callback: {1}, SortingLayer: {2}, isTapInside: {3}",itemCollider, callback, SortingLayer, isTapInside);
                            #endif
                            if (m_TapEvents.ContainsKey(layerMaskAsKey))
                            {
                                m_TapEvents[layerMaskAsKey].Add(new TouchEventData(itemCollider, callback, SortingLayer, isTapInside));
                            }
                            else
                            {
                                m_TapEvents[layerMaskAsKey] = new List<TouchEventData>() { new TouchEventData(itemCollider, callback, SortingLayer, isTapInside) };
                            }
                        }
                        success = true;
                    }
                    else
                    {
                        #if ENABLE_DEBUGGING
                        Log.Debug("TouchEventManager", "There is no 3D collider of given gameobjectToTouch");
                        #endif
                    }
                    if (!success)
                    {
                        Collider2D[] colliderList2D = gameObjectToTouch.GetComponentsInChildren<Collider2D> ();
                        if (colliderList2D != null && colliderList2D.Length > 0)
                        {
                            foreach (Collider2D itemCollider in colliderList2D)
                            {
                                int layerMaskAsKey = (layerMask != default(LayerMask)) ? layerMask.value : (1 << gameObjectToTouch.layer);
                                #if ENABLE_DEBUGGING
                                Log.Debug("TouchEventManager", "RegisterTapEvent For 2D. itemCollider: {0}, callback: {1}, SortingLayer: {2}, isTapInside: {3}",itemCollider, callback, SortingLayer, isTapInside);
                                #endif
                                if (m_TapEvents.ContainsKey (layerMaskAsKey))
                                {
                                    m_TapEvents [layerMaskAsKey].Add (new TouchEventData (itemCollider, callback, SortingLayer, isTapInside));
                                } else
                                {
                                    m_TapEvents [layerMaskAsKey] = new List<TouchEventData> () { new TouchEventData (itemCollider, callback, SortingLayer, isTapInside) };
                                }
                            }
                            success = true;
                        } else
                        {
                            #if ENABLE_DEBUGGING
                            Log.Debug ("TouchEventManager", "There is no 2D collider of given gameobjectToTouch");
                            #endif
                        }
                    }
                    #if UNITY_4_6 || UNITY_5 || UNITY_5_3_OR_NEWER
                    if (!success)
                    {
                        RectTransform[] rectTransformList = gameObjectToTouch.GetComponentsInChildren<RectTransform> (includeInactive: true);
                        if (rectTransformList != null && rectTransformList.Length > 0)
                        {
                            foreach (RectTransform itemRectTransform in rectTransformList)
                            {
                                int layerMaskAsKey = (layerMask != default(LayerMask)) ? layerMask.value : (1 << itemRectTransform.gameObject.layer);
                                #if ENABLE_DEBUGGING
                                Log.Debug("TouchEventManager", "RegisterTapEvent For 2D Event System. itemRectTransform: {0}, callback: {1}, SortingLayer: {2}, isTapInside: {3}",itemRectTransform, callback, SortingLayer, isTapInside);
                                #endif
                                if (m_TapEvents.ContainsKey (layerMaskAsKey))
                                {
                                    m_TapEvents [layerMaskAsKey].Add (new TouchEventData (itemRectTransform, callback, SortingLayer, isTapInside));
                                } else
                                {
                                    m_TapEvents [layerMaskAsKey] = new List<TouchEventData> () { new TouchEventData (itemRectTransform, callback, SortingLayer, isTapInside) };
                                }
                            }
                            success = true;
                        } else
                        {
                            #if ENABLE_DEBUGGING
                            Log.Debug ("TouchEventManager", "There is no Rect Transform of given gameobjectToTouch");
                            #endif
                        }
                    }
                    #endif
                }
                else
                {
                    Log.Warning("TouchEventManager", "There is no callback for tap event registration");
                }
            }
            else
            {
                Log.Warning("TouchEventManager", "There is no gameobject for tap event registration");
            }
            return success;
        }
public bool RegisterTapEvent ( GameObject gameObjectToTouch , string callback , int SortingLayer = 0 , bool isTapInside = true , LayerMask layerMask = default ( LayerMask ) ) { bool success = false ; if ( gameObjectToTouch != null ) { if ( ! string . IsNullOrEmpty ( callback ) ) { Collider [ ] colliderList = gameObjectToTouch . GetComponentsInChildren < Collider > ( ) ; if ( colliderList != null && colliderList . Length > 0 ) { foreach ( Collider itemCollider in colliderList ) { int layerMaskAsKey = ( layerMask != default ( LayerMask ) ) ? layerMask . value : ( 1 << gameObjectToTouch . layer ) ; if ENABLE_DEBUGGING Log . Debug ( " " , " " , itemCollider , callback , SortingLayer , isTapInside ) ; endif if ( m_TapEvents . ContainsKey ( layerMaskAsKey ) ) { m_TapEvents [ layerMaskAsKey ] . Add ( new TouchEventData ( itemCollider , callback , SortingLayer , isTapInside ) ) ; } else { m_TapEvents [ layerMaskAsKey ] = new List < TouchEventData > ( ) { new TouchEventData ( itemCollider , callback , SortingLayer , isTapInside ) } ; } } success = true ; } else { if ENABLE_DEBUGGING Log . Debug ( " " , " " ) ; endif } if ( ! success ) { Collider2D [ ] colliderList2D = gameObjectToTouch . GetComponentsInChildren < Collider2D > ( ) ; if ( colliderList2D != null && colliderList2D . Length > 0 ) { foreach ( Collider2D itemCollider in colliderList2D ) { int layerMaskAsKey = ( layerMask != default ( LayerMask ) ) ? layerMask . value : ( 1 << gameObjectToTouch . layer ) ; if ENABLE_DEBUGGING Log . Debug ( " " , " " , itemCollider , callback , SortingLayer , isTapInside ) ; endif if ( m_TapEvents . ContainsKey ( layerMaskAsKey ) ) { m_TapEvents [ layerMaskAsKey ] . Add ( new TouchEventData ( itemCollider , callback , SortingLayer , isTapInside ) ) ; } else { m_TapEvents [ layerMaskAsKey ] = new List < TouchEventData > ( ) { new TouchEventData ( itemCollider , callback , SortingLayer , isTapInside ) } ; } } success = true ; } else { if ENABLE_DEBUGGING Log . Debug ( " " , " " ) ; endif } } if UNITY_4_6 || UNITY_5 || UNITY_5_3_OR_NEWER if ( ! success ) { RectTransform [ ] rectTransformList = gameObjectToTouch . GetComponentsInChildren < RectTransform > ( includeInactive : true ) ; if ( rectTransformList != null && rectTransformList . Length > 0 ) { foreach ( RectTransform itemRectTransform in rectTransformList ) { int layerMaskAsKey = ( layerMask != default ( LayerMask ) ) ? layerMask . value : ( 1 << itemRectTransform . gameObject . layer ) ; if ENABLE_DEBUGGING Log . Debug ( " " , " " , itemRectTransform , callback , SortingLayer , isTapInside ) ; endif if ( m_TapEvents . ContainsKey ( layerMaskAsKey ) ) { m_TapEvents [ layerMaskAsKey ] . Add ( new TouchEventData ( itemRectTransform , callback , SortingLayer , isTapInside ) ) ; } else { m_TapEvents [ layerMaskAsKey ] = new List < TouchEventData > ( ) { new TouchEventData ( itemRectTransform , callback , SortingLayer , isTapInside ) } ; } } success = true ; } else { if ENABLE_DEBUGGING Log . Debug ( " " , " " ) ; endif } } endif } else { Log . Warning ( " " , " " ) ; } } else { Log . Warning ( " " , " " ) ; } return success ; }
Registers the tap event.
Registers the tap event .
None
gameObjectToTouch
GameObject
Game object to touch.
Game object to touch .
callback
string
None
SortingLayer
int
None
isTapInside
bool
If set to true
If set to true
layerMask
LayerMask
None