Ejemplo n.º 1
0
        /// <summary>
        /// Callback that is invoked when external file(s) are
        /// dragged-and-dropped into the Project Browser.
        /// </summary>
        private static void HandleDragAndDrop(string targetPath, string[] droppedPaths)
        {
            // Hold the Control key or Command key while
            // dragging-and-dropping a .gltf/.glb/.zip to
            // copy the file into the project without
            // performing an automatic glTF import.

            if (Event.current.control || Event.current.command)
            {
                return;
            }

            // Read current import options from Piglet Options window.
            //
            // Note: The SaveAssets/Refresh calls ensure that any changes
            // made in the Piglet Options window are saved out to disk
            // first.

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();

            _importOptions = Resources.Load <ImportOptions>("ImportOptions");

            // Do nothing if drag-and-drop glTF import has been disabled by the user

            if (!_importOptions.EnableDragAndDropImport)
            {
                return;
            }

            // If `targetPath` is a regular file and not a directory, use
            // the parent directory as the import directory.

            string importDir = Directory.Exists(targetPath)
                ? targetPath : Path.GetDirectoryName(targetPath);

            importDir = UnityPathUtil.NormalizePathSeparators(importDir);

            // Exclude files that don't have .gltf/.glb extension.
            //
            // Note: I would prefer to pass skipped files through to Unity
            // for default drag-and-drop handling, but that does not seem
            // to be possible because `DragAndDrop.paths` is read-only.

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

            foreach (string path in DragAndDrop.paths)
            {
                // Don't trigger automatic glTF import when we are dragging
                // a .gltf/.glb/.zip file from within the Unity project folder.
                //
                // When the source file is inside the Unity project folder,
                // the Unity drag-and-drop machinery will report a relative path
                // starting with "Assets/".

                if (path.StartsWith("Assets/"))
                {
                    continue;
                }

                string _path = path.ToLower();

                if (_path.EndsWith(".gltf") || _path.EndsWith(".glb"))
                {
                    acceptedPaths.Add(path);
                }

                else if (_path.EndsWith(".zip") && ZipUtil.ContainsGltfFile(path))
                {
                    acceptedPaths.Add(path);
                }
            }

            if (acceptedPaths.Count > 0)
            {
                // Run GLTF import(s) in the background.

                StartImport(acceptedPaths, importDir);

                // Consume the `DragPerform` event, so that Unity's
                // default drag-and-drop handling, which
                // simply copies the file(s) into the target Assets
                // folder, is not performed.

                Event.current.Use();
            }
        }