コード例 #1
0
        public List <GameObjectRayHit> RaycastAllMesh(Ray ray)
        {
            if (!RuntimeEditorApplication.Instance.UseUnityColliders)
            {
                return(_gameObjectSphereTree.RaycastAllMesh(ray));
            }
            else
            {
                RaycastHit[] hits           = Physics.RaycastAll(ray);
                var          gameObjectHits = new List <GameObjectRayHit>();
                foreach (var hit in hits)
                {
                    // Retrieve the object which was hit
                    GameObject gameObject = hit.collider.gameObject;
                    if (gameObject == null)
                    {
                        continue;
                    }
                    if (!gameObject.activeSelf)
                    {
                        continue;
                    }

                    GameObjectRayHit gameObjectRayHit = null;
                    if (gameObject.RaycastMesh(ray, out gameObjectRayHit))
                    {
                        gameObjectHits.Add(gameObjectRayHit);
                    }
                }

                return(gameObjectHits);
            }
        }
コード例 #2
0
        public static bool RaycastBox(this GameObject gameObject, Ray ray, out GameObjectRayHit objectRayHit)
        {
            objectRayHit = null;
            OrientedBox objectWorldOrientedBox = gameObject.GetWorldOrientedBox();

            OrientedBoxRayHit objectBoxRayHit;

            if (objectWorldOrientedBox.Raycast(ray, out objectBoxRayHit))
            {
                objectRayHit = new GameObjectRayHit(ray, gameObject, objectBoxRayHit, null, null, null);
            }

            return(objectRayHit != null);
        }
コード例 #3
0
        public static bool RaycastMesh(this GameObject gameObject, Ray ray, out GameObjectRayHit objectRayHit)
        {
            objectRayHit = null;
            Mesh objectMesh = gameObject.GetMeshFromFilterOrSkinnedMeshRenderer();

            if (objectMesh == null)
            {
                return(false);
            }

            EditorMesh editorMesh = EditorMeshDatabase.Instance.GetEditorMesh(objectMesh);

            if (editorMesh != null)
            {
                MeshRayHit meshRayHit = editorMesh.Raycast(ray, gameObject.transform.GetWorldMatrix());
                if (meshRayHit == null)
                {
                    return(false);
                }

                objectRayHit = new GameObjectRayHit(ray, gameObject, null, meshRayHit, null, null);
                return(true);
            }
            else
            {
                MeshCollider meshCollider = gameObject.GetComponent <MeshCollider>();
                if (meshCollider != null)
                {
                    RaycastHit rayHit;
                    if (meshCollider.Raycast(ray, out rayHit, float.MaxValue))
                    {
                        MeshRayHit meshRayHit = new MeshRayHit(ray, rayHit.distance, rayHit.triangleIndex, rayHit.point, rayHit.normal);
                        objectRayHit = new GameObjectRayHit(ray, gameObject, null, meshRayHit, null, null);
                        return(true);
                    }
                }
                else
                {
                    return(gameObject.RaycastBox(ray, out objectRayHit));
                }
            }

            return(false);
        }
コード例 #4
0
        /// <summary>
        /// Performs a raycast and returns a list of hits for all sprite objects intersected
        /// by the ray.
        /// </summary>
        public List <GameObjectRayHit> RaycastAllSprite(Ray ray)
        {
            // First, retrieve a list of the sphere tree nodes which were hit by the ray.
            // If no nodes were hit, it means no object was hit either.
            List <SphereTreeNodeRayHit <GameObject> > allNodeHits = _sphereTree.RaycastAll(ray);

            if (allNodeHits.Count == 0)
            {
                return(new List <GameObjectRayHit>());
            }

            // Loop through all nodes which were hit by the ray. For each node, we have to detect
            // if the ray hits the sprite object.
            var gameObjectHits = new List <GameObjectRayHit>();

            foreach (SphereTreeNodeRayHit <GameObject> nodeHit in allNodeHits)
            {
                // Retrieve the object which resides in the node
                GameObject gameObject = nodeHit.HitNode.Data;
                if (gameObject == null)
                {
                    continue;
                }
                if (!gameObject.HasSpriteRendererWithSprite())
                {
                    continue;
                }

                // If the ray intersects the object's sprite, add the hit to the list
                GameObjectRayHit gameObjectRayHit = null;
                if (gameObject.RaycastSprite(ray, out gameObjectRayHit))
                {
                    gameObjectHits.Add(gameObjectRayHit);
                }
            }

            return(gameObjectHits);
        }
コード例 #5
0
        public static bool RaycastSprite(this GameObject gameObject, Ray ray, out GameObjectRayHit objectRayHit)
        {
            objectRayHit = null;

            SpriteRenderer spriteRenderer = gameObject.GetComponent <SpriteRenderer>();

            if (spriteRenderer == null)
            {
                return(false);
            }

            OrientedBox objectWorldOrientedBox = gameObject.GetWorldOrientedBox();

            OrientedBoxRayHit objectBoxRayHit;

            if (objectWorldOrientedBox.Raycast(ray, out objectBoxRayHit))
            {
                SpriteRayHit spriteHit = new SpriteRayHit(ray, objectBoxRayHit.HitEnter, spriteRenderer, objectBoxRayHit.HitPoint, objectBoxRayHit.HitNormal);
                objectRayHit = new GameObjectRayHit(ray, gameObject, null, null, null, spriteHit);
            }

            return(objectRayHit != null);
        }
コード例 #6
0
        /// <summary>
        /// Performs a raycast and returns a list of hits for all objects whose meshes
        /// are intersected by the specified ray.
        /// </summary>
        public List <GameObjectRayHit> RaycastAllMesh(Ray ray)
        {
            // First, we will gather the objects whos boxes are intersected by the ray. If
            // no such objects exist, we will return an empty list.
            List <GameObjectRayHit> allBoxHits = RaycastAllBox(ray);

            if (allBoxHits.Count == 0)
            {
                return(new List <GameObjectRayHit>());
            }

            // Now we will loop through all these objects and identify the ones whose meshes
            // are hit by the ray.
            var allMeshObjectHits = new List <GameObjectRayHit>(allBoxHits.Count);

            foreach (var boxHit in allBoxHits)
            {
                // Store the object for easy access
                GameObject hitObject = boxHit.HitObject;
                if (hitObject == null)
                {
                    continue;
                }
                if (!hitObject.activeSelf)
                {
                    continue;
                }

                GameObjectRayHit gameObjectRayHit = null;
                if (hitObject.RaycastMesh(ray, out gameObjectRayHit))
                {
                    allMeshObjectHits.Add(gameObjectRayHit);
                }
            }

            return(allMeshObjectHits);
        }