void AddDraggedAssetToPack(PackSettings.Node startingNode) { Undo.RecordObject(m_EditedPack, "Added asset to pack"); foreach (var path in DragAndDrop.paths) { string fullPath = path.Replace("Assets", Application.dataPath).Replace('\\', '/'); string parentPath = fullPath.Substring(0, fullPath.LastIndexOf('/')); string[] allFiles = null; if (File.GetAttributes(fullPath).HasFlag(FileAttributes.Directory)) { allFiles = Directory.GetFiles(fullPath, "*.*", SearchOption.AllDirectories); } else { allFiles = new string[] { fullPath } }; foreach (var file in allFiles) { if (file.Contains(".meta")) { continue; } string shortPath = file.Replace(parentPath, ""); var obj = AssetDatabase.LoadAssetAtPath <UnityEngine.Object>(file.Replace(Application.dataPath, "Assets")); m_EditedPack.InsertPathIntoTree(shortPath, obj, startingNode); } } Reload(); } void MoveDraggedObject(PackSettings.Node newParent) { Undo.RecordObject(m_EditedPack, "Move Object"); foreach (var entry in m_DraggedItem) { ArrayUtility.Remove(ref entry.nodeEntry.parent.children, entry.nodeEntry); entry.nodeEntry.parent = newParent; ArrayUtility.Add(ref entry.nodeEntry.parent.children, entry.nodeEntry); } Reload(); }
void CreateFolderInsideItem(object item) { PackTreeItem packItem = item as PackTreeItem; //TODO : check if a New Folder already exist under that parent to don't have 2 folder named the same PackSettings.Node n = new PackSettings.Node() { name = "New Folder", parent = packItem.nodeEntry }; ArrayUtility.Add(ref packItem.nodeEntry.children, n); Reload(); }
TreeViewItem RecursiveAddItem(PackSettings.Node node, int level) { //hashcode is not unique but that should be enough her, hopefully. Need to have consistent ID across reload //as otherwise selected thing get lost. TreeViewItem ret = new PackTreeItem() { id = node.id, displayName = node.name, nodeEntry = node, icon = s_Icons[node.obj == null ? 0 : 1] }; currentTopID += 1; for (int i = 0; i < node.children.Length; ++i) { ret.AddChild(RecursiveAddItem(node.children[i], level + 1)); } return(ret); }
protected override void ContextClicked() { if (m_EditedPack == null) { return; } GenericMenu menu = new GenericMenu(); menu.AddItem(new GUIContent("Create New Folder..."), false, () => { PackSettings.Node n = new PackSettings.Node() { name = "New Folder", parent = m_EditedPack.root }; ArrayUtility.Add(ref m_EditedPack.root.children, n); Reload(); }); menu.ShowAsContext(); Repaint(); }
//path part is optional, only used by the deserialization to keep id coherent across reload public void InsertPathIntoTree(string path, Object obj, Node startNode, FileEntry.PathPartData[] pathParts = null) { string[] pathBreak = path.Split(new char[] { '\\', '/' }, StringSplitOptions.RemoveEmptyEntries); var currentParent = startNode; //Don't go until the end, as the end is the file. for (int p = 0; p < pathBreak.Length - 1; ++p) { Node found = null; FileEntry.PathPartData part = null; if (pathParts != null && pathParts.Length > p) { part = pathParts[p]; } for (int i = 0; i < currentParent.children.Length; ++i) { var child = currentParent.children[i]; if (child.name == pathBreak[p]) { found = child; break; } } // if found, we make it the new level, otherwise we create a new children and set it as new level if (found != null) { currentParent = found; } else { Node node = new PackSettings.Node() { parent = currentParent, name = pathBreak[p] }; ArrayUtility.Add(ref currentParent.children, node); currentParent = node; } if (part != null) { currentParent.id = part.id; } } bool fileExist = false; //..but first check if the cart isn't already part of the children for (int i = 0; i < currentParent.children.Length; ++i) { if (currentParent.children[i].obj == obj) { fileExist = true; break; } } if (!fileExist) { Node objNode = new Node() { name = pathBreak[pathBreak.Length - 1], parent = currentParent, obj = obj }; if (pathParts != null) { objNode.id = pathParts[pathParts.Length - 1].id; } ArrayUtility.Add(ref currentParent.children, objNode); } }