/// <inheritdoc cref="IDrawer.SelectNextOfType" />
        public override void SelectNextOfType()
        {
            // Get rect of focused control to try and preserve focused control.
            int  focusedControlWas;
            Rect focusedControlLocalRect;

            GetFocusedControlAndLocalRect(out focusedControlWas, out focusedControlLocalRect);

            Component component;

            if (!HierarchyUtility.TryGetNextOfType(Component, out component))
            {
                if (component == null)
                {
                    inspector.Message("No instances to select found in scene.");
                }
                else
                {
                    inspector.Message("No additional instances found in scene.");
                }
                return;
            }

            SelectShowAndFocusControl(component, focusedControlWas, focusedControlLocalRect);
        }
Ejemplo n.º 2
0
        public static Object DeserializeReference(string content, bool throwErrorIfFailed)
        {
            // support Object ID
            // (non-asset reference copied from an Object field)
            int objectId;

            if (int.TryParse(content, out objectId))
            {
                return(InstanceIdUtility.IdToObject(objectId, Types.UnityObject));
            }

                        #if UNITY_EDITOR
            // support pasting by path and local file identifier
            // (asset reference copied from an Object field,
            // where target is not the main asset)
            object pathAndIdObject = new AssetPathAndLocalId();
            if (DeserializeOverride(content, typeof(AssetPathAndLocalId), ref pathAndIdObject))
            {
                var pathAndId = (AssetPathAndLocalId)pathAndIdObject;
                if (pathAndId.HasPath())
                {
                    return(pathAndId.Load());
                }
            }

            // support pasting using asset path
            var asset = UnityEditor.AssetDatabase.LoadMainAssetAtPath(content);
            if (asset != null)
            {
                return(asset);
            }
                        #endif

            // support pasting GameObject using hierarchy path
            var go = HierarchyUtility.FindByHierarchyPath(content);
            if (go != null)
            {
                return(go);
            }
            // support pasting Component using hierarchy path
            var comp = HierarchyUtility.FindComponentByHierarchyPath(content, Types.Component);
            if (comp != null)
            {
                return(comp);
            }

            if (throwErrorIfFailed)
            {
                throw new InvalidOperationException();
            }

            return(null);
        }
Ejemplo n.º 3
0
        public static bool TryDeserializeReference(string stringData, out Object result)
        {
            // support Object ID
            // (non-asset reference copied from an Object field)
            int objectId;

            if (int.TryParse(stringData, out objectId))
            {
                result = InstanceIdUtility.IdToObject(objectId, Types.UnityObject);
                if (result != null)
                {
                    return(true);
                }
            }

                        #if UNITY_EDITOR
            // support pasting by path and local file identifier
            // (asset reference copied from an Object field,
            // where target is not the main asset)
            object pathAndIdObject = new AssetPathAndLocalId();
            if (DeserializeOverride(stringData, typeof(AssetPathAndLocalId), ref pathAndIdObject))
            {
                var pathAndId = (AssetPathAndLocalId)pathAndIdObject;
                if (pathAndId.HasPath())
                {
                    result = pathAndId.Load();
                    return(true);
                }
            }

            // support pasting using asset path
            result = UnityEditor.AssetDatabase.LoadMainAssetAtPath(stringData);
            if (result != null)
            {
                return(true);
            }
                        #endif

            // support pasting GameObject using hierarchy path
            result = HierarchyUtility.FindByHierarchyPath(stringData);
            if (result != null)
            {
                return(true);
            }
            // support pasting Component using hierarchy path
            result = HierarchyUtility.FindComponentByHierarchyPath(stringData, Types.Component);
            if (result != null)
            {
                return(result);
            }

            return(false);
        }
Ejemplo n.º 4
0
        public static bool IsSerializedReference(string stringData)
        {
                        #if UNITY_EDITOR
            // support reference by guid and local file identifier
            // (asset reference copied from an Object field)
            object pathAndId = new AssetPathAndLocalId();
            if (DeserializeOverride(stringData, typeof(AssetPathAndLocalId), ref pathAndId))
            {
                var guidAndId = (AssetPathAndLocalId)pathAndId;
                if (guidAndId.HasPath())
                {
                    return(true);
                }
            }
                        #endif

            // support reference by Object ID
            // (non-asset reference copied from an Object field)
            int objectId;
            if (int.TryParse(stringData, out objectId))
            {
                if (InstanceIdUtility.IdToObject(objectId, Types.UnityObject) != null)
                {
                    return(true);
                }
            }

                        #if UNITY_EDITOR
            // support preference by asset path
            if (UnityEditor.AssetDatabase.LoadMainAssetAtPath(stringData) != null)
            {
                return(true);
            }
                        #endif

            // support GameObject reference by hierarchy path
            if (HierarchyUtility.FindByHierarchyPath(stringData) != null)
            {
                return(true);
            }

            // support pasting Component using hierarchy path
            if (HierarchyUtility.FindComponentByHierarchyPath(stringData, Types.Component) != null)
            {
                return(true);
            }

            return(false);
        }
Ejemplo n.º 5
0
        private static bool TryGetPreviewUsingStringValue([NotNull] string stringValue, [CanBeNull] Type type, MenuItemValueType valueType, ref Texture preview)
        {
            switch (valueType)
            {
            case MenuItemValueType.Undefined:

                if (TryGetPreviewUsingStringValue(stringValue, type, MenuItemValueType.AssetPath, ref preview))
                {
                    return(true);
                }
                if (TryGetPreviewUsingStringValue(stringValue, type, MenuItemValueType.AssetGuid, ref preview))
                {
                    return(true);
                }
                return(TryGetPreviewUsingStringValue(stringValue, type, MenuItemValueType.HierarchyPath, ref preview));

            case MenuItemValueType.AssetPath:
                preview = AssetDatabase.GetCachedIcon(stringValue);
                return(preview != null);

            case MenuItemValueType.AssetGuid:
                var guid = AssetDatabase.GUIDToAssetPath(stringValue);
                if (!string.IsNullOrEmpty(guid))
                {
                    preview = AssetDatabase.GetCachedIcon(guid);
                    return(preview != null);
                }
                return(false);

            case MenuItemValueType.HierarchyPath:
                if (type != null && type.IsComponent())
                {
                    var component = HierarchyUtility.FindComponentByHierarchyPath(stringValue, type);
                    preview = AssetPreview.GetAssetPreview(component);
                    return(preview != null);
                }

                var gameObject = HierarchyUtility.FindByHierarchyPath(stringValue);
                if (gameObject != null)
                {
                    preview = AssetPreview.GetAssetPreview(gameObject);
                    return(preview != null);
                }
                return(false);
            }

            throw new NotImplementedException("TryGetPreviewUsingStringValue does not support MenuItemValueType " + valueType);
        }
Ejemplo n.º 6
0
        public static void SelectPreviousOfType([NotNull] IComponentDrawer subject)
        {
            var inspector = InspectorUtility.ActiveInspector;

            var selected          = inspector.FocusedDrawer;
            var selectedIndexPath = selected == null ? null : selected.GenerateMemberIndexPath(subject);

            Component component;

            if (!HierarchyUtility.TryGetPreviousOfType(subject.Component, out component))
            {
                if (component == null)
                {
                    inspector.Message("No instances to select found in scene.");
                }
                else
                {
                    inspector.Message("No additional instances found in scene.");
                }
                return;
            }

            if (component.gameObject != subject.gameObject)
            {
                // UPDATE: New test to preserve selected path
                // TO DO: Support custom editors
                inspector.OnNextInspectedChanged(() =>
                {
                    if (selectedIndexPath == null)
                    {
                        inspector.SelectAndShow(component, ReasonSelectionChanged.SelectPrevOfType);
                    }
                    else
                    {
                        var componentDrawer = inspector.State.drawers.FindDrawer(component);
                        componentDrawer.SelectMemberAtIndexPath(selectedIndexPath, ReasonSelectionChanged.SelectPrevOfType);
                    }
                });
                inspector.Select(component);
            }
            else
            {
                inspector.SelectAndShow(component, ReasonSelectionChanged.SelectPrevOfType);
            }
        }
Ejemplo n.º 7
0
        public static bool CanPasteAs(Type type)
        {
            if (type.IsUnityObject())
            {
                var clipboardContentType = CopiedType;

                                #if DEV_MODE
                Debug.Log("CanPasteAs(" + type.Name + ") with CopiedType=" + clipboardContentType.Name);
                                #endif

                if (type.IsAssignableFrom(clipboardContentType))
                {
                    lastOperationFailed = false;
                    return(true);
                }

                if (clipboardContentType == Types.String)
                {
                    string content = Content;

                                        #if UNITY_EDITOR
                    //support pasting using asset path
                    var assetAtPath = AssetDatabase.LoadAssetAtPath(content, type);
                    if (assetAtPath != null)
                    {
                        lastOperationFailed = false;
                        return(true);
                    }
                                        #endif

                    //support pasting using hierarchy path
                    if (type == Types.GameObject || type == Types.UnityObject)
                    {
                        if (HierarchyUtility.FindByHierarchyPath(content) != null)
                        {
                            lastOperationFailed = false;
                            return(true);
                        }
                    }
                    if (type == Types.UnityObject || type.IsComponent())
                    {
                        if (HierarchyUtility.FindComponentByHierarchyPath(content, type) != null)
                        {
                            lastOperationFailed = false;
                            return(true);
                        }
                    }
                }

                // we can convert between Component and GameObject types
                // using Component.gameObject and gameObject.GetComponent
                if (clipboardContentType == Types.GameObject)
                {
                    if (type.IsComponent())
                    {
                        lastOperationFailed = false;
                        return(true);
                    }
                }
                else if (clipboardContentType.IsComponent())
                {
                    if (type.IsComponent() || type.IsGameObject())
                    {
                        lastOperationFailed = false;
                        return(true);
                    }
                }

                                #if DEV_MODE
                Debug.Log("CanPasteAs(" + type.Name + "): " + StringUtils.False);
                                #endif
                lastOperationFailed = true;
                return(false);
            }

            try
            {
                Paste(type, true);
                return(true);
            }
                        #if DEV_MODE
            catch (Exception e)
            {
                Debug.LogWarning("CanPasteAs failed for type " + StringUtils.ToString(type) + ": " + e);
                return(false);
            }
                        #else
            catch
            {
                return(false);
            }
                        #endif
        }
Ejemplo n.º 8
0
        public static Object PasteObjectReference(Type type)
        {
            lastOperationFailed = true;

            Object result;

            if (CopiedType == Types.String)
            {
                result = null;
                string content = Content;

                //support pasting using hierarchy path
                if (type == Types.GameObject || type == Types.UnityObject)
                {
                    result = HierarchyUtility.FindByHierarchyPath(content);
                }

                if (result == null)
                {
                    if (type == Types.UnityObject || type.IsComponent())
                    {
                        result = HierarchyUtility.FindComponentByHierarchyPath(content, type);
                    }

                                        #if UNITY_EDITOR
                    //support pasting using asset path
                    if (result == null)
                    {
                        var asset = AssetDatabase.LoadAssetAtPath(content, type);
                        if (asset != null)
                        {
                            result = asset;
                        }
                    }
                                        #endif
                }
            }
            else
            {
                result = objectReference;
            }

            if (result != null)
            {
                var resultType = result.GetType();
                if (!type.IsAssignableFrom(resultType))
                {
                    var resultGameObject = result as GameObject;
                    if (resultGameObject != null)
                    {
                        if (type.IsComponent())
                        {
                            result = resultGameObject.GetComponent(type);
                        }
                        else
                        {
                            result = null;
                        }
                    }
                    else
                    {
                        var resultComponent = result as Component;
                        if (resultComponent != null)
                        {
                            if (type.IsGameObject())
                            {
                                result = resultComponent.gameObject;
                            }
                            else
                            {
                                result = null;
                            }
                        }
                        else
                        {
                                                        #if UNITY_EDITOR
                            string path = AssetDatabase.GetAssetPath(result);
                            if (!string.IsNullOrEmpty(path))
                            {
                                result = AssetDatabase.LoadAssetAtPath(path, type);
                            }
                            else
                            {
                                result = null;
                            }
                                                        #else
                            result = null;
                                                        #endif
                        }
                    }
                }
            }

            if (isCut)
            {
                OnCutPasted();
            }

            lastOperationFailed = false;

            return(result);
        }
Ejemplo n.º 9
0
        public static void SelectNextOfType([NotNull] IComponentDrawer subject)
        {
            var inspector = InspectorUtility.ActiveInspector;

            var selected          = inspector.FocusedDrawer;
            var selectedIndexPath = selected == null ? null : selected.GenerateMemberIndexPath(subject);

            Component component;

            if (!HierarchyUtility.TryGetNextOfType(subject.Component, out component))
            {
                if (component == null)
                {
                    inspector.Message("No instances to select found in scene");
                }
                else
                {
                    inspector.Message("No additional instances found in scene");
                }
                return;
            }

            if (component.gameObject != subject.gameObject)
            {
                                #if DEV_MODE
                Debug.Log("Selecting " + component.GetType().Name + " on " + component.name + " during OnNextInspectedChanged.");
                                #endif

                //UPDATE: New test to preserve selected path
                // TO DO: Support custom editors
                inspector.OnNextInspectedChanged(() =>
                {
                    if (selectedIndexPath == null)
                    {
                        inspector.SelectAndShow(component, ReasonSelectionChanged.SelectNextOfType);
                    }
                    else
                    {
                        var componentDrawer = inspector.State.drawers.FindDrawer(component);
                        componentDrawer.SelectMemberAtIndexPath(selectedIndexPath, ReasonSelectionChanged.SelectNextOfType);
                    }
                });
                inspector.Select(component.gameObject);
            }
            else
            {
                                #if DEV_MODE
                Debug.Log("Component " + component.GetType().Name + " found on same GameObject " + component.name + ".");
                                #endif

                if (selectedIndexPath == null)
                {
                    inspector.SelectAndShow(component, ReasonSelectionChanged.SelectNextOfType);
                }
                else
                {
                    var componentDrawer = inspector.State.drawers.FindDrawer(component);
                    componentDrawer.SelectMemberAtIndexPath(selectedIndexPath, ReasonSelectionChanged.SelectNextOfType);
                }
            }
        }
Ejemplo n.º 10
0
        public static Object PasteObjectReference([NotNull] Type type)
        {
                        #if DEV_MODE && PI_ASSERTATIONS
            Debug.Assert(type != null, "PasteObjectReference(null)");
            Debug.Assert(type.IsUnityObject() || type.IsInterface, type.FullName);
                        #endif

            lastOperationFailed = true;

            Object result;
            if (CopiedType == Types.String)
            {
                result = null;
                string content = Content;

                //support pasting using hierarchy path
                if (type == Types.GameObject || type == Types.UnityObject)
                {
                    result = HierarchyUtility.FindByHierarchyPath(content);
                }

                if (result == null)
                {
                    if (type == Types.UnityObject || type.IsComponent())
                    {
                        result = HierarchyUtility.FindComponentByHierarchyPath(content, type);
                    }

                                        #if UNITY_EDITOR
                    //support pasting using asset path
                    if (result == null)
                    {
                        var asset = AssetDatabase.LoadAssetAtPath(content, type);
                        if (asset != null)
                        {
                            result = asset;
                        }
                    }
                                        #endif
                }
            }
            else
            {
                result = objectReference;
            }

            if (result != null && !type.IsInstanceOfType(result))
            {
                var resultGameObject = result as GameObject;
                if (resultGameObject != null)
                {
                    if (type.IsComponent() || type.IsInterface)
                    {
                                                #if DEV_MODE
                        Debug.Log("PasteObjectReference(" + type.Name + ") !IsInstanceOfType(" + result.GetType().Name + ") and resultGameObject.GetComponent(" + type.Name + "): " + StringUtils.ToColorizedString(resultGameObject.GetComponent(type)));
                                                #endif

                        result = resultGameObject.GetComponent(type);
                    }
                    else
                    {
                                                #if DEV_MODE
                        Debug.Log("PasteObjectReference(" + type.Name + ") !IsInstanceOfType(" + result.GetType().Name + ") so returning: " + StringUtils.Null);
                                                #endif

                        result = null;
                    }
                }
                else
                {
                    var resultComponent = result as Component;
                    if (resultComponent != null)
                    {
                        if (type.IsGameObject())
                        {
                                                        #if DEV_MODE
                            Debug.Log("PasteObjectReference(" + type.Name + ") !IsInstanceOfType(" + result.GetType().Name + ") but returning resultComponent.gameObject: " + StringUtils.ToColorizedString(resultComponent.gameObject));
                                                        #endif

                            result = resultComponent.gameObject;
                        }
                        else
                        {
                                                        #if DEV_MODE
                            Debug.Log("PasteObjectReference(" + type.Name + ") !IsInstanceOfType(" + result.GetType().Name + ") so returning: " + StringUtils.Null);
                                                        #endif

                            result = null;
                        }
                    }
                    else
                    {
                                                #if UNITY_EDITOR
                        string path = AssetDatabase.GetAssetPath(result);
                        if (!string.IsNullOrEmpty(path))
                        {
                            result = AssetDatabase.LoadAssetAtPath(path, type);
                        }
                        else
                        {
                            result = null;
                        }
                                                #else
                        result = null;
                                                #endif
                    }
                }
            }
                        #if DEV_MODE
            else
            {
                Debug.Log("PasteObjectReference(" + type.Name + ") IsInstanceOfType(" + StringUtils.ToColorizedString(result) + ")=" + StringUtils.True);
            }
                        #endif

            if (isCut)
            {
                OnCutPasted();
            }

            lastOperationFailed = false;

            return(result);
        }
Ejemplo n.º 11
0
 int IComparer <GameObject> .Compare(GameObject a, GameObject b)
 {
     return(HierarchyUtility.CompareHierarchyOrder(a, b));
 }
Ejemplo n.º 12
0
 int IComparer <Transform> .Compare(Transform a, Transform b)
 {
     return(HierarchyUtility.CompareHierarchyOrder(a, b));
 }
 int IComparer <Component> .Compare(Component a, Component b)
 {
     return(HierarchyUtility.CompareHierarchyOrder(a, b));
 }