示例#1
0
        public static List <Unit> GetUnitsUnderMouse()
        {
            tempUnits.Clear();
            Vector2 mousePos = InputManager.MousePos;

            int count = Physics2D.GetRayIntersectionNonAlloc(new Ray(new Vector3(mousePos.x, mousePos.y, -100f), Vector3.forward), hits, 200f, staticMask);

            if (count > hits.Length)
            {
                Debug.LogError($"Number of intersection hits ({count}) exceeded the maximum of {hits.Length}.");
                count = hits.Length;
            }

            for (int i = 0; i < count; i++)
            {
                var hit  = hits[i];
                var unit = hit.transform.GetComponentInParent <Unit>();
                if (unit != null && !tempUnits.Contains(unit))
                {
                    tempUnits.Add(unit);
                }
            }

            return(tempUnits);
        }
    protected bool HandleRaycast(Vector3 inputPosition)
    {
        var ray = Camera.main.ScreenPointToRay(inputPosition);

        RaycastHit2D[] hits = new RaycastHit2D[1];

        var hitsCount = Physics2D.GetRayIntersectionNonAlloc(ray, hits, 100f, GetLayerMask());

        if (hitsCount == 0)
        {
            return(false);
        }

        var hitGameObject = hits[0].collider.gameObject;

        if (hitGameObject.layer != GetLayerIndex())
        {
            return(false);
        }

        if (hitGameObject != gameObject)
        {
            return(false);
        }

        return(true);
    }
示例#3
0
        private void FixedUpdate()
        {
            if (!Input.GetMouseButton(0))
            {
                return;
            }

            var ray    = Camera.main.ScreenPointToRay(Input.mousePosition);
            var result = new RaycastHit2D[1];

            Physics2D.GetRayIntersectionNonAlloc(ray, result);

            foreach (var hit2D in result)
            {
                if (!hit2D.collider)
                {
                    continue;
                }
                if (!hit2D.collider.gameObject.CompareTag("Enemy Place"))
                {
                    continue;
                }

                if (!LevelManager.Instance.IsGamePaused() && !isBulletFire)
                {
                    StartCoroutine(Fire());
                }
            }
        }
示例#4
0
    public static Unit GetUnder(Vector2 pos)
    {
        Ray r     = new Ray(new Vector3(0f, 0f, -100f) + (Vector3)pos, Vector3.forward);
        int total = Physics2D.GetRayIntersectionNonAlloc(r, hits);

        if (total > MAX_RAY_HITS)
        {
            Debug.LogWarning("Number of raycast hits under the unit selection exceeds the max capacity.");
            total = MAX_RAY_HITS;
        }

        for (int i = 0; i < total; i++)
        {
            var hit = hits[i];

            if (hit.collider.isTrigger)
            {
                continue;
            }

            var unit = hit.transform.GetComponentInParent <Unit>();
            if (unit != null)
            {
                return(unit);
            }
        }
        return(null);
    }
示例#5
0
        private void OnConfirm(InputAction.CallbackContext ctx)
        {
            var ray = SceneObjRef.Instance.MainCamera.ScreenPointToRay(mousePos);
            var cnt = Physics2D.GetRayIntersectionNonAlloc(ray, targetBlock, 10, layerMask);

            if (cnt < 1)
            {
                Debug.Log($"F{Time.frameCount} NO HIT");
                return;
            }
            var nameof  = targetBlock[0].transform.name;
            var substrs = nameof.Split(' ');
            var x       = int.Parse(substrs[1]);
            var y       = int.Parse(substrs[2]);

            Debug.Log($"F{Time.frameCount}. ({x}, {y}) = {x + y * 25}");
            var spr = targetBlock[0].transform.GetComponentInChildren <SpriteRenderer>();

            if (spr.color.r > .5f)
            {
                spr.color = new Color(0, 1, 0, 1);
                mapdescsobj.blockDescs[x + y * 25].canOccupy = true;
            }
            else
            {
                spr.color = new Color(1, 1, 1, 0.2f);
                mapdescsobj.blockDescs[x + y * 25].canOccupy = false;
            }
        }
示例#6
0
    private void StartDrag()
    {
        Vector3 mousePosition = inputHandler.MousePosition;

        if (Physics2D.GetRayIntersectionNonAlloc(camera.ScreenPointToRay(mousePosition)
                                                 , results, Mathf.Infinity, _bodyPartLayer) != 0)
        {
            BodyPart bodyPart;
            for (int i = 0; i < results.Length; ++i)
            {
                bodyPart = results[i].transform.gameObject.GetComponent <BodyPart>();
                if (bodyPart.Slot == null)
                {
                    itemEvent?.Invoke(bodyPart.item);
                }
                else
                {
                    GameManager.Instance.PlayDetach();
                    bodyPart.Slot.Remove(bodyPart);
                    bodyPart.Slot = null;
                }

                objectIsSelected = true;
                startPos         = camera.ScreenToWorldPoint(mousePosition);
                offset           = camera.ScreenToWorldPoint(mousePosition) - results[i].transform.position;
                selectedObject   = bodyPart;
                break;
            }
        }
    }
示例#7
0
 public void RayIntersection(
     Ray ray,
     float distance = float.PositiveInfinity,
     int layerMask  = Physics2D.DefaultRaycastLayers)
 {
     Count = Physics2D.GetRayIntersectionNonAlloc(ray, Results, distance, layerMask);
 }
示例#8
0
        private void Update()
        {
            if (!Input.GetMouseButtonDown(0))
            {
                return;
            }

            Vector3 mousePosition = Input.mousePosition;

            Ray r = mainCam.ScreenPointToRay(mousePosition);

            r.origin    = rayCaster.position;
            r.direction = rotationTransform * r.direction;

            int count = Physics2D.GetRayIntersectionNonAlloc(r, cache, 1000f, tooltipMask);

            if (count > 0)
            {
                ItemObjectBase newObj = cache[0].transform.GetComponent <ItemObjectBase>();

                tooltip.Enable();
                tooltip.ShowTooltip(newObj);
            }
            else
            {
                tooltip.Disable();
            }
        }
示例#9
0
 void CastRay(Vector3 mousePosition)
 {
     //Vector3 worldPoint = Camera.main.ScreenToWorldPoint(mousePosition);
     //if(Physics2D.Raycast(Vector2.zero, new Vector2(worldPoint.x, worldPoint.y))){
     //if(Physics.Raycast(Camera.main.ScreenPointToRay(mousePosition))){
     if (Physics2D.GetRayIntersectionNonAlloc(Camera.main.ScreenPointToRay(mousePosition), new RaycastHit2D[1]) > 0)
     {
         print("Hit");
     }
 }
示例#10
0
    private Vector3?GetMousePositionInWorldCoordinates()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        Debug.DrawRay(ray.origin, ray.direction * 10, Color.yellow);
        RaycastHit2D[] hits = new RaycastHit2D[1];
        if (Physics2D.GetRayIntersectionNonAlloc(ray, hits) > 0 && obstacleMask.value != (1 << hits[0].collider.gameObject.layer))
        {
            return(hits[0].point);
        }
        return(null);
    }
示例#11
0
    public void Update()
    {
        if (Camera == null)
        {
            return;
        }
        if (!Application.isEditor)
        {
            return;
        }

        if (CurrentlySelected == null && InputManager.IsDown("Select"))
        {
            int hits = Physics2D.GetRayIntersectionNonAlloc(MakeRay(), Hits);

            if (hits > 0)
            {
                for (int i = 0; i < hits; i++)
                {
                    RaycastHit2D hit = Hits[i];

                    Rigidbody2D rb = hit.rigidbody;
                    if (rb == null)
                    {
                        rb = hit.transform.GetComponentInParent <Rigidbody2D>();
                    }

                    if (rb != null)
                    {
                        if (CanDrag(rb))
                        {
                            CurrentlySelected = rb;
                            break;
                        }
                        else
                        {
                            continue;
                        }
                    }
                }
            }
        }

        if (CurrentlySelected != null)
        {
            if (!InputManager.IsPressed("Select"))
            {
                CurrentlySelected = null;
            }
        }
    }
示例#12
0
 void doDragStart(Vector3 v)
 {
     screenRay = Camera.main.ScreenPointToRay(v);
     Physics2D.GetRayIntersectionNonAlloc(screenRay, hitResults);
     //Debug.Log(hitResults[0].collider);
     if (hitResults[0].collider != null)
     {
         pointerWorldPosition = hitResults[0].point;
         insideBoardBoundary  = BoardUtilities.pointerInsideBoundary(pointerWorldPosition, gameBoardBoundary);
         if (insideBoardBoundary)
         {
             //Debug.Log("fire");
             fireOnDragStart(hitResults[0].collider.gameObject);
         }
     }
 }
        private void OnMove(InputAction.CallbackContext ctx)
        {
            hitPoint.Set(-1, -1);
            var mousePos = ctx.ReadValue <Vector2>();
            var ray      = mainCamera.ScreenPointToRay(mousePos);
            var cnt      = Physics2D.GetRayIntersectionNonAlloc(ray, targetBlock, 10, layerMask);

            // ---- Follow Mouse ----
            var worldPos = mainCamera.ScreenToWorldPoint(mousePos);

            worldPos.z = 0;
            followMouseHolder.transform.position = worldPos + (Vector3)TargetBuilding.spriteOffset;
            followMouseHolder.sprite             = TargetBuilding.mainImage;
            var alpha = followMouseHolder.color.a;

            followMouseHolder.color = new Color(1, 0, 0, alpha);
            // --------

            if (cnt < 1)
            {
                //Debug.Log($"F{Time.frameCount} No hit");
                return;
            }

            //Debug.Log($"F{Time.frameCount} {targetBlock[0].transform.name}");
            var subs = targetBlock[0].transform.name.Split(' ');
            var fx   = int.TryParse(subs[1], out var x);
            var fy   = int.TryParse(subs[2], out var y);

            if (!fx || !fy)
            {
                return;
            }

            // check if building can be set on hit point
            var canSetBlock = MapManager.Instance.CanSetBlock(x, y, TargetBuilding);

            if (!canSetBlock)
            {
                return;
            }

            hitPoint.Set(x, y);
            followMouseHolder.color = new Color(0, 1, 0, alpha);
        }
示例#14
0
        public bool TryGetRaycast2DObject(SGTouchCover touchCover, out GameObject hit2dObj)
        {
            hit2dObj          = null;
            _screenPointToRay = _camera.ScreenPointToRay(touchCover.Position);
            var n = _sortinglayerMasks.Length;

            for (var i = 0; i < n; ++i)
            {
                if (Physics2D.GetRayIntersectionNonAlloc(_screenPointToRay,
                                                         _raycastHit2Ds, raycastDistance, _sortinglayerMasks[i]) > 0)
                {
                    hit2dObj = _raycastHit2Ds[0].collider.gameObject;
                    return(true);
                }
            }

            return(false);
        }
        private void DoRaycast2D(Camera camera, Vector2 screenPosition, ref Component bestResult, ref float bestDistance, ref Vector3 bestPosition)
        {
            var ray   = camera.ScreenPointToRay(screenPosition);
            var count = Physics2D.GetRayIntersectionNonAlloc(ray, raycastHit2Ds, float.PositiveInfinity, Layers);

            if (count > 0)
            {
                var closestHit = raycastHit2Ds[0];
                var distance   = closestHit.distance;

                if (distance < bestDistance)
                {
                    bestDistance = distance;
                    bestResult   = closestHit.transform;
                    bestPosition = closestHit.point;
                }
            }
        }
示例#16
0
        public override void Raycast(PointerEventData eventData, List <RaycastResult> resultAppendList)
        {
            var eventPos = eventData.position;
            var ray      = _eventCamera.ScreenPointToRay(eventPos);
            var distance = _eventCamera.farClipPlane - _eventCamera.nearClipPlane;

            if (Physics2D.GetRayIntersectionNonAlloc(ray, _hitsCache, distance, eventMask) > 0)
            {
                var hitInfo = _hitsCache[0];
                _result.gameObject     = hitInfo.collider.gameObject;
                _result.module         = this;
                _result.distance       = (_result.gameObject.transform.position - _eventCameraTransform.position).sqrMagnitude;
                _result.index          = resultAppendList.Count;
                _result.worldPosition  = hitInfo.point;
                _result.screenPosition = eventPos;
                resultAppendList.Add(_result);
            }
        }
    public List <RaycastHit2D> DoRayCasting_2D(LayerMask sLayerMask_Hit)
    {
        _listHit_2D.Clear();

        Vector2 vecMousePosition;

        if (CalculateMousePosition_And_IsInScreen(out vecMousePosition) == false)
        {
            return(_listHit_2D);
        }

        int iHitCount = Physics2D.GetRayIntersectionNonAlloc(p_pEventCamera.ScreenPointToRay(vecMousePosition), _arrHit_2D, Mathf.Infinity, sLayerMask_Hit);

        for (int i = 0; i < iHitCount; i++)
        {
            _listHit_2D.Add(_arrHit_2D[i]);
        }

        return(_listHit_2D);
    }
示例#18
0
 public void ChooseSlot(BodyPart itemForSlot)
 {
     if (Physics2D.GetRayIntersectionNonAlloc(_camera.ScreenPointToRay(Input.mousePosition)
                                              , _results, Mathf.Infinity, _slotLayerNumber) != 0)
     {
         for (int i = 0; i < _results.Length; i++)
         {
             var slot = _results[i].transform.GetComponent <Slot>();
             if (slot.CheckEntry(itemForSlot))
             {
                 slot.Add(itemForSlot);
                 GameManager.Instance.PlayAttach();
                 return;
             }
         }
     }
     InventoryController.Instance.AddItem(itemForSlot.item);
     GameManager.Instance.PlayWrong();
     //inventoryController.AddItem(itemForSlot.item);
 }
        public override void Raycast(Ray ray, float distance, List <RaycastResult> raycastResults)
        {
            var hitCount = Physics2D.GetRayIntersectionNonAlloc(ray, hits, distance, RaycastMask);

            for (int i = 0; i < hitCount; ++i)
            {
                var sr = hits[i].collider.gameObject.GetComponent <SpriteRenderer>();

                raycastResults.Add(new RaycastResult
                {
                    gameObject     = hits[i].collider.gameObject,
                    module         = raycaster,
                    distance       = Vector3.Distance(ray.origin, hits[i].transform.position),
                    worldPosition  = hits[i].point,
                    worldNormal    = hits[i].normal,
                    screenPosition = Pointer3DInputModule.ScreenCenterPoint,
                    index          = raycastResults.Count,
                    sortingLayer   = sr != null ? sr.sortingLayerID : 0,
                    sortingOrder   = sr != null ? sr.sortingOrder : 0
                });
            }
        }
    // ========================================================================== //

    /* public - [Do] Function
     * 외부 객체가 호출(For External class call)*/


    // ========================================================================== //

    /* protected - Override & Unity API         */

    protected override IEnumerator OnEnableObjectCoroutine()
    {
        while (true)
        {
            bool bMouseClick = Input.GetMouseButtonDown(0);
            int  iHitCount   = Physics2D.GetRayIntersectionNonAlloc(pEventCamera.ScreenPointToRay(Input.mousePosition), _arrHit, Mathf.Infinity);
            for (int i = 0; i < iHitCount; i++)
            {
                RaycastHit2D pHit          = _arrHit[i];
                Transform    pTransformHit = pHit.transform;

                if (_bIsPrintDebug)
                {
                    Debug.Log(pTransformHit.name + " RayCast Hit", pTransformHit);
                }

                var pEnter = pTransformHit.GetComponent <IPointerEnterHandler>();
                if (pEnter != null)
                {
                    pEnter.OnPointerEnter(null);
                }

                //var pExit = pTransformHit.GetComponent<IPointerExitHandler>();
                //if (pExit != null)
                //    pExit.OnPointerExit(null);

                if (bMouseClick)
                {
                    var pClick = pTransformHit.GetComponent <IPointerClickHandler>();
                    if (pClick != null)
                    {
                        pClick.OnPointerClick(null);
                    }
                }
            }

            yield return(null);
        }
    }
示例#21
0
        protected virtual void Update()
        {
            if (Input.GetKeyDown(Requires) == true)
            {
                var camera = D2dHelper.GetCamera(Camera, gameObject);

                if (camera != null)
                {
                    var ray   = camera.ScreenPointToRay(Input.mousePosition);
                    var count = Physics2D.GetRayIntersectionNonAlloc(ray, raycastHit2Ds, float.PositiveInfinity, Layers);

                    if (count > 0)
                    {
                        for (var i = 0; i < count; i++)
                        {
                            var raycastHit2D = raycastHit2Ds[i];

                            if (TryFracture(raycastHit2D) == true)
                            {
                                // Spawn prefab?
                                if (Prefab != null)
                                {
                                    var clone = Instantiate(Prefab, raycastHit2D.point, Quaternion.Euler(0.0f, 0.0f, Random.Range(0.0f, 360.0f)));

                                    clone.SetActive(true);
                                }

                                if (Hit == HitType.First)
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
        public override void Raycast(BaseRaycaster module, Vector2 position, Camera eventCamera, List <RaycastResult> raycastResults)
        {
            var ray      = eventCamera.ScreenPointToRay(position);
            var distance = eventCamera.farClipPlane - eventCamera.nearClipPlane;
            var hitCount = Physics2D.GetRayIntersectionNonAlloc(ray, hits, distance, RaycastMask);

            for (int i = 0; i < hitCount; ++i)
            {
                var sr = hits[i].collider.gameObject.GetComponent <SpriteRenderer>();

                raycastResults.Add(new RaycastResult
                {
                    gameObject     = hits[i].collider.gameObject,
                    module         = module,
                    distance       = Vector3.Distance(eventCamera.transform.position, hits[i].transform.position),
                    worldPosition  = hits[i].point,
                    worldNormal    = hits[i].normal,
                    screenPosition = position,
                    index          = raycastResults.Count,
                    sortingLayer   = sr != null ? sr.sortingLayerID : 0,
                    sortingOrder   = sr != null ? sr.sortingOrder : 0
                });
            }
        }
        protected override void OnSelectUp(LeanFinger finger)
        {
            // Stores the component we hit (Collider or Collider2D)
            var component = default(Component);

            switch (SelectUsing)
            {
            case SelectType.Raycast3D:
            {
                // Make sure the camera exists
                var camera = LeanHelper.GetCamera(Camera, gameObject);

                if (camera != null)
                {
                    var ray = camera.ScreenPointToRay(finger.ScreenPosition);
                    var hit = default(RaycastHit);

                    if (Physics.Raycast(ray, out hit, float.PositiveInfinity, LayerMask) == true)
                    {
                        component = hit.collider;
                    }
                }
                else
                {
                    Debug.LogError("Failed to find camera. Either tag your cameras MainCamera, or set one in this component.", this);
                }
            }
            break;

            case SelectType.Overlap2D:
            {
                // Make sure the camera exists
                var camera = LeanHelper.GetCamera(Camera, gameObject);

                if (camera != null)
                {
                    var ray   = camera.ScreenPointToRay(finger.ScreenPosition);
                    var count = Physics2D.GetRayIntersectionNonAlloc(ray, raycastHit2Ds, float.PositiveInfinity, LayerMask);

                    if (count > 0)
                    {
                        component = raycastHit2Ds[0].transform;
                    }
                }
                else
                {
                    Debug.LogError("Failed to find camera. Either tag your cameras MainCamera, or set one in this component.", this);
                }
            }
            break;

            case SelectType.CanvasUI:
            {
                var results = LeanTouch.RaycastGui(finger.ScreenPosition, LayerMask);

                if (results != null && results.Count > 0)
                {
                    component = results[0].gameObject.transform;
                }
            }
            break;
            }

            // Select the component
            Drop(finger, component);
        }
示例#24
0
 public bool Check(Ray ray)
 {
     Debug.DrawLine(ray.origin, ray.direction * 15, Color.red, 5);
     return(Physics2D.GetRayIntersectionNonAlloc(ray, _hit, 15, tileMask) > 0);
 }
示例#25
0
        private void TryGetComponent(SelectType selectUsing, Vector2 screenPosition, ref Component component)
        {
            switch (selectUsing)
            {
            case SelectType.Raycast3D:
            {
                // Make sure the camera exists
                var camera = LeanTouch.GetCamera(Camera, gameObject);

                if (camera != null)
                {
                    var ray   = camera.ScreenPointToRay(screenPosition);
                    var count = Physics.RaycastNonAlloc(ray, raycastHits, float.PositiveInfinity, LayerMask);

                    if (count > 0)
                    {
                        component = raycastHits[GetClosestRaycastHitsIndex(count)].transform;
                    }
                }
                else
                {
                    Debug.LogError("Failed to find camera. Either tag your cameras MainCamera, or set one in this component.", this);
                }
            }
            break;

            case SelectType.Overlap2D:
            {
                // Make sure the camera exists
                var camera = LeanTouch.GetCamera(Camera, gameObject);

                if (camera != null)
                {
                    var ray   = camera.ScreenPointToRay(screenPosition);
                    var count = Physics2D.GetRayIntersectionNonAlloc(ray, raycastHit2Ds, float.PositiveInfinity, LayerMask);

                    if (count > 0)
                    {
                        component = raycastHit2Ds[0].transform;
                    }
                }
                else
                {
                    Debug.LogError("Failed to find camera. Either tag your cameras MainCamera, or set one in this component.", this);
                }
            }
            break;

            case SelectType.CanvasUI:
            {
                var results = LeanTouch.RaycastGui(screenPosition, LayerMask);

                if (results != null && results.Count > 0)
                {
                    component = results[0].gameObject.transform;
                }
            }
            break;

            case SelectType.ScreenDistance:
            {
                var bestDistance = MaxScreenDistance * LeanTouch.ScalingFactor;

                bestDistance *= bestDistance;

                // Make sure the camera exists
                var camera = LeanTouch.GetCamera(Camera, gameObject);

                if (camera != null)
                {
                    foreach (var selectable in LeanSelectable.Instances)
                    {
                        var distance = Vector2.SqrMagnitude(GetScreenPoint(camera, selectable.transform) - screenPosition);

                        if (distance <= bestDistance)
                        {
                            bestDistance = distance;
                            component    = selectable;
                        }
                    }
                }
            }
            break;
            }
        }
    private void CalculateInputEvent_OnMobile()
    {
        p_listLastHit.Clear();
        _listTransform_EnterNew.Clear();
        _listTransform_ExitEnter.Clear();

        for (int j = 0; j < Input.touchCount; j++)
        {
            Touch pTouch   = Input.GetTouch(j);
            bool  bIsClick = pTouch.phase == TouchPhase.Ended;

            if (p_bIs2D)
            {
                _iLastHitCount = Physics2D.GetRayIntersectionNonAlloc(p_pEventCamera.ScreenPointToRay(Input.mousePosition), _arrHit_2D, Mathf.Infinity, p_pLayerMask_Hit.value);
            }
            else
            {
                _iLastHitCount = Physics.RaycastNonAlloc(p_pEventCamera.ScreenPointToRay(Input.mousePosition), _arrHit, Mathf.Infinity, p_pLayerMask_Hit.value);
            }

            for (int i = 0; i < _iLastHitCount; i++)
            {
                CRaycastHitWrapper pHit = _arrHit[i];
                p_listLastHit.Add(pHit);
                Transform pTransformHit = pHit.transform;

                if (CheckDebugFilter(EDebugFilter.Debug_Level_Core))
                {
                    Debug.Log(pTransformHit.name + " RayCast Hit bMouseClick: " + bIsClick, pTransformHit);
                }

                if (bIsClick)
                {
                    var pClick = pTransformHit.GetComponent <IPointerClickHandler>();
                    if (pClick != null)
                    {
                        pClick.OnPointerClick(null);
                    }
                }

                _listTransform_EnterNew.Add(pHit.transform);
            }

            for (int i = 0; i < _listTransform_EnterAlready.Count; i++)
            {
                Transform pTransform = _listTransform_EnterAlready[i];
                if (_listTransform_EnterNew.Contains(pTransform))
                {
                    _listTransform_EnterNew.Remove(pTransform);
                }
                else
                {
                    _listTransform_ExitEnter.Add(pTransform);
                }
            }

            for (int i = 0; i < _listTransform_EnterNew.Count; i++)
            {
                var pEnter = _listTransform_EnterNew[i].GetComponent <IPointerEnterHandler>();
                if (pEnter != null)
                {
                    pEnter.OnPointerEnter(null);
                }
            }

            for (int i = 0; i < _listTransform_ExitEnter.Count; i++)
            {
                Transform pTransform = _listTransform_ExitEnter[i];
                _listTransform_EnterAlready.Remove(pTransform);

                var pExit = pTransform.GetComponent <IPointerExitHandler>();
                if (pExit != null)
                {
                    pExit.OnPointerExit(null);
                }
            }

            _listTransform_EnterAlready.AddRange(_listTransform_EnterNew);
        }
    }
示例#27
0
    private void CalculateInputEvent_OnPC()
    {
        bool bIsClick = Input.GetMouseButtonUp(0);

        if (p_bIs2D)
        {
            _iLastHitCount = Physics2D.GetRayIntersectionNonAlloc(p_pEventCamera.ScreenPointToRay(Input.mousePosition), _arrHit_2D, Mathf.Infinity, p_pLayerMask_Hit.value);
        }
        else
        {
            _iLastHitCount = Physics.RaycastNonAlloc(p_pEventCamera.ScreenPointToRay(Input.mousePosition), _arrHit, Mathf.Infinity, p_pLayerMask_Hit.value);
        }

        if (p_bIsPrintDebug && bIsClick)
        {
            pRay_OnClick_ForDebug = p_pEventCamera.ScreenPointToRay(Input.mousePosition);
        }

        p_listLastHit.Clear();
        _listTransform_EnterNew.Clear();
        _listTransform_ExitEnter.Clear();
        for (int i = 0; i < _iLastHitCount; i++)
        {
            CRaycastHitWrapper pHit = _arrHit[i];
            p_listLastHit.Add(pHit);
            Transform pTransformHit = pHit.transform;

            if (p_bIsPrintDebug)
            {
                Debug.Log(pTransformHit.name + " RayCast Hit bMouseClick: " + bIsClick, pTransformHit);
            }

            if (bIsClick)
            {
                var pClick = pTransformHit.GetComponent <IPointerClickHandler>();
                if (pClick != null)
                {
                    pClick.OnPointerClick(null);
                }
            }

            _listTransform_EnterNew.Add(pHit.transform);
        }

        for (int i = 0; i < _listTransform_EnterAlready.Count; i++)
        {
            Transform pTransform = _listTransform_EnterAlready[i];
            if (_listTransform_EnterNew.Contains(pTransform))
            {
                _listTransform_EnterNew.Remove(pTransform);
            }
            else
            {
                _listTransform_ExitEnter.Add(pTransform);
            }
        }

        for (int i = 0; i < _listTransform_EnterNew.Count; i++)
        {
            var pEnter = _listTransform_EnterNew[i].GetComponent <IPointerEnterHandler>();
            if (pEnter != null)
            {
                pEnter.OnPointerEnter(null);
            }
        }

        for (int i = 0; i < _listTransform_ExitEnter.Count; i++)
        {
            Transform pTransform = _listTransform_ExitEnter[i];
            _listTransform_EnterAlready.Remove(pTransform);

            var pExit = pTransform.GetComponent <IPointerExitHandler>();
            if (pExit != null)
            {
                pExit.OnPointerExit(null);
            }
        }

        _listTransform_EnterAlready.AddRange(_listTransform_EnterNew);
    }
示例#28
0
        protected void TryGetComponent(SelectType selectUsing, Vector2 screenPosition, ref Component component, ref Vector3 worldPosition)
        {
            switch (selectUsing)
            {
            case SelectType.Raycast3D:
            {
                // Make sure the camera exists
                var camera = LeanTouch.GetCamera(Camera, gameObject);

                if (camera != null && camera.pixelRect.Contains(screenPosition) == true)
                {
                    var ray   = camera.ScreenPointToRay(screenPosition);
                    var count = Physics.RaycastNonAlloc(ray, raycastHits, float.PositiveInfinity, LayerMask);

                    if (count > 0)
                    {
                        var closestHit = raycastHits[GetClosestRaycastHitsIndex(count)];

                        component     = closestHit.transform;
                        worldPosition = closestHit.point;
                    }
                }
                else
                {
                    Debug.LogError("Failed to find camera. Either tag your cameras MainCamera, or set one in this component.", this);
                }
            }
            break;

            case SelectType.Overlap2D:
            {
                // Make sure the camera exists
                var camera = LeanTouch.GetCamera(Camera, gameObject);

                if (camera != null && camera.pixelRect.Contains(screenPosition) == true)
                {
                    var ray   = camera.ScreenPointToRay(screenPosition);
                    var slope = -ray.direction.z;

                    if (slope != 0.0f)
                    {
                        var point = ray.GetPoint(ray.origin.z / slope);

                        component = Physics2D.OverlapPoint(point, LayerMask);

                        if (component != null)
                        {
                            worldPosition = component.transform.position;
                        }
                    }
                }
                else
                {
                    Debug.LogError("Failed to find camera. Either tag your cameras MainCamera, or set one in this component.", this);
                }
            }
            break;

            case SelectType.CanvasUI:
            {
                var results = LeanTouch.RaycastGui(screenPosition, LayerMask);

                if (results != null && results.Count > 0)
                {
                    var firstTransform = results[0].gameObject.transform;

                    component     = firstTransform;
                    worldPosition = firstTransform.position;
                }
            }
            break;

            case SelectType.ScreenDistance:
            {
                var bestDistance = MaxScreenDistance * LeanTouch.ScalingFactor;

                bestDistance *= bestDistance;

                // Make sure the camera exists
                var camera = LeanTouch.GetCamera(Camera, gameObject);

                if (camera != null && camera.pixelRect.Contains(screenPosition) == true)
                {
                    foreach (var selectable in LeanSelectable.Instances)
                    {
                        var distance = Vector2.SqrMagnitude(GetScreenPoint(camera, selectable.transform) - screenPosition);

                        if (distance <= bestDistance)
                        {
                            bestDistance  = distance;
                            component     = selectable;
                            worldPosition = selectable.transform.position;
                        }
                    }
                }
            }
            break;

            case SelectType.Intersect2D:
            {
                // Make sure the camera exists
                var camera = LeanTouch.GetCamera(Camera, gameObject);

                if (camera != null && camera.pixelRect.Contains(screenPosition) == true)
                {
                    var ray   = camera.ScreenPointToRay(screenPosition);
                    var count = Physics2D.GetRayIntersectionNonAlloc(ray, raycastHit2Ds, float.PositiveInfinity, LayerMask);

                    if (count > 0)
                    {
                        var firstHit = raycastHit2Ds[0];

                        component     = firstHit.transform;
                        worldPosition = firstHit.point;
                    }
                }
                else
                {
                    Debug.LogError("Failed to find camera. Either tag your cameras MainCamera, or set one in this component.", this);
                }
            }
            break;
            }
        }
示例#29
0
        private HitResult performWorldSearch(IPointer pointer, out HitData hit)
        {
            hit = default(HitData);

            if (_camera == null)
            {
                return(HitResult.Miss);
            }
            if ((_camera.enabled == false) || (_camera.gameObject.activeInHierarchy == false))
            {
                return(HitResult.Miss);
            }
            var position = pointer.Position;

            if (!_camera.pixelRect.Contains(position))
            {
                return(HitResult.Miss);
            }

            hitList.Clear();
            var ray = _camera.ScreenPointToRay(position);

            int  count;
            bool exclusiveSet = manager.HasExclusive;

            if (hit3DObjects)
            {
#if UNITY_5_3_OR_NEWER
                count = Physics.RaycastNonAlloc(ray, raycastHits, float.PositiveInfinity, layerMask);
#else
                var raycastHits = Physics.RaycastAll(ray, float.PositiveInfinity, layerMask);
                var count       = raycastHits.Length;
#endif

                // Try to do some optimizations if 2D and WS UI are not required
                if (!hit2DObjects && !hitWorldSpaceUI)
                {
                    RaycastHit raycast;

                    if (count == 0)
                    {
                        return(HitResult.Miss);
                    }
                    if (count > 1)
                    {
                        raycastHitList.Clear();
                        for (var i = 0; i < count; i++)
                        {
                            raycast = raycastHits[i];
                            if (exclusiveSet && !manager.IsExclusive(raycast.transform))
                            {
                                continue;
                            }
                            raycastHitList.Add(raycast);
                        }
                        if (raycastHitList.Count == 0)
                        {
                            return(HitResult.Miss);
                        }

                        raycastHitList.Sort(_raycastHitComparerFunc);
                        if (useHitFilters)
                        {
                            for (var i = 0; i < count; i++)
                            {
                                var result = doHit(pointer, raycastHitList[i], out hit);
                                if (result != HitResult.Miss)
                                {
                                    return(result);
                                }
                            }
                            return(HitResult.Miss);
                        }
                        hit = new HitData(raycastHitList[0], this);
                        return(HitResult.Hit);
                    }

                    raycast = raycastHits[0];
                    if (exclusiveSet && !manager.IsExclusive(raycast.transform))
                    {
                        return(HitResult.Miss);
                    }
                    if (useHitFilters)
                    {
                        return(doHit(pointer, raycast, out hit));
                    }
                    hit = new HitData(raycast, this);
                    return(HitResult.Hit);
                }
                for (var i = 0; i < count; i++)
                {
                    var raycast = raycastHits[i];
                    if (exclusiveSet && !manager.IsExclusive(raycast.transform))
                    {
                        continue;
                    }
                    hitList.Add(new HitData(raycastHits[i], this));
                }
            }

            if (hit2DObjects)
            {
                count = Physics2D.GetRayIntersectionNonAlloc(ray, raycastHits2D, float.MaxValue, layerMask);
                for (var i = 0; i < count; i++)
                {
                    var raycast = raycastHits2D[i];
                    if (exclusiveSet && !manager.IsExclusive(raycast.transform))
                    {
                        continue;
                    }
                    hitList.Add(new HitData(raycast, this));
                }
            }

            if (hitWorldSpaceUI)
            {
                raycastHitUIList.Clear();
                if (raycasters == null)
                {
                    raycasters = TouchScriptInputModule.Instance.GetRaycasters();
                }
                count = raycasters.Count;

                for (var i = 0; i < count; i++)
                {
                    var raycaster = raycasters[i] as GraphicRaycaster;
                    if (raycaster == null)
                    {
                        continue;
                    }
                    var canvas = TouchScriptInputModule.Instance.GetCanvasForRaycaster(raycaster);
                    if ((canvas == null) || (canvas.renderMode == RenderMode.ScreenSpaceOverlay) || (canvas.worldCamera != _camera))
                    {
                        continue;
                    }
                    performUISearchForCanvas(pointer, canvas, raycaster, _camera, float.MaxValue, ray);
                }

                count = raycastHitUIList.Count;
                for (var i = 0; i < count; i++)
                {
                    hitList.Add(new HitData(raycastHitUIList[i], this));
                }
            }

            count = hitList.Count;
            if (hitList.Count == 0)
            {
                return(HitResult.Miss);
            }
            if (count > 1)
            {
                hitList.Sort(_hitDataComparerFunc);
                if (useHitFilters)
                {
                    for (var i = 0; i < count; ++i)
                    {
                        hit = hitList[i];
                        var result = checkHitFilters(pointer, hit);
                        if (result != HitResult.Miss)
                        {
                            return(result);
                        }
                    }
                    return(HitResult.Miss);
                }
                else
                {
                    hit = hitList[0];
                    return(HitResult.Hit);
                }
            }
            hit = hitList[0];
            if (useHitFilters)
            {
                return(checkHitFilters(pointer, hit));
            }
            return(HitResult.Hit);
        }