public void AddAssetToFolder(AssetInfo assetInfo, AssetFolderInfo folder)
        {
            if (folder.Assets == null)
            {
                folder.Assets = new List <AssetInfo>();
            }

            if (assetInfo.Folder != null)
            {
                if (!m_folders.Contains(folder))
                {
                    TreeView.treeModel.RemoveElements(new[] { assetInfo.id });
                }
                else if (TreeView.treeModel.Find(assetInfo.id) == null)
                {
                    AssetInfo parent = (AssetInfo)assetInfo.parent;
                    if (parent == null)
                    {
                        parent = TreeView.treeModel.root;
                    }

                    TreeView.treeModel.AddElement(assetInfo, parent, parent.hasChildren ? parent.children.Count : 0);
                }
                assetInfo.Folder.Assets.Remove(assetInfo);
            }

            assetInfo.Folder = folder;
            folder.Assets.Add(assetInfo);
        }
        private void RemoveFromFolder(AssetInfo assetInfo, AssetFolderInfo folder)
        {
            List <AssetInfo> assetsToRemove = new List <AssetInfo>();

            FlattenItems(assetInfo, assetsToRemove);
            DoRemove(assetsToRemove.ToArray());
        }
        private bool MoveToNewLocationDialog(UnityObject[] assets, AssetFolderInfo folder)
        {
            bool moveToNewLocation   = true;
            bool moveDialogDisplayed = false;

            foreach (UnityObject asset in assets)
            {
                if (!moveDialogDisplayed)
                {
                    AssetFolderInfo existingFolder;
                    AssetInfo       existingAsset;
                    if (m_asset.AssetLibrary.TryGetAssetInfo(asset, out existingFolder, out existingAsset))
                    {
                        if (existingFolder != folder)
                        {
                            moveToNewLocation = EditorUtility.DisplayDialog(
                                "Same asset already added",
                                "Same asset already added to asset library. Do you want to move it to new location?", "Yes", "No");
                            moveDialogDisplayed = true;
                        }
                    }
                }
            }

            return(moveToNewLocation);
        }
Пример #4
0
        private bool Foreach(AssetFolderInfo folder, Func <AssetInfo, bool> callback)
        {
            if (folder.Assets != null)
            {
                for (int i = 0; i < folder.Assets.Count; ++i)
                {
                    AssetInfo assetInfo = folder.Assets[i];
                    if (assetInfo != null)
                    {
                        if (!callback(assetInfo))
                        {
                            return(false);
                        }
                    }
                }
            }

            if (folder.hasChildren)
            {
                for (int i = 0; i < folder.children.Count; ++i)
                {
                    AssetFolderInfo childFolder = folder.children[i] as AssetFolderInfo;
                    if (childFolder != null)
                    {
                        if (!Foreach(childFolder, callback))
                        {
                            return(false);
                        }
                    }
                }
            }

            return(true);
        }
Пример #5
0
        public void LoadIDMappingTo(MappingInfo mapping, bool IIDtoPID, bool PIDtoObj)
        {
            if (!IIDtoPID && !PIDtoObj)
            {
                return;
            }

            if (m_assetLibrary == null || m_assetLibrary.Folders == null || m_assetLibrary.Folders.Count == 0)
            {
                return;
            }

            List <int> instanceIDs   = new List <int>();
            List <int> persistentIDs = new List <int>();

            for (int i = 0; i < m_assetLibrary.Folders.Count; ++i)
            {
                AssetFolderInfo folder = m_assetLibrary.Folders[i];
                if (folder != null)
                {
                    if (folder.Assets != null && folder.Assets.Count > 0)
                    {
                        for (int j = 0; j < folder.Assets.Count; ++j)
                        {
                            AssetInfo asset = folder.Assets[j];
                            LoadIDMappingTo(asset, mapping, instanceIDs, persistentIDs, IIDtoPID, PIDtoObj);
                        }
                    }
                }
            }

            mapping.Add(this, instanceIDs, persistentIDs);
        }
Пример #6
0
        private int BuildSubtree(AssetFolderInfo parent, int startIndex)
        {
            parent.children = new List <TreeElement>();
            for (int i = startIndex; i < Folders.Count; ++i)
            {
                AssetFolderInfo folder = Folders[i];
                if (folder == null)
                {
                    continue;
                }

                if (folder.depth == parent.depth + 1)
                {
                    parent.children.Add(folder);
                }
                else if (folder.depth == parent.depth + 2)
                {
                    i = BuildSubtree(Folders[i - 1], i);
                }
                else if (folder.depth > parent.depth + 2)
                {
                    throw new InvalidOperationException("Unable to build AssetLibraryInfo tree -> folder.depth > parent.depth + 2");
                }
                else
                {
                    return(i - 1);
                }
            }

            return(Folders.Count);
        }
Пример #7
0
        private bool TryGetAssetInfo(AssetFolderInfo folder, UnityObject obj, out AssetFolderInfo resultFolder, out AssetInfo resultAsset)
        {
            if (folder.Assets != null)
            {
                for (int i = 0; i < folder.Assets.Count; ++i)
                {
                    AssetInfo asset = folder.Assets[i];
                    if (asset.Object == obj)
                    {
                        resultFolder = folder;
                        resultAsset  = asset;
                        return(true);
                    }
                }
            }

            if (folder.hasChildren)
            {
                for (int i = 0; i < folder.children.Count; ++i)
                {
                    AssetFolderInfo subfolder = (AssetFolderInfo)folder.children[i];
                    if (TryGetAssetInfo(subfolder, obj, out resultFolder, out resultAsset))
                    {
                        return(true);
                    }
                }
            }

            resultAsset  = null;
            resultFolder = null;
            return(false);
        }
Пример #8
0
        private void MergeFolders(AssetFolderInfo from, ProjectItem to, int ordinal)
        {
            if (from.hasChildren)
            {
                for (int i = 0; i < from.children.Count; ++i)
                {
                    AssetFolderInfo childFrom = (AssetFolderInfo)from.children[i];

                    if (to.Children == null)
                    {
                        to.Children = new List <ProjectItem>();
                    }

                    ProjectItem childTo = to.Children.Where(item => item.Name == childFrom.name && !(item is AssetItem)).FirstOrDefault();
                    if (childTo == null)
                    {
                        childTo = new ProjectItem();
                        to.Children.Add(childTo);
                    }
                    childTo.Name   = childFrom.name;
                    childTo.ItemID = m_assetDB.ToExposedFolderID(ordinal, childFrom.id);
                    childTo.Parent = to;

                    MergeFolders(childFrom, childTo, ordinal);
                }
            }

            MergeAssets(from, to, ordinal);
        }
Пример #9
0
        private void Awake()
        {
            if (m_assetLibrary == null || m_assetLibrary.Folders.Count == 0)
            {
                AssetFolderInfo rootFolder = new AssetFolderInfo
                {
                    name  = "Root",
                    depth = -1,
                };

                AssetFolderInfo assetsFolder = new AssetFolderInfo("Assets", 0, 3);
                assetsFolder.IsEnabled = true;

                m_assetLibrary = new AssetLibraryInfo
                {
                    name    = "Root",
                    depth   = -1,
                    Folders = new List <AssetFolderInfo>
                    {
                        rootFolder,
                        assetsFolder
                    },
                };
            }
        }
Пример #10
0
        private void MergeAssets(AssetFolderInfo from, ProjectItem to, int ordinal)
        {
            List <AssetInfo> fromAssets = from.Assets;

            if (fromAssets == null)
            {
                return;
            }

            if (to.Children == null)
            {
                to.Children = new List <ProjectItem>();
            }

            for (int i = 0; i < fromAssets.Count; ++i)
            {
                AssetInfo assetFrom = fromAssets[i];
                if (assetFrom.Object == null)
                {
                    continue;
                }
                AssetItem assetTo = to.Children.OfType <AssetItem>().Where(item => item.Name == assetFrom.name).FirstOrDefault();
                if (assetTo == null)
                {
                    assetTo = new AssetItem();
                    to.Children.Add(assetTo);
                }

                assetTo.Name     = assetFrom.name;
                assetTo.ItemID   = m_assetDB.ToExposedResourceID(ordinal, assetFrom.PersistentID);
                assetTo.Parent   = to;
                assetTo.TypeGuid = m_typeMap.ToGuid(assetFrom.Object.GetType());
                assetTo.Preview  = null; //must rebuild preview

                if (assetFrom.PrefabParts != null)
                {
                    assetTo.Parts = new PrefabPart[assetFrom.PrefabParts.Count];
                    for (int j = 0; j < assetFrom.PrefabParts.Count; ++j)
                    {
                        PrefabPartInfo prefabPart = assetFrom.PrefabParts[j];
                        Guid           typeGuid   = m_typeMap.ToGuid(prefabPart.Object.GetType());
                        if (prefabPart != null && prefabPart.Object != null && typeGuid != Guid.Empty)
                        {
                            assetTo.Parts[j] = new PrefabPart
                            {
                                Name     = prefabPart.Object.name,
                                ParentID = prefabPart.ParentPersistentID,
                                PartID   = prefabPart.PersistentID,
                                TypeGuid = typeGuid
                            };
                        }
                        else
                        {
                            assetTo.Parts[j] = null;
                        }
                    }
                }
            }
        }
Пример #11
0
        public void BuildTree()
        {
            if (Folders == null || Folders.Count == 0)
            {
                return;
            }
            AssetFolderInfo root = Folders[0];

            if (root.depth != -1)
            {
                throw new InvalidOperationException("Unable to build AssetLibraryInfo tree -> root.depth != -1");
            }
            BuildSubtree(root, 1);
        }
Пример #12
0
 public bool TryGetAssetInfo(UnityObject obj, out AssetFolderInfo resultFolder, out AssetInfo resultAsset)
 {
     for (int i = 0; i < Folders.Count; ++i)
     {
         AssetFolderInfo folder = Folders[i];
         if (folder != null && TryGetAssetInfo(folder, obj, out resultFolder, out resultAsset))
         {
             return(true);
         }
     }
     resultAsset  = null;
     resultFolder = null;
     return(false);
 }
Пример #13
0
        private AssetFolderInfo CopyFolder(string path, TreeViewItem parent, int insertIndex)
        {
            string          lastFolderName = Path.GetFileName(path);
            AssetFolderInfo parentData     = GetAssetFolderInfo(parent);
            AssetFolderInfo folder         = CreateFolder(lastFolderName, parentData,
                                                          insertIndex == -1 ?
                                                          parentData.hasChildren ?
                                                          parentData.children.Count
                        : 0
                    : insertIndex);

            if (folder == null)
            {
                return(null);
            }

            TreeViewItem folderTreeViewItem = TreeView.FindItem(folder.id);

            string[] subfolders = AssetDatabase.GetSubFolders(path);

            string[] assetGuids = AssetDatabase.FindAssets("", new[] { path }).Distinct().ToArray();

            List <UnityObject> assets = new List <UnityObject>();

            foreach (string assetGuid in assetGuids)
            {
                string assetPath = AssetDatabase.GUIDToAssetPath(assetGuid);
                if (path == Path.GetDirectoryName(assetPath))
                {
                    UnityObject asset = AssetDatabase.LoadAssetAtPath(assetPath, typeof(UnityObject));
                    assets.Add(asset);
                }
            }

            UnityObject[] assetsArray = assets.ToArray();
            if (assetsArray.Length > 0)
            {
                MoveToNewLocationDialog(assetsArray);
                m_assetsGUI.InitIfNeeded();
                m_assetsGUI.AddAssetToFolder(assetsArray, folder, m_moveToNewLocation);
            }

            for (int i = 0; i < subfolders.Length; ++i)
            {
                CopyFolder(subfolders[i], folderTreeViewItem, i);
            }

            return(folder);
        }
        public AssetLibraryInfo CloneVisible()
        {
            AssetLibraryInfo proxy = new AssetLibraryInfo();

            proxy.Identity       = Identity;
            proxy.FolderIdentity = FolderIdentity;
            proxy.name           = name;
            if (Folders == null)
            {
                return(proxy);
            }
            proxy.Folders = new List <AssetFolderInfo>();
            for (int i = 0; i < Folders.Count; ++i)
            {
                AssetFolderInfo folder  = Folders[i];
                bool            include = false;
                if (folder != null)
                {
                    if (folder.depth == -1)
                    {
                        include = true;
                    }
                    else
                    {
                        if (folder.IsEnabled)
                        {
                            AssetFolderInfo parent = (AssetFolderInfo)folder.parent;
                            if (parent != null)
                            {
                                if (parent.IsEnabled || parent.depth == -1)
                                {
                                    include = true;
                                }
                            }
                        }
                    }
                }

                if (include)
                {
                    AssetFolderInfo proxyFolder = new AssetFolderInfo();
                    proxyFolder.IsEnabled = true;
                    CopyFolder(folder, proxyFolder);
                    proxy.Folders.Add(proxyFolder);
                }
            }
            return(proxy);
        }
Пример #15
0
        public bool Foreach(Func <AssetInfo, bool> callback)
        {
            if (m_assetLibrary.Folders != null)
            {
                for (int i = 0; i < m_assetLibrary.Folders.Count; ++i)
                {
                    AssetFolderInfo folder = m_assetLibrary.Folders[i];
                    if (!Foreach(folder, callback))
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Пример #16
0
        private void CreateFolder()
        {
            Undo.RecordObject(m_asset, "Create Asset Folder");

            TreeView.EndRename();
            var             selection = TreeView.GetSelection();
            TreeElement     parent    = (selection.Count == 1 ? TreeView.treeModel.Find(selection[0]) : null) ?? TreeView.treeModel.root;
            AssetFolderInfo folder    = CreateFolder("Folder", parent, 0);

            if (folder == null)
            {
                return;
            }
            // Select newly created element
            TreeView.SetSelection(new[] { folder.id }, TreeViewSelectionOptions.RevealAndFrame);
            TreeView.BeginRename(folder.id);
        }
Пример #17
0
        private AssetFolderInfo CreateFolder(string name, TreeElement parent, int insertPosition)
        {
            int depth = parent != null ? parent.depth + 1 : 0;

            if (m_asset.AssetLibrary.FolderIdentity >= AssetLibraryInfo.MAX_FOLDERS)
            {
                EditorUtility.DisplayDialog("Unable to add folder", string.Format("Max 'FolderIndentity' value reached. 'FolderIndentity' ==  {0}", AssetLibraryInfo.MAX_FOLDERS), "OK");
                return(null);
            }
            int id      = m_asset.AssetLibrary.FolderIdentity++;
            var element = new AssetFolderInfo(name, depth, id);

            TreeView.treeModel.AddElement(element, parent, insertPosition);

            TreeView.SetSelection(new[] { element.id }, TreeViewSelectionOptions.RevealAndFrame);

            return(element);
        }
        private void CopyFolder(AssetFolderInfo sourceFolder, AssetFolderInfo targetFolder)
        {
            targetFolder.id    = sourceFolder.id;
            targetFolder.name  = sourceFolder.name;
            targetFolder.depth = sourceFolder.depth;

            if (sourceFolder.Assets != null)
            {
                targetFolder.Assets = new List <AssetInfo>();
                for (int i = 0; i < sourceFolder.Assets.Count; ++i)
                {
                    AssetInfo sourceAsset = sourceFolder.Assets[i];
                    if (sourceAsset.IsEnabled)
                    {
                        AssetInfo targetAsset = new AssetInfo();
                        targetAsset.depth = sourceAsset.depth;
                        targetAsset.id    = sourceAsset.id;
                        targetAsset.name  = sourceAsset.name;

                        targetAsset.IsEnabled = sourceAsset.IsEnabled;
                        targetAsset.Object    = sourceAsset.Object;

                        if (sourceAsset.PrefabParts != null)
                        {
                            targetAsset.PrefabParts = new List <PrefabPartInfo>();
                            for (int j = 0; j < sourceAsset.PrefabParts.Count; ++j)
                            {
                                PrefabPartInfo sourcePart = sourceAsset.PrefabParts[j];

                                PrefabPartInfo targetPart = new PrefabPartInfo();
                                targetPart.Depth              = sourcePart.Depth;
                                targetPart.Object             = sourcePart.Object;
                                targetPart.ParentPersistentID = sourcePart.ParentPersistentID;
                                targetPart.PersistentID       = sourcePart.PersistentID;
                                targetAsset.PrefabParts.Add(targetPart);
                            }
                        }

                        targetFolder.Assets.Add(targetAsset);
                    }
                }
            }
        }
Пример #19
0
        private void MergeAssetLibrary(AssetLibraryReference asset, int ordinal)
        {
            if (!asset.KeepRuntimeProjectInSync)
            {
                return;
            }

            AssetLibraryInfo assetLibrary = asset.AssetLibrary;

            if (assetLibrary == null)
            {
                return;
            }

            assetLibrary.BuildTree();

            AssetFolderInfo rootFolder = assetLibrary.Folders.Where(folder => folder.depth == -1).First();

            MergeFolders(rootFolder, m_root, ordinal);
        }
        private void Awake()
        {
            if (m_assetLibrary == null || m_assetLibrary.Folders.Count == 0)
            {
                AssetFolderInfo assetsFolder = new AssetFolderInfo
                {
                    name  = "Assets",
                    depth = -1,
                };

                m_assetLibrary = new AssetLibraryInfo
                {
                    name    = "Root",
                    depth   = -1,
                    Folders = new List <AssetFolderInfo>
                    {
                        assetsFolder
                    }
                };
            }
        }
        private DragAndDropVisualMode PerformDrop(TreeViewItem parent, int insertIndex)
        {
            DragAndDrop.AcceptDrag();

            List <UnityObject> assets = new List <UnityObject>();

            foreach (UnityObject dragged_object in DragAndDrop.objectReferences)
            {
                string path = AssetDatabase.GetAssetPath(dragged_object);
                if (!string.IsNullOrEmpty(path) && File.Exists(path))
                {
                    assets.Add(dragged_object);
                }
            }


            AssetFolderInfo folder = m_folders[0];

            UnityObject[] assetsArray       = assets.ToArray();
            bool          moveToNewLocation = MoveToNewLocationDialog(assetsArray, folder);

            AddAssetToFolder(parent, insertIndex, assetsArray, folder, moveToNewLocation);
            return(DragAndDropVisualMode.Copy);
        }
Пример #22
0
        private static HashSet <UnityObject> ReadFromAssetLibraries(Scene scene, out int index, out AssetLibraryAsset asset, out AssetFolderInfo folder)
        {
            HashSet <UnityObject> hs = new HashSet <UnityObject>();

            string[] guids = AssetDatabase.FindAssets("", new[] { "Assets/" + BHPath.Root + "/RTSaveLoad2/Resources_Auto/Resources/" + scene.name });

            List <AssetLibraryAsset> assetLibraries = new List <AssetLibraryAsset>();

            foreach (string guid in guids)
            {
                string path = AssetDatabase.GUIDToAssetPath(guid);

                AssetLibraryAsset assetLibrary = AssetDatabase.LoadAssetAtPath <AssetLibraryAsset>(path);
                if (assetLibrary != null)
                {
                    assetLibrary.Foreach(assetInfo =>
                    {
                        if (assetInfo.Object != null)
                        {
                            if (!hs.Contains(assetInfo.Object))
                            {
                                hs.Add(assetInfo.Object);
                            }

                            if (assetInfo.PrefabParts != null)
                            {
                                foreach (PrefabPartInfo prefabPart in assetInfo.PrefabParts)
                                {
                                    if (prefabPart.Object != null)
                                    {
                                        if (!hs.Contains(prefabPart.Object))
                                        {
                                            hs.Add(prefabPart.Object);
                                        }
                                    }
                                }
                            }
                        }
                        return(true);
                    });

                    assetLibraries.Add(assetLibrary);
                }
            }

            if (assetLibraries.Count == 0)
            {
                asset = ScriptableObject.CreateInstance <AssetLibraryAsset>();
                index = 0;
            }
            else
            {
                asset = assetLibraries.OrderBy(a => a.AssetLibrary.Identity).FirstOrDefault();
                index = assetLibraries.Count - 1;
            }

            folder = asset.AssetLibrary.Folders.Where(f => f.depth == 0).First();
            if (folder.Assets == null)
            {
                folder.Assets = new List <AssetInfo>();
            }
            return(hs);
        }
Пример #23
0
        private void CreateAsset(UnityObject obj, AssetInfo parentAssetInfo, int insertIndex = -1, AssetFolderInfo folder = null)
        {
            AssetInfo assetInfo = CreateAsset(obj.name, parentAssetInfo, insertIndex, folder);

            assetInfo.Object = obj;
            AddAssetToFolder(assetInfo, folder);
            if (obj is GameObject)
            {
                GameObject go = (GameObject)obj;
                assetInfo.PrefabParts = new List <PrefabPartInfo>();
                CreatePefabParts(assetInfo, go);

                if (folder != null)
                {
                    if (IsFolderSelected(folder))
                    {
                        if (assetInfo.PrefabParts != null)
                        {
                            Dictionary <int, AssetInfo> assets = new Dictionary <int, AssetInfo>();
                            assets.Add(-1, assetInfo);

                            for (int i = 0; i < assetInfo.PrefabParts.Count; ++i)
                            {
                                PrefabPartInfo prefabPart = assetInfo.PrefabParts[i];
                                string         name;
                                if (prefabPart.Object == null)
                                {
                                    name = "<Null>";
                                }
                                else
                                {
                                    if (prefabPart.Object is Component)
                                    {
                                        name = prefabPart.Object.GetType().Name;
                                    }
                                    else
                                    {
                                        name = prefabPart.Object.name;
                                    }
                                }

                                AssetInfo prefabPartAssetInfo = new AssetInfo(name, assetInfo.depth + prefabPart.Depth, prefabPart.PersistentID);
                                prefabPartAssetInfo.Object = prefabPart.Object;

                                assets.Add(prefabPartAssetInfo.id, prefabPartAssetInfo);

                                TreeElement parent = assets[prefabPart.ParentPersistentID];
                                TreeView.treeModel.AddElement(prefabPartAssetInfo, parent, parent.children != null ? parent.children.Count : 0);
                            }
                        }
                    }
                }
            }
        }
        private AssetInfo CreateAsset(string name, TreeElement parent, int insertIndex = -1, AssetFolderInfo folder = null)
        {
            int depth = parent != null ? parent.depth + 1 : 0;
            int id    = m_asset.AssetLibrary.Identity;

            m_asset.AssetLibrary.Identity++;
            var assetInfo = new AssetInfo(name, depth, id);

            if (folder != null)
            {
                if (IsFolderSelected(folder))
                {
                    TreeView.treeModel.AddElement(assetInfo, parent, insertIndex == -1 ?
                                                  parent.hasChildren ?
                                                  parent.children.Count
                        : 0
                        : insertIndex);

                    // Select newly created element

                    if (depth == 0)
                    {
                        TreeView.SetSelection(new[] { id }, TreeViewSelectionOptions.RevealAndFrame);
                    }
                }
            }

            return(assetInfo);
        }
Пример #25
0
        private void BuildTree(ProjectItem projectItem, AssetFolderInfo folder, int ordinal)
        {
            projectItem.Name = folder.name;

            if (folder.hasChildren)
            {
                projectItem.Children = new List <ProjectItem>();
                for (int i = 0; i < folder.children.Count; ++i)
                {
                    ProjectItem child = new ProjectItem();
                    projectItem.AddChild(child);
                    BuildTree(child, (AssetFolderInfo)folder.children[i], ordinal);
                }
            }

            if (folder.Assets != null && folder.Assets.Count > 0)
            {
                if (projectItem.Children == null)
                {
                    projectItem.Children = new List <ProjectItem>();
                }

                List <string> existingNames = new List <string>();
                for (int i = 0; i < folder.Assets.Count; ++i)
                {
                    AssetInfo assetInfo = folder.Assets[i];
                    if (assetInfo.Object != null)
                    {
                        string ext = GetExt(assetInfo.Object);

                        ImportItem importItem = new ImportItem
                        {
                            Name     = PathHelper.GetUniqueName(assetInfo.name, ext, existingNames),
                            Ext      = ext,
                            Object   = assetInfo.Object,
                            TypeGuid = m_typeMap.ToGuid(assetInfo.Object.GetType()),
                            ItemID   = m_assetDB.ToStaticResourceID(ordinal, assetInfo.PersistentID)
                        };

                        if (assetInfo.PrefabParts != null)
                        {
                            List <PrefabPart> parts = new List <PrefabPart>();
                            for (int j = 0; j < assetInfo.PrefabParts.Count; ++j)
                            {
                                PrefabPartInfo partInfo = assetInfo.PrefabParts[j];
                                if (partInfo.Object != null)
                                {
                                    PrefabPart part = new PrefabPart
                                    {
                                        Name     = partInfo.Object.name,
                                        PartID   = m_assetDB.ToStaticResourceID(ordinal, partInfo.PersistentID),
                                        ParentID = m_assetDB.ToStaticResourceID(ordinal, partInfo.PersistentID),
                                        TypeGuid = m_typeMap.ToGuid(partInfo.Object.GetType()),
                                    };

                                    parts.Add(part);
                                }
                            }
                            importItem.Parts = parts.ToArray();
                        }

                        projectItem.AddChild(importItem);
                        existingNames.Add(importItem.NameExt);
                    }
                }
            }
        }
 public bool IsFolderSelected(AssetFolderInfo folder)
 {
     return(m_folders != null && m_folders.Contains(folder));
 }
        private bool CreateAsset(UnityObject obj, AssetInfo parentAssetInfo, int insertIndex = -1, AssetFolderInfo folder = null)
        {
            if (obj is GameObject)
            {
                GameObject go = (GameObject)obj;

                int identity = m_asset.AssetLibrary.Identity + 1;
                List <PrefabPartInfo> prefabParts = new List <PrefabPartInfo>();
                CreatePefabParts(go, ref identity, prefabParts);
                if (identity >= AssetLibraryInfo.MAX_ASSETS)
                {
                    return(false);
                }

                AssetInfo assetInfo = CreateAsset(obj.name, parentAssetInfo, insertIndex, folder);
                assetInfo.Object = obj;
                AddAssetToFolder(assetInfo, folder);

                assetInfo.PrefabParts         = prefabParts;
                m_asset.AssetLibrary.Identity = identity;

                if (folder != null)
                {
                    if (IsFolderSelected(folder))
                    {
                        if (assetInfo.PrefabParts != null)
                        {
                            Dictionary <int, AssetInfo> assets = new Dictionary <int, AssetInfo>();
                            assets.Add(-1, assetInfo);

                            for (int i = 0; i < assetInfo.PrefabParts.Count; ++i)
                            {
                                PrefabPartInfo prefabPart = assetInfo.PrefabParts[i];
                                string         name;
                                if (prefabPart.Object == null)
                                {
                                    name = "<Null>";
                                }
                                else
                                {
                                    if (prefabPart.Object is Component)
                                    {
                                        name = prefabPart.Object.GetType().Name;
                                    }
                                    else
                                    {
                                        name = prefabPart.Object.name;
                                    }
                                }

                                AssetInfo prefabPartAssetInfo = new AssetInfo(name, assetInfo.depth + prefabPart.Depth, prefabPart.PersistentID);
                                prefabPartAssetInfo.Object = prefabPart.Object;

                                assets.Add(prefabPartAssetInfo.id, prefabPartAssetInfo);

                                TreeElement parent = assets[prefabPart.ParentPersistentID];
                                TreeView.treeModel.AddElement(prefabPartAssetInfo, parent, parent.children != null ? parent.children.Count : 0);
                            }
                        }
                    }
                }
            }
            else
            {
                AssetInfo assetInfo = CreateAsset(obj.name, parentAssetInfo, insertIndex, folder);
                assetInfo.Object = obj;
                AddAssetToFolder(assetInfo, folder);
            }
            return(true);
        }
        private void AddAssetToFolder(TreeViewItem parent, int insertIndex, UnityObject[] objects, AssetFolderInfo folder, bool moveToNewLocation)
        {
            for (int i = 0; i < objects.Length; ++i)
            {
                UnityObject obj = objects[i];
                if (obj == null || obj.GetType().Assembly.FullName.Contains("UnityEditor"))
                {
                    continue;
                }
                AssetInfo       parentAssetInfo = GetAssetInfo(parent);
                AssetInfo       assetInfo;
                AssetFolderInfo existingFolder;
                AssetInfo       existingAsset;
                if (m_asset.AssetLibrary.TryGetAssetInfo(obj, out existingFolder, out existingAsset))
                {
                    assetInfo = existingAsset;
                    if (!moveToNewLocation)
                    {
                        continue;
                    }

                    AddAssetToFolder(assetInfo, folder);

                    TreeView.treeModel.SetData(GetData());
                    TreeView.Reload();
                }
                else
                {
                    if (m_asset.AssetLibrary.Identity >= AssetLibraryInfo.MAX_ASSETS)
                    {
                        EditorUtility.DisplayDialog("Unable to add asset", string.Format("Max 'Indentity' value reached. 'Identity' ==  {0}", AssetLibraryInfo.MAX_ASSETS), "OK");
                        return;
                    }

                    if (!CreateAsset(obj, parentAssetInfo, insertIndex, folder))
                    {
                        EditorUtility.DisplayDialog("Unable to add asset", string.Format("Max 'Indentity' value reached. 'Identity' ==  {0}", AssetLibraryInfo.MAX_ASSETS), "OK");
                    }
                }
            }
        }
 public void AddAssetToFolder(UnityObject[] objects, AssetFolderInfo folder, bool moveToNewLocation)
 {
     AddAssetToFolder(null, -1, objects, folder, moveToNewLocation);
 }
Пример #30
0
        private static void CreateAssetLibraryFromScene(Scene scene, int index, AssetLibraryAsset asset, AssetFolderInfo folder, HashSet <UnityObject> hs)
        {
            TypeMap typeMap = new TypeMap();
            AssetDB assetDB = new AssetDB();

            IOC.Register <ITypeMap>(typeMap);
            IOC.Register <IAssetDB>(assetDB);

            PersistentRuntimeScene rtScene = new PersistentRuntimeScene();

            GetDepsFromContext ctx = new GetDepsFromContext();

            rtScene.GetDepsFrom(scene, ctx);

            IOC.Unregister <ITypeMap>(typeMap);
            IOC.Unregister <IAssetDB>(assetDB);

            int identity = asset.AssetLibrary.Identity;

            foreach (UnityObject obj in ctx.Dependencies)
            {
                if (hs.Contains(obj))
                {
                    continue;
                }

                if (obj is GameObject)
                {
                    GameObject go = (GameObject)obj;
                    if (go.IsPrefab())
                    {
                        AssetInfo assetInfo = new AssetInfo(go.name, 0, identity);
                        assetInfo.Object = go;
                        identity++;

                        List <PrefabPartInfo> prefabParts = new List <PrefabPartInfo>();
                        AssetLibraryAssetsGUI.CreatePefabParts(go, ref identity, prefabParts);
                        if (prefabParts.Count >= AssetLibraryInfo.MAX_ASSETS - AssetLibraryInfo.INITIAL_ID)
                        {
                            EditorUtility.DisplayDialog("Unable Create AssetLibrary", string.Format("Max 'Indentity' value reached. 'Identity' ==  {0}", AssetLibraryInfo.MAX_ASSETS), "OK");
                            return;
                        }

                        if (identity >= AssetLibraryInfo.MAX_ASSETS)
                        {
                            SaveAssetLibrary(asset, scene, index);
                            index++;

                            asset  = ScriptableObject.CreateInstance <AssetLibraryAsset>();
                            folder = asset.AssetLibrary.Folders.Where(f => f.depth == 0).First();

                            identity = asset.AssetLibrary.Identity;
                        }

                        assetInfo.PrefabParts       = prefabParts;
                        asset.AssetLibrary.Identity = identity;
                        folder.Assets.Add(assetInfo);
                    }
                }
                else
                {
                    AssetInfo assetInfo = new AssetInfo(obj.name, 0, identity);
                    assetInfo.Object = obj;
                    identity++;

                    if (identity >= AssetLibraryInfo.MAX_ASSETS)
                    {
                        SaveAssetLibrary(asset, scene, index);
                        index++;

                        asset    = ScriptableObject.CreateInstance <AssetLibraryAsset>();
                        folder   = asset.AssetLibrary.Folders.Where(f => f.depth == 0).First();
                        identity = asset.AssetLibrary.Identity;
                    }

                    asset.AssetLibrary.Identity = identity;
                    folder.Assets.Add(assetInfo);
                }
            }

            SaveAssetLibrary(asset, scene, index);
            index++;

            Selection.activeObject = asset;
            EditorGUIUtility.PingObject(asset);
        }