示例#1
0
        void FolderBrowsing(HierarchyProperty property)
        {
            // We are not concerned with assets being added multiple times as we only show the contents
            // of each selected folder. This is an issue when searching recursively into child folders.
            List <FilterResult> list = new List <FilterResult>();

            foreach (string folderPath in m_SearchFilter.folders)
            {
                int folderInstanceID = AssetDatabase.GetMainAssetOrInProgressProxyInstanceID(folderPath);
                if (property.Find(folderInstanceID, null))
                {
                    int   folderDepth = property.depth;
                    int[] expanded    = { folderInstanceID };
                    while (property.Next(expanded))
                    {
                        if (property.depth <= folderDepth)
                        {
                            break; // current property is outside folder
                        }
                        FilterResult result = new FilterResult();
                        CopyPropertyData(ref result, property);
                        list.Add(result);

                        // Fetch sub assets by expanding the main asset (ignore folders)
                        if (property.hasChildren && !property.isFolder)
                        {
                            System.Array.Resize(ref expanded, expanded.Length + 1);
                            expanded[expanded.Length - 1] = property.instanceID;
                        }
                    }
                }
            }
            m_Results = list.ToArray();
        }
        internal static void GetAncestors(int instanceID, HashSet <int> ancestors)
        {
            // Ensure we handle packages root folder
            if (instanceID == ProjectBrowser.kPackagesFolderInstanceId)
            {
                return;
            }

            // Ensure we add the main asset as ancestor if input is a subasset
            int  mainAssetInstanceID = AssetDatabase.GetMainAssetOrInProgressProxyInstanceID(AssetDatabase.GetAssetPath(instanceID));
            bool isSubAsset          = mainAssetInstanceID != instanceID;

            if (isSubAsset)
            {
                ancestors.Add(mainAssetInstanceID);
            }

            // Find ancestors of main aset
            string currentFolderPath = GetContainingFolder(AssetDatabase.GetAssetPath(mainAssetInstanceID));

            while (!string.IsNullOrEmpty(currentFolderPath))
            {
                int currentInstanceID = ProjectBrowser.GetFolderInstanceID(currentFolderPath);
                ancestors.Add(currentInstanceID);
                currentFolderPath = GetContainingFolder(AssetDatabase.GetAssetPath(currentInstanceID));
            }
        }
示例#3
0
        private static IEnumerator <T> FindInFolders <T>(SearchFilter searchFilter, Func <HierarchyProperty,  T> selector)
        {
            foreach (string folderPath in searchFilter.folders)
            {
                var folderInstanceID = AssetDatabase.GetMainAssetOrInProgressProxyInstanceID(folderPath);
                var rootPath         = "Assets";

                var pathComponents = folderPath.Split('/');
                // Find the right rootPath if folderPath is part of a package
                if (pathComponents.Length > 1 && pathComponents[0] == UnityEditor.PackageManager.Folders.GetPackagesMountPoint())
                {
                    rootPath = pathComponents[0] + "/" + pathComponents[1];
                }

                // Set empty filter to ensure we search all assets to find folder
                var property = new HierarchyProperty(rootPath);
                property.SetSearchFilter(new SearchFilter());
                if (property.Find(folderInstanceID, null))
                {
                    // Set filter after we found the folder
                    property.SetSearchFilter(searchFilter);
                    int   folderDepth = property.depth;
                    int[] expanded    = null; // enter all children of folder
                    while (property.NextWithDepthCheck(expanded, folderDepth + 1))
                    {
                        yield return(selector(property));
                    }
                }
                else
                {
                    Debug.LogWarning("AssetDatabase.FindAssets: Folder not found: '" + folderPath + "'");
                }
            }
        }
示例#4
0
        private void FolderBrowsing(HierarchyProperty property)
        {
            List <FilteredHierarchy.FilterResult> list = new List <FilteredHierarchy.FilterResult>();

            string[] folders = this.m_SearchFilter.folders;
            for (int i = 0; i < folders.Length; i++)
            {
                string assetPath = folders[i];
                int    mainAssetOrInProgressProxyInstanceID = AssetDatabase.GetMainAssetOrInProgressProxyInstanceID(assetPath);
                if (property.Find(mainAssetOrInProgressProxyInstanceID, null))
                {
                    int   depth = property.depth;
                    int[] array = new int[]
                    {
                        mainAssetOrInProgressProxyInstanceID
                    };
                    while (property.Next(array))
                    {
                        if (property.depth <= depth)
                        {
                            break;
                        }
                        FilteredHierarchy.FilterResult item = new FilteredHierarchy.FilterResult();
                        this.CopyPropertyData(ref item, property);
                        list.Add(item);
                        if (property.hasChildren && !property.isFolder)
                        {
                            Array.Resize <int>(ref array, array.Length + 1);
                            array[array.Length - 1] = property.instanceID;
                        }
                    }
                }
            }
            this.m_Results = list.ToArray();
        }
示例#5
0
        protected override void DoubleClickedItem(int id)
        {
            BuildPlayerSceneTreeViewItem buildPlayerSceneTreeViewItem = base.FindItem(id, base.rootItem) as BuildPlayerSceneTreeViewItem;
            int mainAssetOrInProgressProxyInstanceID = AssetDatabase.GetMainAssetOrInProgressProxyInstanceID(buildPlayerSceneTreeViewItem.fullName);

            EditorGUIUtility.PingObject(mainAssetOrInProgressProxyInstanceID);
        }
示例#6
0
        private void BuildRoots()
        {
            m_Roots = new List <RootItem>();

            if (m_rootInstanceID != 0)
            {
                m_Roots.Add(new RootItem(m_rootInstanceID, null, null, true));
                return;
            }

            var packagesMountPoint = PackageManager.Folders.GetPackagesPath();

            var assetsFolderInstanceID = AssetDatabase.GetMainAssetOrInProgressProxyInstanceID("Assets");

            m_Roots.Add(new RootItem(assetsFolderInstanceID, null, null, true));

            var packages = PackageManagerUtilityInternal.GetAllVisiblePackages(skipHiddenPackages);

            m_Roots.Add(new RootItem(ProjectBrowser.kPackagesFolderInstanceId, packagesMountPoint, packagesMountPoint, true));
            foreach (var package in packages)
            {
                var displayName             = !string.IsNullOrEmpty(package.displayName) ? package.displayName : package.name;
                var packageFolderInstanceID = AssetDatabase.GetMainAssetOrInProgressProxyInstanceID(package.assetPath);
                if (packageFolderInstanceID == 0)
                {
                    continue;
                }

                m_Roots.Add(new RootItem(packageFolderInstanceID, displayName, package.assetPath));
            }
        }
示例#7
0
        // Returns instanceID of folders (and main asset if input is a subasset) up until and including the Assets folder
        public static int[] GetAncestors(int instanceID)
        {
            List <int> ancestors = new List <int>();

            // Ensure we add the main asset as ancestor if input is a subasset
            int  mainAssetInstanceID = AssetDatabase.GetMainAssetOrInProgressProxyInstanceID(AssetDatabase.GetAssetPath(instanceID));
            bool isSubAsset          = mainAssetInstanceID != instanceID;

            if (isSubAsset)
            {
                ancestors.Add(mainAssetInstanceID);
            }

            // Find ancestors of main aset
            string currentFolderPath = GetContainingFolder(AssetDatabase.GetAssetPath(mainAssetInstanceID));

            while (!string.IsNullOrEmpty(currentFolderPath))
            {
                int currentInstanceID = AssetDatabase.GetMainAssetOrInProgressProxyInstanceID(currentFolderPath);
                ancestors.Add(currentInstanceID);
                currentFolderPath = GetContainingFolder(AssetDatabase.GetAssetPath(currentInstanceID));
            }

            return(ancestors.ToArray());
        }
        void FolderBrowsing()
        {
            // We are not concerned with assets being added multiple times as we only show the contents
            // of each selected folder. This is an issue when searching recursively into child folders.
            List <FilterResult> list = new List <FilterResult>();
            HierarchyProperty   property;

            foreach (string folderPath in m_SearchFilter.folders)
            {
                if (folderPath == PackageManager.Folders.GetPackagesPath())
                {
                    var packages = PackageManagerUtilityInternal.GetAllVisiblePackages(m_SearchFilter.skipHidden);
                    foreach (var package in packages)
                    {
                        var packageFolderInstanceId = AssetDatabase.GetMainAssetOrInProgressProxyInstanceID(package.assetPath);
                        property = new HierarchyProperty(package.assetPath);
                        if (property.Find(packageFolderInstanceId, null))
                        {
                            FilterResult result = new FilterResult();
                            CopyPropertyData(ref result, property);
                            result.name = !string.IsNullOrEmpty(package.displayName) ? package.displayName : package.name;
                            list.Add(result);
                        }
                    }
                    continue;
                }

                if (m_SearchFilter.skipHidden && !PackageManagerUtilityInternal.IsPathInVisiblePackage(folderPath))
                {
                    continue;
                }

                int folderInstanceID = AssetDatabase.GetMainAssetOrInProgressProxyInstanceID(folderPath);
                property = new HierarchyProperty(folderPath);
                property.SetSearchFilter(m_SearchFilter);

                int   folderDepth = property.depth;
                int[] expanded    = { folderInstanceID };
                while (property.Next(expanded))
                {
                    if (property.depth <= folderDepth)
                    {
                        break; // current property is outside folder
                    }
                    FilterResult result = new FilterResult();
                    CopyPropertyData(ref result, property);
                    list.Add(result);

                    // Fetch sub assets by expanding the main asset (ignore folders)
                    if (property.hasChildren && !property.isFolder)
                    {
                        System.Array.Resize(ref expanded, expanded.Length + 1);
                        expanded[expanded.Length - 1] = property.instanceID;
                    }
                }
            }
            m_Results = list.ToArray();
        }
        private static IEnumerator <T> FindInFolders <T>(SearchFilter searchFilter, Func <HierarchyProperty,  T> selector)
        {
            var folders = new List <string>();

            folders.AddRange(searchFilter.folders);
            if (folders.Remove(PackageManager.Folders.GetPackagesMountPoint()))
            {
                var packages = PackageManager.Packages.GetAll();
                foreach (var package in packages)
                {
                    if (package.source == PackageManager.PackageSource.BuiltIn)
                    {
                        continue;
                    }

                    if (!folders.Contains(package.assetPath))
                    {
                        folders.Add(package.assetPath);
                    }
                }
            }

            foreach (string folderPath in folders)
            {
                var folderInstanceID = AssetDatabase.GetMainAssetOrInProgressProxyInstanceID(folderPath);
                var rootPath         = "Assets";

                // Find the right rootPath if folderPath is part of a package
                var packageInfo = PackageManager.Packages.GetForAssetPath(folderPath);
                if (packageInfo != null)
                {
                    rootPath = packageInfo.assetPath;
                }

                // Set empty filter to ensure we search all assets to find folder
                var property = new HierarchyProperty(rootPath);
                property.SetSearchFilter(new SearchFilter());
                if (property.Find(folderInstanceID, null))
                {
                    // Set filter after we found the folder
                    property.SetSearchFilter(searchFilter);
                    int   folderDepth = property.depth;
                    int[] expanded    = null; // enter all children of folder
                    while (property.NextWithDepthCheck(expanded, folderDepth + 1))
                    {
                        yield return(selector(property));
                    }
                }
                else
                {
                    Debug.LogWarning("AssetDatabase.FindAssets: Folder not found: '" + folderPath + "'");
                }
            }
        }
示例#10
0
        private static IEnumerator <T> FindInFolders <T>(SearchFilter searchFilter, Func <HierarchyProperty,  T> selector)
        {
            var folders = new List <string>();

            folders.AddRange(searchFilter.folders);
            if (folders.Remove(PackageManager.Folders.GetPackagesPath()))
            {
                var packages = PackageManagerUtilityInternal.GetAllVisiblePackages(searchFilter.skipHidden);
                foreach (var package in packages)
                {
                    if (!folders.Contains(package.assetPath))
                    {
                        folders.Add(package.assetPath);
                    }
                }
            }

            foreach (var folderPath in folders)
            {
                var sanitizedFolderPath = folderPath.ConvertSeparatorsToUnity().TrimTrailingSlashes();
                var folderInstanceID    = AssetDatabase.GetMainAssetOrInProgressProxyInstanceID(sanitizedFolderPath);
                var rootPath            = "Assets";

                // Find the right rootPath if folderPath is part of a package
                var packageInfo = PackageManager.PackageInfo.FindForAssetPath(sanitizedFolderPath);
                if (packageInfo != null)
                {
                    rootPath = packageInfo.assetPath;
                    if (searchFilter.skipHidden && !PackageManagerUtilityInternal.IsPathInVisiblePackage(rootPath))
                    {
                        continue;
                    }
                }

                // Set empty filter to ensure we search all assets to find folder
                var property = new HierarchyProperty(rootPath);
                property.SetSearchFilter(new SearchFilter());
                if (property.Find(folderInstanceID, null))
                {
                    // Set filter after we found the folder
                    property.SetSearchFilter(searchFilter);
                    int   folderDepth = property.depth;
                    int[] expanded    = null; // enter all children of folder
                    while (property.NextWithDepthCheck(expanded, folderDepth + 1))
                    {
                        yield return(selector(property));
                    }
                }
                else
                {
                    Debug.LogWarning("AssetDatabase.FindAssets: Folder not found: '" + sanitizedFolderPath + "'");
                }
            }
        }
        public override void FetchData()
        {
            m_RootItem = new TreeViewItem(System.Int32.MaxValue, 0, null, "Invisible Root Item");
            SetExpanded(m_RootItem, true); // ensure always visible

            // We want three roots: Favorites, Assets, and Saved Filters
            List <TreeViewItem> visibleRoots = new List <TreeViewItem>();

            // Fetch asset folders
            int          assetsFolderInstanceID = GetAssetsFolderInstanceID();
            int          depth         = 0;
            string       displayName   = "Assets"; //CreateDisplayName (assetsFolderInstanceID);
            TreeViewItem assetRootItem = new TreeViewItem(assetsFolderInstanceID, depth, m_RootItem, displayName);

            ReadAssetDatabase(HierarchyType.Assets, assetRootItem, depth + 1);

            // Fetch packages
            TreeViewItem packagesRootItem = null;

            if (Unsupported.IsDeveloperMode() && EditorPrefs.GetBool("ShowPackagesFolder"))
            {
                var    packagesFolderInstanceID = AssetDatabase.GetMainAssetOrInProgressProxyInstanceID(AssetDatabase.GetPackagesMountPoint());
                string packagesDisplayName      = AssetDatabase.GetPackagesMountPoint();
                packagesRootItem = new TreeViewItem(packagesFolderInstanceID, depth, m_RootItem, packagesDisplayName);
                ReadAssetDatabase(HierarchyType.Packages, packagesRootItem, depth + 1);
            }

            // Fetch saved filters
            TreeViewItem savedFiltersRootItem = SavedSearchFilters.ConvertToTreeView();

            savedFiltersRootItem.parent = m_RootItem;

            // Order
            visibleRoots.Add(savedFiltersRootItem);
            visibleRoots.Add(assetRootItem);

            if (packagesRootItem != null)
            {
                visibleRoots.Add(packagesRootItem);
            }

            m_RootItem.children = visibleRoots;

            // Get global expanded state of roots
            foreach (TreeViewItem item in m_RootItem.children)
            {
                bool expanded = EditorPrefs.GetBool(kProjectBrowserString + item.displayName, true);
                SetExpanded(item, expanded);
            }

            m_NeedRefreshRows = true;
        }
        // Should return the items that have children from id and below
        protected override HashSet <int> GetParentsBelow(int id)
        {
            // Add all children expanded ids to hashset
            HashSet <int> parentsBelow = new HashSet <int>();

            if (m_Roots.Count > 1)
            {
                var assetsInstanceIDs = AssetDatabase.GetMainAssetOrInProgressProxyInstanceID("Assets");
                if (id != assetsInstanceIDs)
                {
                    // Search in created first-level root items
                    var item = m_RootsTreeViewItem.Values.FirstOrDefault(tvi => tvi.id == id && tvi.depth == 0);
                    if (item != null)
                    {
                        parentsBelow.Add(id);
                        foreach (var child in item.children)
                        {
                            if (child != null && child.hasChildren)
                            {
                                parentsBelow.UnionWith(GetParentsBelow(child.id));
                            }
                        }
                        return(parentsBelow);
                    }
                }
            }

            // Search in all roots
            foreach (var root in m_Roots)
            {
                var rootPath = root.path ?? AssetDatabase.GetAssetPath(root.instanceID);
                IHierarchyProperty search = new HierarchyProperty(rootPath);
                if (search.Find(id, null))
                {
                    parentsBelow.Add(id);

                    int depth = search.depth;
                    while (search.Next(null) && search.depth > depth)
                    {
                        if (search.hasChildren)
                        {
                            parentsBelow.Add(search.instanceID);
                        }
                    }
                    break;
                }
            }
            return(parentsBelow);
        }
示例#13
0
        internal static void CutCopySelectedFolders(int[] instanceIDs, PerformedAction action)
        {
            Reset();
            performedAction = action;
            AssetDatabase.Refresh();

            int assetsFolderInstanceID = AssetDatabase.GetMainAssetOrInProgressProxyInstanceID("Assets");

            foreach (int instanceID in instanceIDs)
            {
                if (instanceID == assetsFolderInstanceID)
                {
                    continue;
                }

                ObjectIdentifier.GetObjectIdentifierFromInstanceID(instanceID, out var identifier);
                assetClipboard.Add(identifier);
            }
        }
示例#14
0
        public static int[] GetAncestors(int instanceID)
        {
            List <int> list = new List <int>();
            int        mainAssetOrInProgressProxyInstanceID = AssetDatabase.GetMainAssetOrInProgressProxyInstanceID(AssetDatabase.GetAssetPath(instanceID));
            bool       flag = mainAssetOrInProgressProxyInstanceID != instanceID;

            if (flag)
            {
                list.Add(mainAssetOrInProgressProxyInstanceID);
            }
            string containingFolder = ProjectWindowUtil.GetContainingFolder(AssetDatabase.GetAssetPath(mainAssetOrInProgressProxyInstanceID));

            while (!string.IsNullOrEmpty(containingFolder))
            {
                int mainAssetOrInProgressProxyInstanceID2 = AssetDatabase.GetMainAssetOrInProgressProxyInstanceID(containingFolder);
                list.Add(mainAssetOrInProgressProxyInstanceID2);
                containingFolder = ProjectWindowUtil.GetContainingFolder(AssetDatabase.GetAssetPath(mainAssetOrInProgressProxyInstanceID2));
            }
            return(list.ToArray());
        }
        public override void FetchData()
        {
            this.m_RootItem = new TreeViewItem(2147483647, 0, null, "Invisible Root Item");
            this.SetExpanded(this.m_RootItem, true);
            List <TreeViewItem> list            = new List <TreeViewItem>();
            int          assetsFolderInstanceID = ProjectBrowserColumnOneTreeViewDataSource.GetAssetsFolderInstanceID();
            int          num          = 0;
            string       displayName  = "Assets";
            TreeViewItem treeViewItem = new TreeViewItem(assetsFolderInstanceID, num, this.m_RootItem, displayName);

            this.ReadAssetDatabase(HierarchyType.Assets, treeViewItem, num + 1);
            TreeViewItem treeViewItem2 = null;

            if (Unsupported.IsDeveloperMode() && EditorPrefs.GetBool("ShowPackagesFolder"))
            {
                int    mainAssetOrInProgressProxyInstanceID = AssetDatabase.GetMainAssetOrInProgressProxyInstanceID(AssetDatabase.GetPackagesMountPoint());
                string packagesMountPoint = AssetDatabase.GetPackagesMountPoint();
                treeViewItem2 = new TreeViewItem(mainAssetOrInProgressProxyInstanceID, num, this.m_RootItem, packagesMountPoint);
                this.ReadAssetDatabase(HierarchyType.Packages, treeViewItem2, num + 1);
            }
            TreeViewItem treeViewItem3 = SavedSearchFilters.ConvertToTreeView();

            treeViewItem3.parent = this.m_RootItem;
            list.Add(treeViewItem3);
            list.Add(treeViewItem);
            if (treeViewItem2 != null)
            {
                list.Add(treeViewItem2);
            }
            this.m_RootItem.children = list;
            foreach (TreeViewItem current in this.m_RootItem.children)
            {
                bool @bool = EditorPrefs.GetBool(ProjectBrowserColumnOneTreeViewDataSource.kProjectBrowserString + current.displayName, true);
                this.SetExpanded(current, @bool);
            }
            this.m_NeedRefreshRows = true;
        }
        private HashSet <int> GetParentsBelow(int id)
        {
            // Add all children expanded ids to hashset
            HashSet <int> parentsBelow = new HashSet <int>();

            // Check if packages instance
            if (id == ProjectBrowser.kPackagesFolderInstanceId)
            {
                parentsBelow.Add(id);
                var packages = PackageManagerUtilityInternal.GetAllVisiblePackages(skipHiddenPackages);
                foreach (var package in packages)
                {
                    var packageFolderInstanceId = AssetDatabase.GetMainAssetOrInProgressProxyInstanceID(package.assetPath);
                    parentsBelow.UnionWith(GetParentsBelow(packageFolderInstanceId));
                }
                return(parentsBelow);
            }

            var path = AssetDatabase.GetAssetPath(id);
            IHierarchyProperty search = new HierarchyProperty(path);

            if (search.Find(id, null))
            {
                parentsBelow.Add(id);

                int depth = search.depth;
                while (search.Next(null) && search.depth > depth)
                {
                    if (search.isFolder && search.hasChildren)
                    {
                        parentsBelow.Add(search.instanceID);
                    }
                }
            }
            return(parentsBelow);
        }
        public override void FetchData()
        {
            bool firstInitialize = !isInitialized;

            m_RootItem = new TreeViewItem(0, 0, null, "Invisible Root Item");
            SetExpanded(m_RootItem, true); // ensure always visible

            // We want three roots: Favorites, Assets, and Packages
            List <TreeViewItem> visibleRoots = new List <TreeViewItem>();

            // Favorites root
            TreeViewItem savedFiltersRootItem = SavedSearchFilters.ConvertToTreeView();

            visibleRoots.Add(savedFiltersRootItem);

            // Assets root
            int    assetsFolderInstanceID = AssetDatabase.GetMainAssetOrInProgressProxyInstanceID("Assets");
            int    depth       = 0;
            string displayName = "Assets";

            AssetsTreeViewDataSource.RootTreeItem assetRootItem = new AssetsTreeViewDataSource.RootTreeItem(assetsFolderInstanceID, depth, m_RootItem, displayName);
            assetRootItem.icon = s_FolderIcon;
            visibleRoots.Add(assetRootItem);

            // Packages root
            displayName = PackageManager.Folders.GetPackagesPath();
            AssetsTreeViewDataSource.RootTreeItem packagesRootItem = new AssetsTreeViewDataSource.RootTreeItem(ProjectBrowser.kPackagesFolderInstanceId, depth, m_RootItem, displayName);
            packagesRootItem.icon = s_FolderIcon;
            visibleRoots.Add(packagesRootItem);

            m_RootItem.children = visibleRoots;

            // Set global expanded state for roots from EditorPrefs (must be before building the rows)
            if (firstInitialize)
            {
                foreach (TreeViewItem item in m_RootItem.children)
                {
                    bool expanded = EditorPrefs.GetBool(kProjectBrowserString + item.displayName, true);
                    SetExpanded(item, expanded);
                }
            }

            // Build rows
            //-----------
            m_Rows = new List <TreeViewItem>(100);

            // Favorites
            savedFiltersRootItem.parent = m_RootItem;
            m_Rows.Add(savedFiltersRootItem);
            if (IsExpanded(savedFiltersRootItem))
            {
                foreach (var f in savedFiltersRootItem.children)
                {
                    m_Rows.Add(f);
                }
            }
            else
            {
                savedFiltersRootItem.children = CreateChildListForCollapsedParent();
            }

            // Asset folders
            m_Rows.Add(assetRootItem);
            ReadAssetDatabase("Assets", assetRootItem, depth + 1, m_Rows);

            // Individual Package folders (under the Packages root item)
            m_Rows.Add(packagesRootItem);
            var packages = PackageManagerUtilityInternal.GetAllVisiblePackages(skipHiddenPackages);

            if (IsExpanded(packagesRootItem))
            {
                depth++;
                foreach (var package in packages)
                {
                    var packageFolderInstanceId = AssetDatabase.GetMainAssetOrInProgressProxyInstanceID(package.assetPath);

                    displayName = !string.IsNullOrEmpty(package.displayName) ? package.displayName : package.name;
                    AssetsTreeViewDataSource.PackageTreeItem packageItem = new AssetsTreeViewDataSource.PackageTreeItem(packageFolderInstanceId, depth, packagesRootItem, displayName);
                    packageItem.icon = s_FolderIcon;
                    packagesRootItem.AddChild(packageItem);
                    m_Rows.Add(packageItem);
                    ReadAssetDatabase(package.assetPath, packageItem, depth + 1, m_Rows);
                }
            }
            else
            {
                if (packages.Length > 0)
                {
                    packagesRootItem.children = CreateChildListForCollapsedParent();
                }
            }

            m_NeedRefreshRows = false;
        }
        // Deletes the assets of the instance IDs, with an optional user confirmation dialog.
        // Returns true if the delete operation was successfully performed on all assets.
        // Note: Zero input assets always returns true.
        // Also note that the operation cannot be undone even if some operations failed.
        internal static bool DeleteAssets(List <int> instanceIDs, bool askIfSure)
        {
            if (instanceIDs.Count == 0)
            {
                return(true);
            }

            bool foundAssetsFolder = instanceIDs.IndexOf(AssetDatabase.GetMainAssetOrInProgressProxyInstanceID("Assets")) >= 0;

            if (foundAssetsFolder)
            {
                EditorUtility.DisplayDialog(L10n.Tr("Cannot Delete"), L10n.Tr("Deleting the 'Assets' folder is not allowed"), L10n.Tr("Ok"));
                return(false);
            }

            bool reparentMaterials = false;
            var  paths             = GetMainPathsOfAssets(instanceIDs).ToArray();

            if (paths.Length == 0)
            {
                return(false);
            }

            if (askIfSure)
            {
                string title;
                if (paths.Length > 1)
                {
                    title = L10n.Tr("Delete selected assets?");
                }
                else
                {
                    title = L10n.Tr("Delete selected asset?");
                }

                int  maxCount         = 3;
                bool containsMaterial = false;

                var infotext = new StringBuilder();
                for (int i = 0; i < paths.Length; ++i)
                {
                    if (i < maxCount)
                    {
                        infotext.AppendLine(paths[i]);
                    }

                    if (paths[i].EndsWith(".mat", StringComparison.OrdinalIgnoreCase))
                    {
                        containsMaterial = true;
                        if (i >= maxCount)
                        {
                            break;
                        }
                    }
                }

                if (paths.Length > maxCount)
                {
                    infotext.AppendLine("...");
                }
                infotext.AppendLine("");
                infotext.AppendLine(L10n.Tr("You cannot undo the delete assets action."));

                containsMaterial &= AnyTargetMaterialHasChildren(paths);
                if (containsMaterial)
                {
                    infotext.AppendLine();
                    string name = (paths.Length == 1) ? "This Material" : "One or more of these Material(s)";
                    infotext.AppendLine(name + " has one or more children. Would you like to reparent all of these children to their closest remaining ancestor?");
                    int dialogOptionIndex = EditorUtility.DisplayDialogComplex(title, infotext.ToString(), L10n.Tr("Delete and reparent children"), L10n.Tr("Delete only"), L10n.Tr("Cancel"));
                    if (dialogOptionIndex == 0)
                    {
                        reparentMaterials = true;
                    }
                    else if (dialogOptionIndex == 2)
                    {
                        return(false);
                    }
                }
                else if (!EditorUtility.DisplayDialog(title, infotext.ToString(), L10n.Tr("Delete"), L10n.Tr("Cancel")))
                {
                    return(false);
                }
            }

            bool          success     = true;
            List <string> failedPaths = new List <string>();

            AssetDatabase.StartAssetEditing();

            if (reparentMaterials)
            {
                for (int i = 0; i < paths.Length; i++)
                {
                    if (paths[i].EndsWith(".mat", StringComparison.OrdinalIgnoreCase))
                    {
                        ReparentMaterialChildren(paths[i]);
                    }
                }
            }

            if (!AssetDatabase.MoveAssetsToTrash(paths, failedPaths))
            {
                success = false;
            }

            AssetDatabase.StopAssetEditing();

            if (!success)
            {
                var vcsOffline = false;
                if (!EditorUserSettings.WorkOffline)
                {
                    var vco = VersionControlManager.activeVersionControlObject;
                    if (vco != null)
                    {
                        vcsOffline = !vco.isConnected;
                    }
                    else if (Provider.enabled)
                    {
                        vcsOffline = !Provider.isActive;
                    }
                }
                var message = vcsOffline ?
                              L10n.Tr("Some assets could not be deleted.\nMake sure you are connected to your Version Control server or \"Work Offline\" is enabled.") :
                              L10n.Tr("Some assets could not be deleted.\nMake sure nothing is keeping a hook on them, like a loaded DLL for example.");

                EditorUtility.DisplayDialog(L10n.Tr("Cannot Delete"), message, L10n.Tr("Ok"));
            }

            PackageManager.Client.Resolve(false);

            return(success);
        }
示例#19
0
        // Deletes the assets of the instance IDs, with an optional user confirmation dialog.
        // Returns true if the delete operation was successfully performed on all assets.
        // Note: Zero input assets always returns true.
        // Also note that the operation cannot be undone even if some operations failed.
        internal static bool DeleteAssets(List <int> instanceIDs, bool askIfSure)
        {
            if (instanceIDs.Count == 0)
            {
                return(true);
            }

            bool foundAssetsFolder = instanceIDs.IndexOf(AssetDatabase.GetMainAssetOrInProgressProxyInstanceID("Assets")) >= 0;

            if (foundAssetsFolder)
            {
                EditorUtility.DisplayDialog(L10n.Tr("Cannot Delete"), L10n.Tr("Deleting the 'Assets' folder is not allowed"), L10n.Tr("Ok"));
                return(false);
            }

            var paths = GetMainPathsOfAssets(instanceIDs).ToList();

            if (paths.Count == 0)
            {
                return(false);
            }

            if (askIfSure)
            {
                string title;
                if (paths.Count > 1)
                {
                    title = L10n.Tr("Delete selected assets?");
                }
                else
                {
                    title = L10n.Tr("Delete selected asset?");
                }

                int maxCount = 3;
                var infotext = new StringBuilder();
                for (int i = 0; i < paths.Count && i < maxCount; ++i)
                {
                    infotext.AppendLine("   " + paths[i]);
                }
                if (paths.Count > maxCount)
                {
                    infotext.AppendLine("   ...");
                }
                infotext.AppendLine(L10n.Tr("You cannot undo this action."));
                if (!EditorUtility.DisplayDialog(title, infotext.ToString(), L10n.Tr("Delete"), L10n.Tr("Cancel")))
                {
                    return(false);
                }
            }

            bool success = true;

            AssetDatabase.StartAssetEditing();

            string[] pathArray = new string[paths.Count];
            for (int i = 0; i < pathArray.Length; i++)
            {
                pathArray[i] = paths[i];
            }

            List <string> failedPaths = new List <string>();

            if (!AssetDatabase.MoveAssetsToTrash(pathArray, failedPaths))
            {
                success = false;
            }

            AssetDatabase.StopAssetEditing();
            if (!success)
            {
                string message = (Provider.enabled && Provider.onlineState == OnlineState.Offline) ?
                                 L10n.Tr("Some assets could not be deleted.\nMake sure you are connected to your Version Control server or \"Work Offline\" is enabled.") :
                                 L10n.Tr("Some assets could not be deleted.\nMake sure nothing is keeping a hook on them, like a loaded DLL for example.");

                EditorUtility.DisplayDialog(L10n.Tr("Cannot Delete"), message, L10n.Tr("Ok"));
            }
            return(success);
        }
        public override void FetchData()
        {
            m_RootItem = new TreeViewItem(0, 0, null, "Invisible Root Item");
            SetExpanded(m_RootItem, true); // ensure always visible

            Texture2D folderIcon = EditorGUIUtility.FindTexture(EditorResources.folderIconName);

            // We want three roots: Favorites, Assets, and Saved Filters
            List <TreeViewItem> visibleRoots = new List <TreeViewItem>();

            // Fetch asset folders
            int    assetsFolderInstanceID = AssetDatabase.GetMainAssetOrInProgressProxyInstanceID("Assets");
            int    depth       = 0;
            string displayName = "Assets"; //CreateDisplayName (assetsFolderInstanceID);

            AssetsTreeViewDataSource.RootTreeItem assetRootItem = new AssetsTreeViewDataSource.RootTreeItem(assetsFolderInstanceID, depth, m_RootItem, displayName);
            assetRootItem.icon = folderIcon;
            ReadAssetDatabase("Assets", assetRootItem, depth + 1);

            // Fetch packages folder
            displayName = PackageManager.Folders.GetPackagesPath();
            AssetsTreeViewDataSource.RootTreeItem packagesRootItem = new AssetsTreeViewDataSource.RootTreeItem(ProjectBrowser.kPackagesFolderInstanceId, depth, m_RootItem, displayName);
            depth++;

            packagesRootItem.icon = folderIcon;

            var packages = PackageManagerUtilityInternal.GetAllVisiblePackages(skipHiddenPackages);

            foreach (var package in packages)
            {
                var packageFolderInstanceId = AssetDatabase.GetMainAssetOrInProgressProxyInstanceID(package.assetPath);

                displayName = !string.IsNullOrEmpty(package.displayName) ? package.displayName : package.name;
                AssetsTreeViewDataSource.PackageTreeItem packageItem = new AssetsTreeViewDataSource.PackageTreeItem(packageFolderInstanceId, depth, packagesRootItem, displayName);
                packageItem.icon = folderIcon;
                packagesRootItem.AddChild(packageItem);
                ReadAssetDatabase(package.assetPath, packageItem, depth + 1);
            }

            // Fetch saved filters
            TreeViewItem savedFiltersRootItem = SavedSearchFilters.ConvertToTreeView();

            savedFiltersRootItem.parent = m_RootItem;

            // Order
            visibleRoots.Add(savedFiltersRootItem);
            visibleRoots.Add(assetRootItem);
            visibleRoots.Add(packagesRootItem);

            m_RootItem.children = visibleRoots;

            // Get global expanded state of roots
            foreach (TreeViewItem item in m_RootItem.children)
            {
                // Do not expand Packages root item
                if (item.id == ProjectBrowser.kPackagesFolderInstanceId)
                {
                    continue;
                }
                bool expanded = EditorPrefs.GetBool(kProjectBrowserString + item.displayName, true);
                SetExpanded(item, expanded);
            }

            m_NeedRefreshRows = true;
        }
        // Deletes the assets of the instance IDs, with an optional user confirmation dialog.
        // Returns true if the delete operation was successfully performed on all assets.
        // Note: Zero input assets always returns true.
        // Also note that the operation cannot be undone even if some operations failed.
        internal static bool DeleteAssets(List <int> instanceIDs, bool askIfSure)
        {
            if (instanceIDs.Count == 0)
            {
                return(true);
            }

            bool foundAssetsFolder = instanceIDs.IndexOf(AssetDatabase.GetMainAssetOrInProgressProxyInstanceID("Assets")) >= 0;

            if (foundAssetsFolder)
            {
                EditorUtility.DisplayDialog(L10n.Tr("Cannot Delete"), L10n.Tr("Deleting the 'Assets' folder is not allowed"), L10n.Tr("Ok"));
                return(false);
            }

            var paths = GetMainPathsOfAssets(instanceIDs).ToList();

            if (paths.Count == 0)
            {
                return(false);
            }

            if (askIfSure)
            {
                string title;
                if (paths.Count > 1)
                {
                    title = L10n.Tr("Delete selected assets?");
                }
                else
                {
                    title = L10n.Tr("Delete selected asset?");
                }

                int maxCount = 3;
                var infotext = new StringBuilder();
                for (int i = 0; i < paths.Count && i < maxCount; ++i)
                {
                    infotext.AppendLine("   " + paths[i]);
                }
                if (paths.Count > maxCount)
                {
                    infotext.AppendLine("   ...");
                }
                infotext.AppendLine(L10n.Tr("You cannot undo this action."));
                if (!EditorUtility.DisplayDialog(title, infotext.ToString(), L10n.Tr("Delete"), L10n.Tr("Cancel")))
                {
                    return(false);
                }
            }

            bool success = true;

            AssetDatabase.StartAssetEditing();
            foreach (string path in paths)
            {
                if (!AssetDatabase.MoveAssetToTrash(path))
                {
                    success = false;
                }
            }
            AssetDatabase.StopAssetEditing();
            if (!success)
            {
                EditorUtility.DisplayDialog(L10n.Tr("Cannot Delete"), L10n.Tr("Some assets could not be deleted.\nMake sure nothing is keeping a hook on them, like a loaded DLL for example."), L10n.Tr("Ok"));
            }
            return(success);
        }
        public static int GetAssetsFolderInstanceID()
        {
            int instanceID = AssetDatabase.GetMainAssetOrInProgressProxyInstanceID("Assets");

            return(instanceID);
        }
示例#23
0
        // Deletes the assets of the instance IDs, with an optional user confirmation dialog.
        // Returns true if the delete operation was successfully performed on all assets.
        // Note: Zero input assets always returns true.
        // Also note that the operation cannot be undone even if some operations failed.
        internal static bool DeleteAssets(List <int> instanceIDs, bool askIfSure)
        {
            if (instanceIDs.Count == 0)
            {
                return(true);
            }

            bool foundAssetsFolder = instanceIDs.IndexOf(AssetDatabase.GetMainAssetOrInProgressProxyInstanceID("Assets")) >= 0;

            if (foundAssetsFolder)
            {
                string title = "Cannot Delete";
                EditorUtility.DisplayDialog(title, "Deleting the 'Assets' folder is not allowed", "Ok");
                return(false);
            }

            var paths = GetMainPathsOfAssets(instanceIDs).ToList();

            if (paths.Count == 0)
            {
                return(false);
            }

            if (askIfSure)
            {
                string title = "Delete selected asset";
                if (paths.Count > 1)
                {
                    title = title + "s";
                }
                title = title + "?";

                int    maxCount = 3;
                string infotext = "";
                for (int i = 0; i < paths.Count && i < maxCount; ++i)
                {
                    infotext += "   " + paths[i] + "\n";
                }
                if (paths.Count > maxCount)
                {
                    infotext += "   ...\n";
                }
                infotext += "\nYou cannot undo this action.";
                if (!EditorUtility.DisplayDialog(title, infotext, "Delete", "Cancel"))
                {
                    return(false);
                }
            }

            bool success = true;

            AssetDatabase.StartAssetEditing();
            foreach (string path in paths)
            {
                if (!AssetDatabase.MoveAssetToTrash(path))
                {
                    success = false;
                }
            }
            AssetDatabase.StopAssetEditing();

            return(success);
        }
        public override void FetchData()
        {
            // Create root Item
            int depth     = 0;
            var multiRoot = (m_Roots.Count > 1);

            if (multiRoot)
            {
                m_RootItem = new TreeViewItem(-1, depth, null, "Invisible Root Item");
                SetExpanded(m_RootItem, true);
            }
            else
            {
                var rootInstanceID = m_Roots[0].instanceID;
                var displayName    = m_Roots[0].displayName ?? CreateDisplayName(rootInstanceID);
                m_RootItem = new TreeViewItem(rootInstanceID, depth, null, displayName);
                SetExpanded(m_RootItem, true);
            }

            m_Rows = new List <TreeViewItem>(m_Roots.Count * 256);
            Texture2D emptyFolderIcon   = EditorGUIUtility.FindTexture(EditorResources.emptyFolderIconName);
            Texture2D folderIcon        = EditorGUIUtility.FindTexture(EditorResources.folderIconName);
            var       assetsInstanceIDs = AssetDatabase.GetMainAssetOrInProgressProxyInstanceID("Assets");
            var       projectPath       = Path.GetFileName(Directory.GetCurrentDirectory());

            // Fetch root Items
            m_RootsTreeViewItem = new Dictionary <string, TreeViewItem>(m_Roots.Count);
            foreach (var root in m_Roots)
            {
                var rootInstanceID = root.instanceID;
                var displayName    = root.displayName ?? CreateDisplayName(rootInstanceID);
                var rootPath       = root.path ?? AssetDatabase.GetAssetPath(rootInstanceID);

                var property = new HierarchyProperty(rootPath);
                if (!root.skipValidation && !property.Find(rootInstanceID, null))
                {
                    Debug.LogError("Root Asset with id " + rootInstanceID + " not valid!!");
                    continue;
                }

                var          minDepth = property.depth;
                var          subDepth = multiRoot ? 0 : -1;
                TreeViewItem rootItem;
                if (multiRoot)
                {
                    var parentItem = m_RootItem;
                    var rootDepth  = minDepth;
                    rootDepth++;

                    // Find parent treeView item
                    var parentPath = Directory.GetParent(rootPath).Name;
                    if (parentPath != projectPath)
                    {
                        if (!m_RootsTreeViewItem.TryGetValue(parentPath, out parentItem))
                        {
                            Debug.LogError("Cannot find parent for " + rootInstanceID);
                            continue;
                        }

                        rootDepth++;
                        subDepth++;
                    }

                    // Create root item TreeView item
                    if (subDepth > 0)
                    {
                        rootItem = new FolderTreeItem(rootInstanceID, rootDepth, parentItem, displayName);
                    }
                    else
                    {
                        rootItem = new RootTreeItem(rootInstanceID, rootDepth, parentItem, displayName);
                    }
                    rootItem.icon = folderIcon;
                    parentItem.AddChild(rootItem);
                }
                else
                {
                    rootItem = m_RootItem;
                }

                m_RootsTreeViewItem[rootPath] = rootItem;

                var  expandIDs      = GetExpandedIDs();
                var  rows           = new List <TreeViewItem>();
                bool shouldExpandIt = m_ExpandAtFirstTime && (rootItem.id == assetsInstanceIDs);
                if (IsExpanded(rootItem.id) && (rootItem == m_RootItem || IsExpanded(rootItem.parent.id)) || shouldExpandIt)
                {
                    m_ExpandAtFirstTime = false;

                    while (property.NextWithDepthCheck(expandIDs, minDepth))
                    {
                        if (!foldersOnly || property.isFolder)
                        {
                            depth = property.depth - minDepth;
                            TreeViewItem item;
                            if (property.isFolder)
                            {
                                item = new FolderTreeItem(property.instanceID, depth + subDepth, null, property.name);
                            }
                            else
                            {
                                item = new NonFolderTreeItem(property.instanceID, depth + subDepth, null, property.name);
                            }

                            if (property.isFolder && !property.hasChildren)
                            {
                                item.icon = emptyFolderIcon;
                            }
                            else
                            {
                                item.icon = property.icon;
                            }

                            if (property.hasChildren)
                            {
                                item.AddChild(null); // add a dummy child in children list to ensure we show the collapse arrow (because we do not fetch data for collapsed items)
                            }
                            rows.Add(item);
                        }
                    }

                    // Setup reference between child and parent items
                    TreeViewUtility.SetChildParentReferences(rows, rootItem);
                }
                else
                {
                    rootItem.AddChild(null);
                }

                if (shouldExpandIt && !IsExpanded(rootItem))
                {
                    SetExpanded(rootItem, true);
                }

                if (multiRoot && IsExpanded(rootItem.parent.id))
                {
                    m_Rows.Add(rootItem);
                }

                ((List <TreeViewItem>)m_Rows).AddRange(rows);
            }

            if (foldersFirst)
            {
                FoldersFirstRecursive(m_RootItem);
                m_Rows.Clear();
                GetVisibleItemsRecursive(m_RootItem, m_Rows);
            }

            // Must be called before InitSelection (it calls GetVisibleItems)
            m_NeedRefreshRows = false;

            // We want to reset selection on copy/duplication/delete
            bool frameLastSelected = false; // use false because we might just be expanding/collapsing a Item (which would prevent collapsing a Item with a selected child)

            m_TreeView.SetSelection(Selection.instanceIDs, frameLastSelected);
        }
示例#25
0
        public override void FetchData()
        {
            m_RootItem = new TreeViewItem(System.Int32.MaxValue, 0, null, "Invisible Root Item");
            SetExpanded(m_RootItem, true); // ensure always visible

            // We want three roots: Favorites, Assets, and Saved Filters
            List <TreeViewItem> visibleRoots = new List <TreeViewItem>();

            // Fetch asset folders
            int          assetsFolderInstanceID = GetAssetsFolderInstanceID();
            int          depth         = 0;
            string       displayName   = "Assets"; //CreateDisplayName (assetsFolderInstanceID);
            TreeViewItem assetRootItem = new TreeViewItem(assetsFolderInstanceID, depth, m_RootItem, displayName);

            ReadAssetDatabase("Assets", assetRootItem, depth + 1);

            TreeViewItem packagesRootItem = new TreeViewItem(0, depth, m_RootItem, "Packages");

            depth++;

            Texture2D folderIcon      = EditorGUIUtility.FindTexture(EditorResources.folderIconName);
            Texture2D emptyFolderIcon = EditorGUIUtility.FindTexture(EditorResources.emptyFolderIconName);

            packagesRootItem.icon = emptyFolderIcon;

            var packages = PackageManager.Packages.GetAll();

            foreach (var package in packages)
            {
                if (package.source == PackageManager.PackageSource.BuiltIn)
                {
                    continue;
                }

                var packagePath             = Path.Combine(PackageManager.Folders.GetPackagesMountPoint(), package.name);
                var packageFolderInstanceId = AssetDatabase.GetMainAssetOrInProgressProxyInstanceID(packagePath);

                displayName = !string.IsNullOrEmpty(package.displayName) ? package.displayName : CultureInfo.InvariantCulture.TextInfo.ToTitleCase(package.name.Replace("com.unity.", ""));
                TreeViewItem packageItem = new TreeViewItem(packageFolderInstanceId, depth, null, displayName);
                packagesRootItem.AddChild(packageItem);
                ReadAssetDatabase(packagePath, packageItem, depth + 1);
                packagesRootItem.icon = folderIcon;
            }

            // Fetch saved filters
            TreeViewItem savedFiltersRootItem = SavedSearchFilters.ConvertToTreeView();

            savedFiltersRootItem.parent = m_RootItem;

            // Order
            visibleRoots.Add(savedFiltersRootItem);
            visibleRoots.Add(assetRootItem);

            if (packagesRootItem != null)
            {
                visibleRoots.Add(packagesRootItem);
            }

            m_RootItem.children = visibleRoots;

            // Get global expanded state of roots
            foreach (TreeViewItem item in m_RootItem.children)
            {
                bool expanded = EditorPrefs.GetBool(kProjectBrowserString + item.displayName, true);
                SetExpanded(item, expanded);
            }

            m_NeedRefreshRows = true;
        }