UnityEditor.HandleUtility.GUIPointToWorldRay C# (CSharp) Method

GUIPointToWorldRay() public static method

Convert 2D GUI position to a world space ray.

public static GUIPointToWorldRay ( Vector2 position ) : Ray
position Vector2
return UnityEngine.Ray
        public static Ray GUIPointToWorldRay(Vector2 position)
        {
            if (Camera.current == null)
            {
                Debug.LogError("Unable to convert GUI point to world ray if a camera has not been set up!");
                return new Ray(Vector3.zero, Vector3.forward);
            }
            Vector2 vector2 = EditorGUIUtility.PointsToPixels(GUIClip.Unclip(position));
            vector2.y = Screen.height - vector2.y;
            return Camera.current.ScreenPointToRay((Vector3) new Vector2(vector2.x, vector2.y));
        }

Usage Example

        public void OnSceneDrag(SceneView sceneView)
        {
            GameObject go = target as GameObject;

            if (!PrefabUtility.IsPartOfPrefabAsset(go))
            {
                return;
            }

            var prefabAssetRoot = go.transform.root.gameObject;

            Event evt = Event.current;

            switch (evt.type)
            {
            case EventType.DragUpdated:

                Scene destinationScene = sceneView.customScene.IsValid() ? sceneView.customScene : SceneManager.GetActiveScene();
                if (dragObject == null)
                {
                    // While dragging the instantiated prefab we do not want to record undo for this object
                    // this will cause a remerge of the instance since changes are undone while dragging.
                    // The DrivenRectTransformTracker by default records Undo when used when driving
                    // UI components. This breaks our hideflag setup below due to a remerge of the dragged instance.
                    // StartRecordingUndo() is called on DragExited. Fixes case 1223793.
                    DrivenRectTransformTracker.StopRecordingUndo();

                    if (!EditorApplication.isPlaying || EditorSceneManager.IsPreviewScene(destinationScene))
                    {
                        dragObject      = (GameObject)PrefabUtility.InstantiatePrefab(prefabAssetRoot, destinationScene);
                        dragObject.name = go.name;
                    }
                    else
                    {
                        // Instatiate as regular GameObject in Play Mode so runtime logic
                        // won't run into restrictions on restructuring Prefab instances.
                        dragObject = Instantiate(prefabAssetRoot);
                        SceneManager.MoveGameObjectToScene(dragObject, destinationScene);
                    }
                    dragObject.hideFlags = HideFlags.HideInHierarchy;
                }

                if (HandleUtility.ignoreRaySnapObjects == null)
                {
                    HandleUtility.ignoreRaySnapObjects = dragObject.GetComponentsInChildren <Transform>();
                }

                DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                Vector3 point, normal;

                if (HandleUtility.PlaceObject(evt.mousePosition, out point, out normal))
                {
                    float offset = 0;
                    if (Tools.pivotMode == PivotMode.Center)
                    {
                        float geomOffset = HandleUtility.CalcRayPlaceOffset(HandleUtility.ignoreRaySnapObjects, normal);
                        if (geomOffset != Mathf.Infinity)
                        {
                            offset = Vector3.Dot(dragObject.transform.position, normal) - geomOffset;
                        }
                    }
                    dragObject.transform.position = Matrix4x4.identity.MultiplyPoint(point + (normal * offset));
                }
                else
                {
                    dragObject.transform.position = HandleUtility.GUIPointToWorldRay(evt.mousePosition).GetPoint(10);
                }

                // Use prefabs original z position when in 2D mode
                if (sceneView.in2DMode)
                {
                    Vector3 dragPosition = dragObject.transform.position;
                    dragPosition.z = prefabAssetRoot.transform.position.z;
                    dragObject.transform.position = dragPosition;
                }

                evt.Use();
                break;

            case EventType.DragPerform:
                if (!DragPerform(sceneView, dragObject, go))
                {
                    return;
                }
                dragObject = null;
                evt.Use();
                break;

            case EventType.DragExited:
                // DragExited is always fired after DragPerform so we do no need to call StartRecordingUndo
                // in DragPerform
                DrivenRectTransformTracker.StartRecordingUndo();

                if (dragObject)
                {
                    UnityObject.DestroyImmediate(dragObject, false);
                    HandleUtility.ignoreRaySnapObjects = null;
                    dragObject = null;
                    evt.Use();
                }
                break;
            }
        }
All Usage Examples Of UnityEditor.HandleUtility::GUIPointToWorldRay