PickGameObject() static private method

static private PickGameObject ( Vector2 position, GameObject ignore, GameObject filter, int &materialIndex ) : GameObject
position Vector2
ignore UnityEngine.GameObject
filter UnityEngine.GameObject
materialIndex int
return UnityEngine.GameObject
Beispiel #1
0
        private static void PickAllHandlesNonAlloc(HashSet <GameObject> results, Vector2 position, int limit = DefaultLimit)
        {
            if (!canPickHandles)
            {
                // HandleUtility.PickGameObject is not supported in those contexts
                Debug.LogWarning($"Cannot pick game objects in the current event: {e?.ToString() ?? "null"}");
                return;
            }

            GameObject result = null;

            var count = 0;

            do
            {
                var ignored = results.ToArray();

                result = HandleUtility.PickGameObject(position, false, ignored);

                // Ignored doesn't seem very reliable. Sometimes, an item included
                // in ignored will still be returned. That's a sign we should stop.
                if (results.Contains(result))
                {
                    result = null;
                }

                if (result != null)
                {
                    results.Add(result);
                }
            } while (result != null && count++ < limit);
        }
        public void OnGUI()
        {
            Event evt = Event.current;

            Handles.BeginGUI();

            Vector2 mousePos = evt.mousePosition;
            int     id       = s_RectSelectionID;

            switch (evt.GetTypeForControl(id))
            {
            case EventType.Layout:
            case EventType.MouseMove:
                if (!Tools.viewToolActive)
                {
                    HandleUtility.AddDefaultControl(id);
                }
                break;

            case EventType.MouseDown:
                if (HandleUtility.nearestControl == id && evt.button == 0)
                {
                    GUIUtility.hotControl = id;
                    m_SelectStartPoint    = mousePos;
                    m_SelectionStart      = Selection.objects;
                    m_RectSelecting       = false;
                }
                break;

            case EventType.MouseDrag:
                if (GUIUtility.hotControl == id)
                {
                    if (!m_RectSelecting && (mousePos - m_SelectStartPoint).magnitude > 6f)
                    {
                        EditorApplication.modifierKeysChanged += SendCommandsOnModifierKeys;
                        m_RectSelecting = true;
                        ActiveEditorTracker.delayFlushDirtyRebuild = true;
                        m_LastSelection    = null;
                        m_CurrentSelection = null;
                        rectSelectionStarting();
                    }
                    if (m_RectSelecting)
                    {
                        m_SelectMousePoint = new Vector2(Mathf.Max(mousePos.x, 0), Mathf.Max(mousePos.y, 0));
                        GameObject[] rectObjs = HandleUtility.PickRectObjects(EditorGUIExt.FromToRect(m_SelectStartPoint, m_SelectMousePoint));
                        m_CurrentSelection = rectObjs;
                        bool setIt = false;
                        if (m_LastSelection == null)
                        {
                            m_LastSelection = new Dictionary <GameObject, bool>();
                            setIt           = true;
                        }
                        setIt |= m_LastSelection.Count != rectObjs.Length;
                        if (!setIt)
                        {
                            Dictionary <GameObject, bool> set = new Dictionary <GameObject, bool>(rectObjs.Length);
                            foreach (GameObject g in rectObjs)
                            {
                                set.Add(g, false);
                            }
                            foreach (GameObject g in m_LastSelection.Keys)
                            {
                                if (!set.ContainsKey(g))
                                {
                                    setIt = true;
                                    break;
                                }
                            }
                        }
                        if (setIt)
                        {
                            m_LastSelection = new Dictionary <GameObject, bool>(rectObjs.Length);
                            foreach (GameObject g in rectObjs)
                            {
                                m_LastSelection.Add(g, false);
                            }
                            if (evt.shift)
                            {
                                UpdateSelection(m_SelectionStart, rectObjs, SelectionType.Additive, m_RectSelecting);
                            }
                            else if (EditorGUI.actionKey)
                            {
                                UpdateSelection(m_SelectionStart, rectObjs, SelectionType.Subtractive, m_RectSelecting);
                            }
                            else
                            {
                                UpdateSelection(m_SelectionStart, rectObjs, SelectionType.Normal, m_RectSelecting);
                            }
                        }
                    }
                    evt.Use();
                }
                break;

            case EventType.Repaint:
                if (GUIUtility.hotControl == id && m_RectSelecting)
                {
                    EditorStyles.selectionRect.Draw(EditorGUIExt.FromToRect(m_SelectStartPoint, m_SelectMousePoint), GUIContent.none, false, false, false, false);
                }
                break;

            case EventType.MouseUp:
                if (GUIUtility.hotControl == id && evt.button == 0)
                {
                    GUIUtility.hotControl = 0;
                    if (m_RectSelecting)
                    {
                        EditorApplication.modifierKeysChanged -= SendCommandsOnModifierKeys;
                        m_RectSelecting = false;
                        ActiveEditorTracker.delayFlushDirtyRebuild = false;
                        ActiveEditorTracker.RebuildAllIfNecessary();
                        m_SelectionStart = new Object[0];
                        rectSelectionFinished();
                        evt.Use();
                    }
                    else
                    {
                        if (evt.shift || EditorGUI.actionKey)
                        {
                            // For shift, we check if EXACTLY the active GO is hovered by mouse and then subtract. Otherwise additive.
                            // For control/cmd, we check if ANY of the selected GO is hovered by mouse and then subtract. Otherwise additive.
                            // Control/cmd takes priority over shift.
                            GameObject hovered = HandleUtility.PickGameObject(evt.mousePosition, false);
                            if (EditorGUI.actionKey ? Selection.gameObjects.Contains(hovered) : Selection.activeGameObject == hovered)
                            {
                                UpdateSelection(m_SelectionStart, hovered, SelectionType.Subtractive, m_RectSelecting);
                            }
                            else
                            {
                                UpdateSelection(m_SelectionStart, HandleUtility.PickGameObject(evt.mousePosition, true), SelectionType.Additive, m_RectSelecting);
                            }
                        }
                        else     // With no modifier keys, we do the "cycle through overlapped" picking logic in SceneViewPicking.cs
                        {
                            GameObject picked = SceneViewPicking.PickGameObject(evt.mousePosition);
                            UpdateSelection(m_SelectionStart, picked, SelectionType.Normal, m_RectSelecting);
                        }

                        evt.Use();
                    }
                }
                break;

            case EventType.ExecuteCommand:
                if (id == GUIUtility.hotControl && evt.commandName == EventCommandNames.ModifierKeysChanged)
                {
                    if (evt.shift)
                    {
                        UpdateSelection(m_SelectionStart, m_CurrentSelection, SelectionType.Additive, m_RectSelecting);
                    }
                    else if (EditorGUI.actionKey)
                    {
                        UpdateSelection(m_SelectionStart, m_CurrentSelection, SelectionType.Subtractive, m_RectSelecting);
                    }
                    else
                    {
                        UpdateSelection(m_SelectionStart, m_CurrentSelection, SelectionType.Normal, m_RectSelecting);
                    }
                    evt.Use();
                }
                break;
            }

            Handles.EndGUI();
        }
        public void OnGUI()
        {
            Event evt = Event.current;

            Handles.BeginGUI();

            Vector2 mousePos = evt.mousePosition;
            int     id       = s_RectSelectionID;

            switch (evt.GetTypeForControl(id))
            {
            case EventType.Layout:
            case EventType.MouseMove:
                if (!Tools.viewToolActive)
                {
                    HandleUtility.AddDefaultControl(id);
                }

                //Handle the case of the drag being canceled
                if (m_RectSelecting && GUIUtility.hotControl != id)
                {
                    CompleteRectSelection();
                }
                break;

            case EventType.MouseDown:
                if (HandleUtility.nearestControl == id && evt.button == 0)
                {
                    GUIUtility.hotControl = id;
                    m_SelectStartPoint    = mousePos;
                    m_SelectionStart      = Selection.objects;
                    m_RectSelecting       = false;
                }
                break;

            case EventType.MouseDrag:
                if (GUIUtility.hotControl == id)
                {
                    if (!m_RectSelecting && (mousePos - m_SelectStartPoint).magnitude > 6f)
                    {
                        EditorApplication.modifierKeysChanged += SendCommandsOnModifierKeys;
                        m_RectSelecting = true;
                        ActiveEditorTracker.delayFlushDirtyRebuild = true;
                        m_LastSelection    = null;
                        m_CurrentSelection = null;
                        rectSelectionStarting();
                    }
                    if (m_RectSelecting)
                    {
                        m_SelectMousePoint = new Vector2(Mathf.Max(mousePos.x, 0), Mathf.Max(mousePos.y, 0));
                        GameObject[] rectObjs = HandleUtility.PickRectObjects(EditorGUIExt.FromToRect(m_SelectStartPoint, m_SelectMousePoint));
                        m_CurrentSelection = rectObjs;
                        bool setIt = false;
                        if (m_LastSelection == null)
                        {
                            m_LastSelection = new Dictionary <GameObject, bool>();
                            setIt           = true;
                        }
                        setIt |= m_LastSelection.Count != rectObjs.Length;
                        if (!setIt)
                        {
                            Dictionary <GameObject, bool> set = new Dictionary <GameObject, bool>(rectObjs.Length);
                            foreach (GameObject g in rectObjs)
                            {
                                set.Add(g, false);
                            }
                            foreach (GameObject g in m_LastSelection.Keys)
                            {
                                if (!set.ContainsKey(g))
                                {
                                    setIt = true;
                                    break;
                                }
                            }
                        }
                        if (setIt)
                        {
                            m_LastSelection = new Dictionary <GameObject, bool>(rectObjs.Length);
                            foreach (GameObject g in rectObjs)
                            {
                                m_LastSelection.Add(g, false);
                            }
                            if (evt.shift)
                            {
                                UpdateSelection(m_SelectionStart, rectObjs, SelectionType.Additive, m_RectSelecting);
                            }
                            else if (EditorGUI.actionKey)
                            {
                                UpdateSelection(m_SelectionStart, rectObjs, SelectionType.Subtractive, m_RectSelecting);
                            }
                            else
                            {
                                UpdateSelection(m_SelectionStart, rectObjs, SelectionType.Normal, m_RectSelecting);
                            }
                        }
                    }
                    evt.Use();
                }
                break;

            case EventType.Repaint:
                if (GUIUtility.hotControl == id && m_RectSelecting)
                {
                    EditorStyles.selectionRect.Draw(EditorGUIExt.FromToRect(m_SelectStartPoint, m_SelectMousePoint), GUIContent.none, false, false, false, false);
                }
                break;

            case EventType.MouseUp:
                if (GUIUtility.hotControl == id && evt.button == 0)
                {
                    GUIUtility.hotControl = 0;
                    if (m_RectSelecting)
                    {
                        CompleteRectSelection();
                        evt.Use();
                    }
                    else
                    {
                        if (evt.shift || EditorGUI.actionKey)
                        {
                            // For shift, we check if EXACTLY the active GO is hovered by mouse and then subtract. Otherwise additive.
                            // For control/cmd, we check if ANY of the selected GO is hovered by mouse and then subtract. Otherwise additive.
                            // Control/cmd takes priority over shift.
                            GameObject hovered = HandleUtility.PickGameObject(evt.mousePosition, false);

                            var handledIt = false;
                            // shift-click deselects only if the active GO is exactly what we clicked on
                            if (!EditorGUI.actionKey && Selection.activeGameObject == hovered)
                            {
                                UpdateSelection(m_SelectionStart, hovered, SelectionType.Subtractive, m_RectSelecting);
                                handledIt = true;
                            }

                            // ctrl-click deselects everything up to prefab root, that is already selected
                            if (!handledIt && EditorGUI.actionKey)
                            {
                                var selectedGos  = Selection.gameObjects;
                                var hoveredRoot  = HandleUtility.FindSelectionBaseForPicking(hovered);
                                var deselectList = new List <Object>();
                                while (hovered != null)
                                {
                                    if (selectedGos.Contains(hovered))
                                    {
                                        deselectList.Add(hovered);
                                    }
                                    if (hovered == hoveredRoot)
                                    {
                                        break;
                                    }
                                    var parent = hovered.transform.parent;
                                    if (parent)
                                    {
                                        hovered = parent.gameObject;
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                if (deselectList.Any())
                                {
                                    UpdateSelection(m_SelectionStart, deselectList.ToArray(), SelectionType.Subtractive, m_RectSelecting);
                                    handledIt = true;
                                }
                            }

                            // we did not deselect anything, so add the new thing into selection instead
                            if (!handledIt)
                            {
                                UpdateSelection(m_SelectionStart, HandleUtility.PickGameObject(evt.mousePosition, true), SelectionType.Additive, m_RectSelecting);
                            }
                        }
                        else     // With no modifier keys, we do the "cycle through overlapped" picking logic in SceneViewPicking.cs
                        {
                            GameObject picked = SceneViewPicking.PickGameObject(evt.mousePosition);
                            UpdateSelection(m_SelectionStart, picked, SelectionType.Normal, m_RectSelecting);
                        }

                        evt.Use();
                    }
                }
                break;

            case EventType.ExecuteCommand:
                if (id == GUIUtility.hotControl && evt.commandName == EventCommandNames.ModifierKeysChanged)
                {
                    if (evt.shift)
                    {
                        UpdateSelection(m_SelectionStart, m_CurrentSelection, SelectionType.Additive, m_RectSelecting);
                    }
                    else if (EditorGUI.actionKey)
                    {
                        UpdateSelection(m_SelectionStart, m_CurrentSelection, SelectionType.Subtractive, m_RectSelecting);
                    }
                    else
                    {
                        UpdateSelection(m_SelectionStart, m_CurrentSelection, SelectionType.Normal, m_RectSelecting);
                    }
                    evt.Use();
                }
                break;
            }

            Handles.EndGUI();
        }
        public void OnGUI()
        {
            Event current = Event.current;

            Handles.BeginGUI();
            Vector2   mousePosition  = current.mousePosition;
            int       num            = RectSelection.s_RectSelectionID;
            EventType typeForControl = current.GetTypeForControl(num);

            switch (typeForControl)
            {
            case EventType.MouseDown:
                if (HandleUtility.nearestControl == num && current.button == 0)
                {
                    GUIUtility.hotControl   = num;
                    this.m_SelectStartPoint = mousePosition;
                    this.m_SelectionStart   = Selection.objects;
                    this.m_RectSelecting    = false;
                }
                goto IL_4F7;

            case EventType.MouseUp:
                if (GUIUtility.hotControl == num && current.button == 0)
                {
                    GUIUtility.hotControl = 0;
                    if (this.m_RectSelecting)
                    {
                        EditorApplication.modifierKeysChanged = (EditorApplication.CallbackFunction)Delegate.Remove(EditorApplication.modifierKeysChanged, new EditorApplication.CallbackFunction(this.SendCommandsOnModifierKeys));
                        this.m_RectSelecting  = false;
                        this.m_SelectionStart = new UnityEngine.Object[0];
                        current.Use();
                    }
                    else
                    {
                        if (current.shift || EditorGUI.actionKey)
                        {
                            GameObject gameObject = HandleUtility.PickGameObject(current.mousePosition, false);
                            if ((!EditorGUI.actionKey) ? (Selection.activeGameObject == gameObject) : Selection.gameObjects.Contains(gameObject))
                            {
                                RectSelection.UpdateSelection(this.m_SelectionStart, gameObject, RectSelection.SelectionType.Subtractive, this.m_RectSelecting);
                            }
                            else
                            {
                                RectSelection.UpdateSelection(this.m_SelectionStart, HandleUtility.PickGameObject(current.mousePosition, true), RectSelection.SelectionType.Additive, this.m_RectSelecting);
                            }
                        }
                        else
                        {
                            GameObject newObject = SceneViewPicking.PickGameObject(current.mousePosition);
                            RectSelection.UpdateSelection(this.m_SelectionStart, newObject, RectSelection.SelectionType.Normal, this.m_RectSelecting);
                        }
                        current.Use();
                    }
                }
                goto IL_4F7;

            case EventType.MouseMove:
            case EventType.KeyDown:
            case EventType.KeyUp:
            case EventType.ScrollWheel:
IL_4B:
                if (typeForControl != EventType.ExecuteCommand)
                {
                    goto IL_4F7;
                }
                if (num == GUIUtility.hotControl && current.commandName == "ModifierKeysChanged")
                {
                    if (current.shift)
                    {
                        RectSelection.UpdateSelection(this.m_SelectionStart, this.m_CurrentSelection, RectSelection.SelectionType.Additive, this.m_RectSelecting);
                    }
                    else if (EditorGUI.actionKey)
                    {
                        RectSelection.UpdateSelection(this.m_SelectionStart, this.m_CurrentSelection, RectSelection.SelectionType.Subtractive, this.m_RectSelecting);
                    }
                    else
                    {
                        RectSelection.UpdateSelection(this.m_SelectionStart, this.m_CurrentSelection, RectSelection.SelectionType.Normal, this.m_RectSelecting);
                    }
                    current.Use();
                }
                goto IL_4F7;

            case EventType.MouseDrag:
                if (GUIUtility.hotControl == num)
                {
                    if (!this.m_RectSelecting && (mousePosition - this.m_SelectStartPoint).magnitude > 6f)
                    {
                        EditorApplication.modifierKeysChanged = (EditorApplication.CallbackFunction)Delegate.Combine(EditorApplication.modifierKeysChanged, new EditorApplication.CallbackFunction(this.SendCommandsOnModifierKeys));
                        this.m_RectSelecting    = true;
                        this.m_LastSelection    = null;
                        this.m_CurrentSelection = null;
                    }
                    if (this.m_RectSelecting)
                    {
                        this.m_SelectMousePoint = new Vector2(Mathf.Max(mousePosition.x, 0f), Mathf.Max(mousePosition.y, 0f));
                        GameObject[] array = HandleUtility.PickRectObjects(EditorGUIExt.FromToRect(this.m_SelectStartPoint, this.m_SelectMousePoint));
                        this.m_CurrentSelection = array;
                        bool flag = false;
                        if (this.m_LastSelection == null)
                        {
                            this.m_LastSelection = new Dictionary <GameObject, bool>();
                            flag = true;
                        }
                        flag |= (this.m_LastSelection.Count != array.Length);
                        if (!flag)
                        {
                            Dictionary <GameObject, bool> dictionary = new Dictionary <GameObject, bool>(array.Length);
                            GameObject[] array2 = array;
                            for (int i = 0; i < array2.Length; i++)
                            {
                                GameObject key = array2[i];
                                dictionary.Add(key, false);
                            }
                            foreach (GameObject current2 in this.m_LastSelection.Keys)
                            {
                                if (!dictionary.ContainsKey(current2))
                                {
                                    flag = true;
                                    break;
                                }
                            }
                        }
                        if (flag)
                        {
                            this.m_LastSelection = new Dictionary <GameObject, bool>(array.Length);
                            GameObject[] array3 = array;
                            for (int j = 0; j < array3.Length; j++)
                            {
                                GameObject key2 = array3[j];
                                this.m_LastSelection.Add(key2, false);
                            }
                            if (array != null)
                            {
                                if (current.shift)
                                {
                                    RectSelection.UpdateSelection(this.m_SelectionStart, array, RectSelection.SelectionType.Additive, this.m_RectSelecting);
                                }
                                else if (EditorGUI.actionKey)
                                {
                                    RectSelection.UpdateSelection(this.m_SelectionStart, array, RectSelection.SelectionType.Subtractive, this.m_RectSelecting);
                                }
                                else
                                {
                                    RectSelection.UpdateSelection(this.m_SelectionStart, array, RectSelection.SelectionType.Normal, this.m_RectSelecting);
                                }
                            }
                        }
                    }
                    current.Use();
                }
                goto IL_4F7;

            case EventType.Repaint:
                if (GUIUtility.hotControl == num && this.m_RectSelecting)
                {
                    EditorStyles.selectionRect.Draw(EditorGUIExt.FromToRect(this.m_SelectStartPoint, this.m_SelectMousePoint), GUIContent.none, false, false, false, false);
                }
                goto IL_4F7;

            case EventType.Layout:
                if (!Tools.viewToolActive)
                {
                    HandleUtility.AddDefaultControl(num);
                }
                goto IL_4F7;
            }
            goto IL_4B;
IL_4F7:
            Handles.EndGUI();
        }
 public static GameObject PickGameObject(Vector2 position, bool selectPrefabRoot, GameObject[] ignore)
 {
     return(HandleUtility.PickGameObject(position, selectPrefabRoot, ignore, null));
 }
 public static GameObject PickGameObject(Vector2 position, GameObject[] ignore, out int materialIndex)
 {
     return(HandleUtility.PickGameObject(position, ignore, null, out materialIndex));
 }
Beispiel #7
0
        public static GameObject PickGameObject(Vector2 mousePosition)
        {
            SceneViewPicking.s_RetainHashes = true;
            IEnumerator <GameObject> enumerator = SceneViewPicking.GetAllOverlapping(mousePosition).GetEnumerator();

            if (!enumerator.MoveNext())
            {
                return(null);
            }
            GameObject current     = enumerator.Current;
            GameObject gameObject  = HandleUtility.FindSelectionBase(current);
            GameObject gameObject2 = (!(gameObject == null)) ? gameObject : current;
            int        hashCode    = current.GetHashCode();
            int        num         = hashCode;

            if (Selection.activeGameObject == null)
            {
                SceneViewPicking.s_PreviousTopmostHash = hashCode;
                SceneViewPicking.s_PreviousPrefixHash  = num;
                return(gameObject2);
            }
            if (hashCode != SceneViewPicking.s_PreviousTopmostHash)
            {
                SceneViewPicking.s_PreviousTopmostHash = hashCode;
                SceneViewPicking.s_PreviousPrefixHash  = num;
                return((!(Selection.activeGameObject == gameObject)) ? gameObject2 : current);
            }
            SceneViewPicking.s_PreviousTopmostHash = hashCode;
            if (Selection.activeGameObject == gameObject)
            {
                if (num == SceneViewPicking.s_PreviousPrefixHash)
                {
                    return(current);
                }
                SceneViewPicking.s_PreviousPrefixHash = num;
                return(gameObject);
            }
            else
            {
                GameObject x = HandleUtility.PickGameObject(mousePosition, false, null, new GameObject[]
                {
                    Selection.activeGameObject
                });
                if (x == Selection.activeGameObject)
                {
                    while (enumerator.Current != Selection.activeGameObject)
                    {
                        if (!enumerator.MoveNext())
                        {
                            SceneViewPicking.s_PreviousPrefixHash = hashCode;
                            return(gameObject2);
                        }
                        SceneViewPicking.UpdateHash(ref num, enumerator.Current);
                    }
                }
                if (num != SceneViewPicking.s_PreviousPrefixHash)
                {
                    SceneViewPicking.s_PreviousPrefixHash = hashCode;
                    return(gameObject2);
                }
                if (!enumerator.MoveNext())
                {
                    SceneViewPicking.s_PreviousPrefixHash = hashCode;
                    return(gameObject2);
                }
                SceneViewPicking.UpdateHash(ref num, enumerator.Current);
                if (enumerator.Current == gameObject)
                {
                    if (!enumerator.MoveNext())
                    {
                        SceneViewPicking.s_PreviousPrefixHash = hashCode;
                        return(gameObject2);
                    }
                    SceneViewPicking.UpdateHash(ref num, enumerator.Current);
                }
                SceneViewPicking.s_PreviousPrefixHash = num;
                return(enumerator.Current);
            }
        }
        public void OnGUI()
        {
            Event current1 = Event.current;

            Handles.BeginGUI();
            Vector2   mousePosition   = current1.mousePosition;
            int       rectSelectionId = RectSelection.s_RectSelectionID;
            EventType typeForControl  = current1.GetTypeForControl(rectSelectionId);

            switch (typeForControl)
            {
            case EventType.MouseDown:
                if (HandleUtility.nearestControl == rectSelectionId && current1.button == 0)
                {
                    GUIUtility.hotControl   = rectSelectionId;
                    this.m_SelectStartPoint = mousePosition;
                    this.m_SelectionStart   = Selection.objects;
                    this.m_RectSelecting    = false;
                    break;
                }
                break;

            case EventType.MouseUp:
                if (GUIUtility.hotControl == rectSelectionId && current1.button == 0)
                {
                    GUIUtility.hotControl = 0;
                    if (this.m_RectSelecting)
                    {
                        EditorApplication.modifierKeysChanged -= new EditorApplication.CallbackFunction(this.SendCommandsOnModifierKeys);
                        this.m_RectSelecting  = false;
                        this.m_SelectionStart = new UnityEngine.Object[0];
                        current1.Use();
                        break;
                    }
                    if (current1.shift || EditorGUI.actionKey)
                    {
                        GameObject[] gameObjectArray;
                        if (current1.shift)
                        {
                            gameObjectArray = new GameObject[1]
                            {
                                Selection.activeGameObject
                            }
                        }
                        ;
                        else
                        {
                            gameObjectArray = Selection.gameObjects;
                        }
                        GameObject[] gameObjects = gameObjectArray;
                        GameObject   hovered     = SceneViewPicking.GetHovered(current1.mousePosition, gameObjects);
                        if ((UnityEngine.Object)hovered != (UnityEngine.Object)null)
                        {
                            RectSelection.UpdateSelection(this.m_SelectionStart, (UnityEngine.Object)hovered, RectSelection.SelectionType.Subtractive, this.m_RectSelecting);
                        }
                        else
                        {
                            RectSelection.UpdateSelection(this.m_SelectionStart, (UnityEngine.Object)HandleUtility.PickGameObject(current1.mousePosition, true), RectSelection.SelectionType.Additive, this.m_RectSelecting);
                        }
                    }
                    else
                    {
                        RectSelection.UpdateSelection(this.m_SelectionStart, (UnityEngine.Object)SceneViewPicking.PickGameObject(current1.mousePosition), RectSelection.SelectionType.Normal, this.m_RectSelecting);
                    }
                    current1.Use();
                    break;
                }
                break;

            case EventType.MouseDrag:
                if (GUIUtility.hotControl == rectSelectionId)
                {
                    if (!this.m_RectSelecting && (double)(mousePosition - this.m_SelectStartPoint).magnitude > 6.0)
                    {
                        EditorApplication.modifierKeysChanged += new EditorApplication.CallbackFunction(this.SendCommandsOnModifierKeys);
                        this.m_RectSelecting    = true;
                        this.m_LastSelection    = (Dictionary <GameObject, bool>)null;
                        this.m_CurrentSelection = (UnityEngine.Object[])null;
                    }
                    if (this.m_RectSelecting)
                    {
                        this.m_SelectMousePoint = new Vector2(Mathf.Max(mousePosition.x, 0.0f), Mathf.Max(mousePosition.y, 0.0f));
                        GameObject[] gameObjectArray = HandleUtility.PickRectObjects(EditorGUIExt.FromToRect(this.m_SelectStartPoint, this.m_SelectMousePoint));
                        this.m_CurrentSelection = (UnityEngine.Object[])gameObjectArray;
                        bool flag1 = false;
                        if (this.m_LastSelection == null)
                        {
                            this.m_LastSelection = new Dictionary <GameObject, bool>();
                            flag1 = true;
                        }
                        bool flag2 = flag1 | this.m_LastSelection.Count != gameObjectArray.Length;
                        if (!flag2)
                        {
                            Dictionary <GameObject, bool> dictionary = new Dictionary <GameObject, bool>(gameObjectArray.Length);
                            foreach (GameObject key in gameObjectArray)
                            {
                                dictionary.Add(key, false);
                            }
                            using (Dictionary <GameObject, bool> .KeyCollection.Enumerator enumerator = this.m_LastSelection.Keys.GetEnumerator())
                            {
                                while (enumerator.MoveNext())
                                {
                                    GameObject current2 = enumerator.Current;
                                    if (!dictionary.ContainsKey(current2))
                                    {
                                        flag2 = true;
                                        break;
                                    }
                                }
                            }
                        }
                        if (flag2)
                        {
                            this.m_LastSelection = new Dictionary <GameObject, bool>(gameObjectArray.Length);
                            foreach (GameObject key in gameObjectArray)
                            {
                                this.m_LastSelection.Add(key, false);
                            }
                            if (gameObjectArray != null)
                            {
                                if (current1.shift)
                                {
                                    RectSelection.UpdateSelection(this.m_SelectionStart, (UnityEngine.Object[])gameObjectArray, RectSelection.SelectionType.Additive, this.m_RectSelecting);
                                }
                                else if (EditorGUI.actionKey)
                                {
                                    RectSelection.UpdateSelection(this.m_SelectionStart, (UnityEngine.Object[])gameObjectArray, RectSelection.SelectionType.Subtractive, this.m_RectSelecting);
                                }
                                else
                                {
                                    RectSelection.UpdateSelection(this.m_SelectionStart, (UnityEngine.Object[])gameObjectArray, RectSelection.SelectionType.Normal, this.m_RectSelecting);
                                }
                            }
                        }
                    }
                    current1.Use();
                    break;
                }
                break;

            case EventType.Repaint:
                if (GUIUtility.hotControl == rectSelectionId && this.m_RectSelecting)
                {
                    EditorStyles.selectionRect.Draw(EditorGUIExt.FromToRect(this.m_SelectStartPoint, this.m_SelectMousePoint), GUIContent.none, false, false, false, false);
                    break;
                }
                break;

            case EventType.Layout:
                if (!Tools.viewToolActive)
                {
                    HandleUtility.AddDefaultControl(rectSelectionId);
                    break;
                }
                break;

            default:
                if (typeForControl == EventType.ExecuteCommand && rectSelectionId == GUIUtility.hotControl && current1.commandName == "ModifierKeysChanged")
                {
                    if (current1.shift)
                    {
                        RectSelection.UpdateSelection(this.m_SelectionStart, this.m_CurrentSelection, RectSelection.SelectionType.Additive, this.m_RectSelecting);
                    }
                    else if (EditorGUI.actionKey)
                    {
                        RectSelection.UpdateSelection(this.m_SelectionStart, this.m_CurrentSelection, RectSelection.SelectionType.Subtractive, this.m_RectSelecting);
                    }
                    else
                    {
                        RectSelection.UpdateSelection(this.m_SelectionStart, this.m_CurrentSelection, RectSelection.SelectionType.Normal, this.m_RectSelecting);
                    }
                    current1.Use();
                    break;
                }
                break;
            }
            Handles.EndGUI();
        }
        public static GameObject PickGameObject(Vector2 mousePosition)
        {
            s_RetainHashes = true;

            var enumerator = GetAllOverlapping(mousePosition).GetEnumerator();

            if (!enumerator.MoveNext())
            {
                return(null);
            }

            var topmost = enumerator.Current;
            // Selection base is only interesting if it's not the topmost or if it's part of a prefab
            var selectionBase = HandleUtility.FindSelectionBaseForPicking(topmost);
            var first         = (selectionBase == null ? topmost : selectionBase);
            int topmostHash   = topmost.GetHashCode();
            int prefixHash    = topmostHash;

            if (Selection.activeGameObject == null)
            {
                // Nothing selected
                // Return selection base if it exists, otherwise topmost game object
                s_PreviousTopmostHash = topmostHash;
                s_PreviousPrefixHash  = prefixHash;
                return(first);
            }

            if (topmostHash != s_PreviousTopmostHash)
            {
                // Topmost game object changed
                // Return selection base if exists and is not already selected, otherwise topmost game object
                s_PreviousTopmostHash = topmostHash;
                s_PreviousPrefixHash  = prefixHash;
                return(Selection.activeGameObject == selectionBase ? topmost : first);
            }

            s_PreviousTopmostHash = topmostHash;

            // Pick potential selection base before topmost game object
            if (Selection.activeGameObject == selectionBase)
            {
                if (prefixHash == s_PreviousPrefixHash)
                {
                    return(topmost);
                }
                s_PreviousPrefixHash = prefixHash;
                return(selectionBase);
            }

            // Check if active game object will appear in selection stack
            var picked = HandleUtility.PickGameObject(mousePosition, false, null, new GameObject[] { Selection.activeGameObject });

            if (picked == Selection.activeGameObject)
            {
                // Advance enumerator to active game object
                while (enumerator.Current != Selection.activeGameObject)
                {
                    if (!enumerator.MoveNext())
                    {
                        s_PreviousPrefixHash = topmostHash;
                        return(first); // Should not occur
                    }

                    UpdateHash(ref prefixHash, enumerator.Current);
                }
            }

            if (prefixHash != s_PreviousPrefixHash)
            {
                // Prefix hash changed, start over
                s_PreviousPrefixHash = topmostHash;
                return(first);
            }

            // Move on to next game object
            if (!enumerator.MoveNext())
            {
                s_PreviousPrefixHash = topmostHash;
                return(first); // End reached, start over
            }

            UpdateHash(ref prefixHash, enumerator.Current);

            if (enumerator.Current == selectionBase)
            {
                // Skip selection base
                if (!enumerator.MoveNext())
                {
                    s_PreviousPrefixHash = topmostHash;
                    return(first); // End reached, start over
                }

                UpdateHash(ref prefixHash, enumerator.Current);
            }

            s_PreviousPrefixHash = prefixHash;
            return(enumerator.Current);
        }
Beispiel #10
0
        public static void OnSceneDrag(SceneView sceneView)
        {
            Event current = Event.current;

            if (current.type != EventType.DragUpdated && current.type != EventType.DragPerform && current.type != EventType.DragExited)
            {
                return;
            }
            if (!sceneView.in2DMode)
            {
                GameObject gameObject = HandleUtility.PickGameObject(Event.current.mousePosition, true);
                if (gameObject != null && DragAndDrop.objectReferences.Length == 1 && DragAndDrop.objectReferences[0] as Texture != null && gameObject.GetComponent <Renderer>() != null)
                {
                    SpriteUtility.CleanUp(true);
                    return;
                }
            }
            EventType type = current.type;

            if (type != EventType.DragUpdated)
            {
                if (type != EventType.DragPerform)
                {
                    if (type == EventType.DragExited)
                    {
                        if (SpriteUtility.s_SceneDragObjects != null && SpriteUtility.s_SceneDragObjects != null)
                        {
                            SpriteUtility.CleanUp(true);
                            current.Use();
                        }
                    }
                }
                else
                {
                    Sprite[] spriteFromDraggedPathsOrObjects = SpriteUtility.GetSpriteFromDraggedPathsOrObjects();
                    if (spriteFromDraggedPathsOrObjects != null && SpriteUtility.s_SceneDragObjects != null)
                    {
                        if (SpriteUtility.s_DragType == SpriteUtility.DragType.SpriteAnimation)
                        {
                            SpriteUtility.AddAnimationToGO((GameObject)SpriteUtility.s_SceneDragObjects[0], spriteFromDraggedPathsOrObjects);
                        }
                        using (List <UnityEngine.Object> .Enumerator enumerator = SpriteUtility.s_SceneDragObjects.GetEnumerator())
                        {
                            while (enumerator.MoveNext())
                            {
                                GameObject gameObject2 = (GameObject)enumerator.Current;
                                Undo.RegisterCreatedObjectUndo(gameObject2, "Create Sprite");
                                gameObject2.hideFlags = HideFlags.None;
                            }
                        }
                        Selection.objects = SpriteUtility.s_SceneDragObjects.ToArray();
                        SpriteUtility.CleanUp(false);
                        current.Use();
                    }
                }
            }
            else
            {
                SpriteUtility.DragType dragType = (!current.alt) ? SpriteUtility.DragType.SpriteAnimation : SpriteUtility.DragType.CreateMultiple;
                if (SpriteUtility.s_DragType != dragType || SpriteUtility.s_SceneDragObjects == null)
                {
                    Sprite[] spriteFromDraggedPathsOrObjects2 = SpriteUtility.GetSpriteFromDraggedPathsOrObjects();
                    if (spriteFromDraggedPathsOrObjects2 == null || spriteFromDraggedPathsOrObjects2.Length == 0)
                    {
                        return;
                    }
                    Sprite x = spriteFromDraggedPathsOrObjects2[0];
                    if (x == null)
                    {
                        return;
                    }
                    if (SpriteUtility.s_DragType != SpriteUtility.DragType.NotInitialized)
                    {
                        SpriteUtility.CleanUp(true);
                    }
                    SpriteUtility.s_DragType         = dragType;
                    SpriteUtility.s_SceneDragObjects = new List <UnityEngine.Object>();
                    if (SpriteUtility.s_DragType == SpriteUtility.DragType.CreateMultiple)
                    {
                        Sprite[] array = spriteFromDraggedPathsOrObjects2;
                        for (int i = 0; i < array.Length; i++)
                        {
                            Sprite frame = array[i];
                            SpriteUtility.s_SceneDragObjects.Add(SpriteUtility.CreateDragGO(frame, Vector3.zero));
                        }
                    }
                    else
                    {
                        SpriteUtility.s_SceneDragObjects.Add(SpriteUtility.CreateDragGO(spriteFromDraggedPathsOrObjects2[0], Vector3.zero));
                    }
                    List <Transform> list = new List <Transform>();
                    using (List <UnityEngine.Object> .Enumerator enumerator2 = SpriteUtility.s_SceneDragObjects.GetEnumerator())
                    {
                        while (enumerator2.MoveNext())
                        {
                            GameObject gameObject3 = (GameObject)enumerator2.Current;
                            list.AddRange(gameObject3.GetComponentsInChildren <Transform>());
                            gameObject3.hideFlags = HideFlags.HideInHierarchy;
                        }
                    }
                    HandleUtility.ignoreRaySnapObjects = list.ToArray();
                }
                Vector3 position = Vector3.zero;
                position = HandleUtility.GUIPointToWorldRay(current.mousePosition).GetPoint(10f);
                if (sceneView.in2DMode)
                {
                    position.z = 0f;
                }
                else
                {
                    DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                    object obj = HandleUtility.RaySnap(HandleUtility.GUIPointToWorldRay(current.mousePosition));
                    if (obj != null)
                    {
                        position = ((RaycastHit)obj).point;
                    }
                }
                using (List <UnityEngine.Object> .Enumerator enumerator3 = SpriteUtility.s_SceneDragObjects.GetEnumerator())
                {
                    while (enumerator3.MoveNext())
                    {
                        GameObject gameObject4 = (GameObject)enumerator3.Current;
                        gameObject4.transform.position = position;
                    }
                }
                DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                current.Use();
            }
        }
Beispiel #11
0
        private static Vector3 MoveHandlesGUI(Rect rect, Vector3 pivot, Quaternion rotation)
        {
            int     controlID = GUIUtility.GetControlID(RectTool.s_MoveHandleHash, FocusType.Passive);
            Vector3 vector    = pivot;
            float   num       = HandleUtility.GetHandleSize(pivot) * 0.2f;
            float   num2      = 1f - GUI.color.a;

            Vector3[] array = new Vector3[]
            {
                rotation *new Vector2(rect.x, rect.y) + pivot,
                rotation *new Vector2(rect.xMax, rect.y) + pivot,
                rotation *new Vector2(rect.xMax, rect.yMax) + pivot,
                rotation *new Vector2(rect.x, rect.yMax) + pivot
            };
            VertexSnapping.HandleKeyAndMouseMove(controlID);
            bool      flag           = Selection.transforms.Length == 1 && InternalEditorUtility.SupportsRectLayout(Selection.activeTransform) && Selection.activeTransform.parent.rotation == rotation;
            Event     current        = Event.current;
            EventType typeForControl = current.GetTypeForControl(controlID);
            Plane     plane          = new Plane(array[0], array[1], array[2]);

            switch (typeForControl)
            {
            case EventType.MouseDown:
            {
                bool flag2 = Tools.vertexDragging || (current.button == 0 && current.modifiers == EventModifiers.None && RectHandles.RaycastGUIPointToWorldHit(current.mousePosition, plane, out RectTool.s_StartMouseWorldPos) && (RectTool.SceneViewDistanceToRectangle(array, current.mousePosition) == 0f || (num2 > 0f && RectTool.SceneViewDistanceToDisc(pivot, rotation * Vector3.forward, num, current.mousePosition) == 0f)));
                if (flag2)
                {
                    RectTool.s_StartPosition = pivot;
                    RectTool.s_StartMousePos = (RectTool.s_CurrentMousePos = current.mousePosition);
                    RectTool.s_Moving        = false;
                    RectTool.s_LockAxis      = -1;
                    int num3 = controlID;
                    GUIUtility.keyboardControl = num3;
                    GUIUtility.hotControl      = num3;
                    EditorGUIUtility.SetWantsMouseJumping(1);
                    HandleUtility.ignoreRaySnapObjects = null;
                    current.Use();
                    if (flag)
                    {
                        Transform     activeTransform = Selection.activeTransform;
                        RectTransform component       = activeTransform.GetComponent <RectTransform>();
                        Transform     parent          = activeTransform.parent;
                        RectTransform component2      = parent.GetComponent <RectTransform>();
                        RectTool.s_StartRectPosition = component.anchoredPosition;
                        RectTransformSnapping.CalculatePositionSnapValues(parent, activeTransform, component2, component);
                    }
                }
                break;
            }

            case EventType.MouseUp:
                if (GUIUtility.hotControl == controlID)
                {
                    if (!RectTool.s_Moving)
                    {
                        Selection.activeGameObject = HandleUtility.PickGameObject(current.mousePosition, true);
                    }
                    GUIUtility.hotControl = 0;
                    EditorGUIUtility.SetWantsMouseJumping(0);
                    HandleUtility.ignoreRaySnapObjects = null;
                    current.Use();
                }
                break;

            case EventType.MouseDrag:
                if (GUIUtility.hotControl == controlID)
                {
                    RectTool.s_CurrentMousePos += current.delta;
                    if (!RectTool.s_Moving && (RectTool.s_CurrentMousePos - RectTool.s_StartMousePos).magnitude > 3f)
                    {
                        RectTool.s_Moving = true;
                        RectHandles.RaycastGUIPointToWorldHit(RectTool.s_CurrentMousePos, plane, out RectTool.s_StartMouseWorldPos);
                    }
                    if (RectTool.s_Moving)
                    {
                        if (Tools.vertexDragging)
                        {
                            if (HandleUtility.ignoreRaySnapObjects == null)
                            {
                                Handles.SetupIgnoreRaySnapObjects();
                            }
                            Vector3 vector2;
                            if (HandleUtility.FindNearestVertex(RectTool.s_CurrentMousePos, null, out vector2))
                            {
                                vector      = vector2;
                                GUI.changed = true;
                            }
                            ManipulationToolUtility.minDragDifference = Vector2.zero;
                        }
                        else
                        {
                            ManipulationToolUtility.SetMinDragDifferenceForPos(pivot);
                            Vector3 a;
                            if (RectHandles.RaycastGUIPointToWorldHit(RectTool.s_CurrentMousePos, plane, out a))
                            {
                                Vector3 vector3 = a - RectTool.s_StartMouseWorldPos;
                                if (current.shift)
                                {
                                    vector3 = Quaternion.Inverse(rotation) * vector3;
                                    if (RectTool.s_LockAxis == -1)
                                    {
                                        RectTool.s_LockAxis = ((Mathf.Abs(vector3.x) <= Mathf.Abs(vector3.y)) ? 1 : 0);
                                    }
                                    vector3[1 - RectTool.s_LockAxis] = 0f;
                                    vector3 = rotation * vector3;
                                }
                                else
                                {
                                    RectTool.s_LockAxis = -1;
                                }
                                if (flag)
                                {
                                    Transform parent2 = Selection.activeTransform.parent;
                                    Vector3   vector4 = RectTool.s_StartRectPosition + parent2.InverseTransformVector(vector3);
                                    vector4.z = 0f;
                                    Quaternion rotation2    = Quaternion.Inverse(rotation);
                                    Vector2    snapDistance = Vector2.one * HandleUtility.GetHandleSize(vector) * 0.05f;
                                    snapDistance.x /= (rotation2 * parent2.TransformVector(Vector3.right)).x;
                                    snapDistance.y /= (rotation2 * parent2.TransformVector(Vector3.up)).y;
                                    Vector3 vector5 = RectTransformSnapping.SnapToGuides(vector4, snapDistance);
                                    ManipulationToolUtility.DisableMinDragDifferenceBasedOnSnapping(vector4, vector5);
                                    vector3 = parent2.TransformVector(vector5 - RectTool.s_StartRectPosition);
                                }
                                vector      = RectTool.s_StartPosition + vector3;
                                GUI.changed = true;
                            }
                        }
                    }
                    current.Use();
                }
                break;

            case EventType.Repaint:
                if (Tools.vertexDragging)
                {
                    RectHandles.RectScalingCap(controlID, pivot, rotation, 1f);
                }
                else
                {
                    Handles.color = Handles.secondaryColor * new Color(1f, 1f, 1f, 1.5f * num2);
                    Handles.CircleCap(controlID, pivot, rotation, num);
                    Handles.color = Handles.secondaryColor * new Color(1f, 1f, 1f, 0.3f * num2);
                    Handles.DrawSolidDisc(pivot, rotation * Vector3.forward, num);
                }
                break;
            }
            ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp("ChangingPosX", typeForControl);
            ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp("ChangingLeft", typeForControl);
            ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp("ChangingRight", typeForControl);
            ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp("ChangingPosY", typeForControl);
            ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp("ChangingTop", typeForControl);
            ManipulationToolUtility.DetectDraggingBasedOnMouseDownUp("ChangingBottom", typeForControl);
            return(vector);
        }
Beispiel #12
0
        public void OnGUI()
        {
            Event current = Event.current;

            Handles.BeginGUI();
            Vector2   mousePosition  = current.mousePosition;
            int       controlID      = s_RectSelectionID;
            EventType typeForControl = current.GetTypeForControl(controlID);

            switch (typeForControl)
            {
            case EventType.MouseDown:
                if ((HandleUtility.nearestControl == controlID) && (current.button == 0))
                {
                    GUIUtility.hotControl   = controlID;
                    this.m_SelectStartPoint = mousePosition;
                    this.m_SelectionStart   = Selection.objects;
                    this.m_RectSelecting    = false;
                }
                goto Label_04D2;

            case EventType.MouseUp:
                if ((GUIUtility.hotControl == controlID) && (current.button == 0))
                {
                    GUIUtility.hotControl = 0;
                    if (!this.m_RectSelecting)
                    {
                        if (current.shift || EditorGUI.actionKey)
                        {
                            GameObject[] gameObjects = !current.shift ? Selection.gameObjects : new GameObject[] { Selection.activeGameObject };
                            GameObject   hovered     = SceneViewPicking.GetHovered(current.mousePosition, gameObjects);
                            if (hovered != null)
                            {
                                UpdateSelection(this.m_SelectionStart, hovered, SelectionType.Subtractive, this.m_RectSelecting);
                            }
                            else
                            {
                                UpdateSelection(this.m_SelectionStart, HandleUtility.PickGameObject(current.mousePosition, true), SelectionType.Additive, this.m_RectSelecting);
                            }
                        }
                        else
                        {
                            GameObject newObject = SceneViewPicking.PickGameObject(current.mousePosition);
                            UpdateSelection(this.m_SelectionStart, newObject, SelectionType.Normal, this.m_RectSelecting);
                        }
                        current.Use();
                    }
                    else
                    {
                        EditorApplication.modifierKeysChanged = (EditorApplication.CallbackFunction)Delegate.Remove(EditorApplication.modifierKeysChanged, new EditorApplication.CallbackFunction(this.SendCommandsOnModifierKeys));
                        this.m_RectSelecting  = false;
                        this.m_SelectionStart = new Object[0];
                        current.Use();
                    }
                }
                goto Label_04D2;

            case EventType.MouseDrag:
                if (GUIUtility.hotControl != controlID)
                {
                    goto Label_04D2;
                }
                if (!this.m_RectSelecting)
                {
                    Vector2 vector2 = mousePosition - this.m_SelectStartPoint;
                    if (vector2.magnitude > 6f)
                    {
                        EditorApplication.modifierKeysChanged = (EditorApplication.CallbackFunction)Delegate.Combine(EditorApplication.modifierKeysChanged, new EditorApplication.CallbackFunction(this.SendCommandsOnModifierKeys));
                        this.m_RectSelecting    = true;
                        this.m_LastSelection    = null;
                        this.m_CurrentSelection = null;
                    }
                }
                if (this.m_RectSelecting)
                {
                    float x = Mathf.Max(mousePosition.x, 0f);
                    this.m_SelectMousePoint = new Vector2(x, Mathf.Max(mousePosition.y, 0f));
                    GameObject[] newObjects = HandleUtility.PickRectObjects(EditorGUIExt.FromToRect(this.m_SelectStartPoint, this.m_SelectMousePoint));
                    this.m_CurrentSelection = newObjects;
                    bool flag = false;
                    if (this.m_LastSelection == null)
                    {
                        this.m_LastSelection = new Dictionary <GameObject, bool>();
                        flag = true;
                    }
                    flag |= this.m_LastSelection.Count != newObjects.Length;
                    if (!flag)
                    {
                        Dictionary <GameObject, bool> dictionary = new Dictionary <GameObject, bool>(newObjects.Length);
                        foreach (GameObject obj2 in newObjects)
                        {
                            dictionary.Add(obj2, false);
                        }
                        foreach (GameObject obj3 in this.m_LastSelection.Keys)
                        {
                            if (!dictionary.ContainsKey(obj3))
                            {
                                flag = true;
                                break;
                            }
                        }
                    }
                    if (flag)
                    {
                        this.m_LastSelection = new Dictionary <GameObject, bool>(newObjects.Length);
                        foreach (GameObject obj4 in newObjects)
                        {
                            this.m_LastSelection.Add(obj4, false);
                        }
                        if (newObjects != null)
                        {
                            if (current.shift)
                            {
                                UpdateSelection(this.m_SelectionStart, newObjects, SelectionType.Additive, this.m_RectSelecting);
                            }
                            else if (EditorGUI.actionKey)
                            {
                                UpdateSelection(this.m_SelectionStart, newObjects, SelectionType.Subtractive, this.m_RectSelecting);
                            }
                            else
                            {
                                UpdateSelection(this.m_SelectionStart, newObjects, SelectionType.Normal, this.m_RectSelecting);
                            }
                        }
                    }
                }
                break;

            case EventType.Repaint:
                if ((GUIUtility.hotControl == controlID) && this.m_RectSelecting)
                {
                    EditorStyles.selectionRect.Draw(EditorGUIExt.FromToRect(this.m_SelectStartPoint, this.m_SelectMousePoint), GUIContent.none, false, false, false, false);
                }
                goto Label_04D2;

            case EventType.Layout:
                if (!Tools.viewToolActive)
                {
                    HandleUtility.AddDefaultControl(controlID);
                }
                goto Label_04D2;

            default:
                if ((typeForControl == EventType.ExecuteCommand) && ((controlID == GUIUtility.hotControl) && (current.commandName == "ModifierKeysChanged")))
                {
                    if (current.shift)
                    {
                        UpdateSelection(this.m_SelectionStart, this.m_CurrentSelection, SelectionType.Additive, this.m_RectSelecting);
                    }
                    else if (EditorGUI.actionKey)
                    {
                        UpdateSelection(this.m_SelectionStart, this.m_CurrentSelection, SelectionType.Subtractive, this.m_RectSelecting);
                    }
                    else
                    {
                        UpdateSelection(this.m_SelectionStart, this.m_CurrentSelection, SelectionType.Normal, this.m_RectSelecting);
                    }
                    current.Use();
                }
                goto Label_04D2;
            }
            current.Use();
Label_04D2:
            Handles.EndGUI();
        }
        public static void OnSceneDrag(SceneView sceneView)
        {
            Event current1 = Event.current;

            if (current1.type != EventType.DragUpdated && current1.type != EventType.DragPerform && current1.type != EventType.DragExited)
            {
                return;
            }
            if (!sceneView.in2DMode)
            {
                GameObject gameObject = HandleUtility.PickGameObject(Event.current.mousePosition, true);
                if ((UnityEngine.Object)gameObject != (UnityEngine.Object)null && DragAndDrop.objectReferences.Length == 1 && ((UnityEngine.Object)(DragAndDrop.objectReferences[0] as Texture) != (UnityEngine.Object)null && (UnityEngine.Object)gameObject.GetComponent <Renderer>() != (UnityEngine.Object)null))
                {
                    return;
                }
            }
            switch (current1.type)
            {
            case EventType.DragUpdated:
                SpriteUtility.DragType dragType = !current1.alt ? SpriteUtility.DragType.SpriteAnimation : SpriteUtility.DragType.CreateMultiple;
                if (SpriteUtility.s_DragType != dragType || SpriteUtility.s_SceneDragObjects == null)
                {
                    Sprite[] draggedPathsOrObjects = SpriteUtility.GetSpriteFromDraggedPathsOrObjects();
                    if (draggedPathsOrObjects == null || draggedPathsOrObjects.Length == 0 || (UnityEngine.Object)draggedPathsOrObjects[0] == (UnityEngine.Object)null)
                    {
                        break;
                    }
                    if (SpriteUtility.s_DragType != SpriteUtility.DragType.NotInitialized)
                    {
                        SpriteUtility.CleanUp();
                    }
                    SpriteUtility.s_DragType         = dragType;
                    SpriteUtility.s_SceneDragObjects = new List <UnityEngine.Object>();
                    if (SpriteUtility.s_DragType == SpriteUtility.DragType.CreateMultiple)
                    {
                        foreach (Sprite frame in draggedPathsOrObjects)
                        {
                            SpriteUtility.s_SceneDragObjects.Add((UnityEngine.Object)SpriteUtility.CreateDragGO(frame, Vector3.zero));
                        }
                    }
                    else
                    {
                        SpriteUtility.s_SceneDragObjects.Add((UnityEngine.Object)SpriteUtility.CreateDragGO(draggedPathsOrObjects[0], Vector3.zero));
                    }
                    List <Transform> transformList = new List <Transform>();
                    using (List <UnityEngine.Object> .Enumerator enumerator = SpriteUtility.s_SceneDragObjects.GetEnumerator())
                    {
                        while (enumerator.MoveNext())
                        {
                            GameObject current2 = (GameObject)enumerator.Current;
                            transformList.AddRange((IEnumerable <Transform>)current2.GetComponentsInChildren <Transform>());
                            current2.hideFlags = HideFlags.HideInHierarchy;
                        }
                    }
                    HandleUtility.ignoreRaySnapObjects = transformList.ToArray();
                }
                Vector3 zero  = Vector3.zero;
                Vector3 point = HandleUtility.GUIPointToWorldRay(current1.mousePosition).GetPoint(10f);
                if (sceneView.in2DMode)
                {
                    point.z = 0.0f;
                }
                else
                {
                    DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                    object obj = HandleUtility.RaySnap(HandleUtility.GUIPointToWorldRay(current1.mousePosition));
                    if (obj != null)
                    {
                        point = ((RaycastHit)obj).point;
                    }
                }
                using (List <UnityEngine.Object> .Enumerator enumerator = SpriteUtility.s_SceneDragObjects.GetEnumerator())
                {
                    while (enumerator.MoveNext())
                    {
                        ((GameObject)enumerator.Current).transform.position = point;
                    }
                }
                DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                current1.Use();
                break;

            case EventType.DragPerform:
                Sprite[] draggedPathsOrObjects1 = SpriteUtility.GetSpriteFromDraggedPathsOrObjects();
                if (draggedPathsOrObjects1 == null || SpriteUtility.s_SceneDragObjects == null)
                {
                    break;
                }
                if (SpriteUtility.s_DragType == SpriteUtility.DragType.SpriteAnimation)
                {
                    SpriteUtility.AddAnimationToGO((GameObject)SpriteUtility.s_SceneDragObjects[0], draggedPathsOrObjects1);
                }
                using (List <UnityEngine.Object> .Enumerator enumerator = SpriteUtility.s_SceneDragObjects.GetEnumerator())
                {
                    while (enumerator.MoveNext())
                    {
                        GameObject current2 = (GameObject)enumerator.Current;
                        Undo.RegisterCreatedObjectUndo((UnityEngine.Object)current2, "Create Sprite");
                        current2.hideFlags = HideFlags.None;
                    }
                }
                Selection.objects = SpriteUtility.s_SceneDragObjects.ToArray();
                SpriteUtility.CleanUp();
                current1.Use();
                break;

            case EventType.DragExited:
                if (SpriteUtility.s_SceneDragObjects == null || SpriteUtility.s_SceneDragObjects == null)
                {
                    break;
                }
                using (List <UnityEngine.Object> .Enumerator enumerator = SpriteUtility.s_SceneDragObjects.GetEnumerator())
                {
                    while (enumerator.MoveNext())
                    {
                        UnityEngine.Object.DestroyImmediate(enumerator.Current, false);
                    }
                }
                SpriteUtility.CleanUp();
                current1.Use();
                break;
            }
        }
Beispiel #14
0
 public static GameObject GetHovered(Vector2 screenPosition, GameObject[] gameObjects)
 {
     return(HandleUtility.PickGameObject(screenPosition, false, null, gameObjects));
 }
Beispiel #15
0
 public static void HandleSpriteSceneDrag(SceneView sceneView, IEvent evt, UnityEngine.Object[] objectReferences, string[] paths, SpriteUtility.ShowFileDialogDelegate saveFileDialog)
 {
     if (evt.type == EventType.DragUpdated || evt.type == EventType.DragPerform || evt.type == EventType.DragExited)
     {
         if (!objectReferences.Any((UnityEngine.Object obj) => obj == null))
         {
             if (objectReferences.Length == 1 && objectReferences[0] as UnityEngine.Texture2D != null)
             {
                 GameObject gameObject = HandleUtility.PickGameObject(evt.mousePosition, true);
                 if (gameObject != null)
                 {
                     Renderer component = gameObject.GetComponent <Renderer>();
                     if (component != null && !(component is SpriteRenderer))
                     {
                         SpriteUtility.CleanUp(true);
                         return;
                     }
                 }
             }
             EventType type = evt.type;
             if (type != EventType.DragUpdated)
             {
                 if (type != EventType.DragPerform)
                 {
                     if (type == EventType.DragExited)
                     {
                         if (SpriteUtility.s_SceneDragObjects != null)
                         {
                             SpriteUtility.CleanUp(true);
                             evt.Use();
                         }
                     }
                 }
                 else
                 {
                     List <Sprite> spriteFromPathsOrObjects = SpriteUtility.GetSpriteFromPathsOrObjects(objectReferences, paths, evt.type);
                     if (spriteFromPathsOrObjects.Count > 0 && SpriteUtility.s_SceneDragObjects != null)
                     {
                         int currentGroup = Undo.GetCurrentGroup();
                         if (SpriteUtility.s_SceneDragObjects.Count == 0)
                         {
                             SpriteUtility.CreateSceneDragObjects(spriteFromPathsOrObjects);
                             SpriteUtility.PositionSceneDragObjects(SpriteUtility.s_SceneDragObjects, sceneView, evt.mousePosition);
                         }
                         using (List <UnityEngine.Object> .Enumerator enumerator = SpriteUtility.s_SceneDragObjects.GetEnumerator())
                         {
                             while (enumerator.MoveNext())
                             {
                                 GameObject gameObject2 = (GameObject)enumerator.Current;
                                 gameObject2.hideFlags = HideFlags.None;
                                 Undo.RegisterCreatedObjectUndo(gameObject2, "Create Sprite");
                                 EditorUtility.SetDirty(gameObject2);
                             }
                         }
                         bool flag = true;
                         if (SpriteUtility.s_DragType == SpriteUtility.DragType.SpriteAnimation && spriteFromPathsOrObjects.Count > 1)
                         {
                             UsabilityAnalytics.Event("Sprite Drag and Drop", "Drop multiple sprites to scene", "null", 1);
                             flag = SpriteUtility.AddAnimationToGO((GameObject)SpriteUtility.s_SceneDragObjects[0], spriteFromPathsOrObjects.ToArray(), saveFileDialog);
                         }
                         else
                         {
                             UsabilityAnalytics.Event("Sprite Drag and Drop", "Drop single sprite to scene", "null", 1);
                         }
                         if (flag)
                         {
                             Selection.objects = SpriteUtility.s_SceneDragObjects.ToArray();
                         }
                         else
                         {
                             Undo.RevertAllDownToGroup(currentGroup);
                         }
                         SpriteUtility.CleanUp(!flag);
                         evt.Use();
                     }
                 }
             }
             else
             {
                 SpriteUtility.DragType dragType = (!evt.alt) ? SpriteUtility.DragType.SpriteAnimation : SpriteUtility.DragType.CreateMultiple;
                 if (SpriteUtility.s_DragType != dragType || SpriteUtility.s_SceneDragObjects == null)
                 {
                     if (!SpriteUtility.ExistingAssets(objectReferences) && SpriteUtility.PathsAreValidTextures(paths))
                     {
                         DragAndDrop.visualMode           = DragAndDropVisualMode.Copy;
                         SpriteUtility.s_SceneDragObjects = new List <UnityEngine.Object>();
                         SpriteUtility.s_DragType         = dragType;
                     }
                     else
                     {
                         List <Sprite> spriteFromPathsOrObjects2 = SpriteUtility.GetSpriteFromPathsOrObjects(objectReferences, paths, evt.type);
                         if (spriteFromPathsOrObjects2.Count == 0)
                         {
                             return;
                         }
                         if (SpriteUtility.s_DragType != SpriteUtility.DragType.NotInitialized)
                         {
                             SpriteUtility.CleanUp(true);
                         }
                         SpriteUtility.s_DragType = dragType;
                         SpriteUtility.CreateSceneDragObjects(spriteFromPathsOrObjects2);
                         SpriteUtility.IgnoreForRaycasts(SpriteUtility.s_SceneDragObjects);
                     }
                 }
                 SpriteUtility.PositionSceneDragObjects(SpriteUtility.s_SceneDragObjects, sceneView, evt.mousePosition);
                 DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                 evt.Use();
             }
         }
     }
 }
Beispiel #16
0
        public static void HandleSpriteSceneDrag(SceneView sceneView, IEvent evt, Object[] objectReferences, string[] paths, ShowFileDialogDelegate saveFileDialog)
        {
            if (evt.type != EventType.DragUpdated && evt.type != EventType.DragPerform && evt.type != EventType.DragExited)
            {
                return;
            }

            // Return if any of the dragged objects are null, e.g. a MonoBehaviour without a managed instance
            if (objectReferences.Any(obj => obj == null))
            {
                return;
            }

            // Regardless of EditorBehaviorMode or SceneView mode we don't handle if texture is dragged over a GO with renderer
            if (objectReferences.Length == 1 && objectReferences[0] as UnityTexture2D != null)
            {
                GameObject go = HandleUtility.PickGameObject(evt.mousePosition, true);
                if (go != null)
                {
                    var renderer = go.GetComponent <Renderer>();
                    if (renderer != null && !(renderer is SpriteRenderer))
                    {
                        // There is an object where the cursor is
                        // and we are dragging a texture. Most likely user wants to
                        // assign texture to the GO
                        // Case 730444: Proceed only if the go has a renderer
                        CleanUp(true);
                        return;
                    }
                }
            }

            switch (evt.type)
            {
            case (EventType.DragUpdated):
                DragType newDragType = evt.alt ? DragType.CreateMultiple : DragType.SpriteAnimation;

                if (s_DragType != newDragType || s_SceneDragObjects == null)               // Either this is first time we are here OR evt.alt changed during drag
                {
                    if (!ExistingAssets(objectReferences) && PathsAreValidTextures(paths)) // External drag with images that are not in the project
                    {
                        DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                        s_SceneDragObjects     = new List <Object>();
                        s_DragType             = newDragType;
                    }
                    else     // Internal drag with assets from project
                    {
                        List <Sprite> assets = GetSpriteFromPathsOrObjects(objectReferences, paths, evt.type);

                        if (assets.Count == 0)
                        {
                            return;
                        }

                        if (s_DragType != DragType.NotInitialized)     // evt.alt changed during drag, so we need to cleanup and start over
                        {
                            CleanUp(true);
                        }

                        s_DragType = newDragType;
                        CreateSceneDragObjects(assets, sceneView);
                        IgnoreForRaycasts(s_SceneDragObjects);
                    }
                }

                PositionSceneDragObjects(s_SceneDragObjects, sceneView, evt.mousePosition);

                DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                evt.Use();
                break;

            case (EventType.DragPerform):
                List <Sprite> sprites = GetSpriteFromPathsOrObjects(objectReferences, paths, evt.type);

                if (sprites.Count > 0 && s_SceneDragObjects != null)
                {
                    // Store current undoIndex to undo all operations done if any part of sprite creation fails
                    int undoIndex = Undo.GetCurrentGroup();

                    // For external drags, we have delayed all creation to DragPerform because only now we have the imported sprite assets
                    if (s_SceneDragObjects.Count == 0)
                    {
                        CreateSceneDragObjects(sprites, sceneView);
                        PositionSceneDragObjects(s_SceneDragObjects, sceneView, evt.mousePosition);
                    }

                    foreach (GameObject dragGO in s_SceneDragObjects)
                    {
                        dragGO.hideFlags = HideFlags.None;

                        // When in e.g Prefab Mode ensure to reparent dragged objects under the prefab root
                        if (sceneView.customParentForDraggedObjects != null)
                        {
                            dragGO.transform.SetParent(sceneView.customParentForDraggedObjects, true);
                        }

                        Undo.RegisterCreatedObjectUndo(dragGO, "Create Sprite");
                        EditorUtility.SetDirty(dragGO);
                    }

                    bool createGameObject = true;
                    if (s_DragType == DragType.SpriteAnimation && sprites.Count > 1)
                    {
                        createGameObject = AddAnimationToGO((GameObject)s_SceneDragObjects[0], sprites.ToArray(), saveFileDialog);
                    }

                    if (createGameObject)
                    {
                        Selection.objects = s_SceneDragObjects.ToArray();
                    }
                    else
                    {
                        // Revert all Create Sprite actions if animation failed to be created or was cancelled
                        Undo.RevertAllDownToGroup(undoIndex);
                    }
                    CleanUp(!createGameObject);
                    evt.Use();
                }
                break;

            case EventType.DragExited:
                if (s_SceneDragObjects != null)
                {
                    CleanUp(true);
                    evt.Use();
                }
                break;
            }
        }
Beispiel #17
0
        public static GameObject PickGameObject(Vector2 mousePosition)
        {
            SceneViewPicking.s_RetainHashes = true;
            IEnumerator <GameObject> enumerator = SceneViewPicking.GetAllOverlapping(mousePosition).GetEnumerator();

            if (!enumerator.MoveNext())
            {
                return((GameObject)null);
            }
            GameObject current       = enumerator.Current;
            GameObject selectionBase = HandleUtility.FindSelectionBase(current);
            GameObject gameObject    = !((UnityEngine.Object)selectionBase == (UnityEngine.Object)null) ? selectionBase : current;
            int        hashCode      = current.GetHashCode();
            int        hash          = hashCode;

            if ((UnityEngine.Object)Selection.activeGameObject == (UnityEngine.Object)null || hashCode != SceneViewPicking.s_PreviousTopmostHash)
            {
                SceneViewPicking.s_PreviousTopmostHash = hashCode;
                SceneViewPicking.s_PreviousPrefixHash  = hash;
                return(gameObject);
            }
            SceneViewPicking.s_PreviousTopmostHash = hashCode;
            if ((UnityEngine.Object)selectionBase != (UnityEngine.Object)null && (UnityEngine.Object)Selection.activeGameObject == (UnityEngine.Object)selectionBase)
            {
                if (hash == SceneViewPicking.s_PreviousPrefixHash)
                {
                    return(current);
                }
                SceneViewPicking.s_PreviousPrefixHash = hash;
                return(selectionBase);
            }
            if ((UnityEngine.Object)HandleUtility.PickGameObject(mousePosition, 0 != 0, (GameObject[])null, new GameObject[1] {
                Selection.activeGameObject
            }) == (UnityEngine.Object)Selection.activeGameObject)
            {
                while ((UnityEngine.Object)enumerator.Current != (UnityEngine.Object)Selection.activeGameObject)
                {
                    if (!enumerator.MoveNext())
                    {
                        SceneViewPicking.s_PreviousPrefixHash = hashCode;
                        return(gameObject);
                    }
                    SceneViewPicking.UpdateHash(ref hash, (object)enumerator.Current);
                }
            }
            if (hash != SceneViewPicking.s_PreviousPrefixHash)
            {
                SceneViewPicking.s_PreviousPrefixHash = hashCode;
                return(gameObject);
            }
            if (!enumerator.MoveNext())
            {
                SceneViewPicking.s_PreviousPrefixHash = hashCode;
                return(gameObject);
            }
            SceneViewPicking.UpdateHash(ref hash, (object)enumerator.Current);
            if ((UnityEngine.Object)enumerator.Current == (UnityEngine.Object)selectionBase)
            {
                if (!enumerator.MoveNext())
                {
                    SceneViewPicking.s_PreviousPrefixHash = hashCode;
                    return(gameObject);
                }
                SceneViewPicking.UpdateHash(ref hash, (object)enumerator.Current);
            }
            SceneViewPicking.s_PreviousPrefixHash = hash;
            return(enumerator.Current);
        }