Esempio n. 1
0
        private void SetupAddMenu()
        {
            var dropdownItem = addMenu.AddBuiltInDropdownItem();

            dropdownItem.text     = L10n.Tr("Add package from disk...");
            dropdownItem.userData = "AddFromDisk";
            dropdownItem.action   = () =>
            {
                var path = m_Application.OpenFilePanelWithFilters(L10n.Tr("Select package on disk"), "", new[] { "package.json file", "json" });
                if (string.IsNullOrEmpty(path))
                {
                    return;
                }

                try
                {
                    if (m_IOProxy.GetFileName(path) != "package.json")
                    {
                        Debug.Log(L10n.Tr("[Package Manager Window] Please select a valid package.json file in a package folder."));
                        return;
                    }


                    if (!m_PackageDatabase.isInstallOrUninstallInProgress)
                    {
                        m_PackageDatabase.InstallFromPath(m_IOProxy.GetParentDirectory(path), out var tempPackageId);
                        PackageManagerWindowAnalytics.SendEvent("addFromDisk");

                        var package = m_PackageDatabase.GetPackage(tempPackageId);
                        if (package != null)
                        {
                            m_PackageFiltering.currentFilterTab = PackageFilterTab.InProject;
                            m_PageManager.SetSelected(package);
                        }
                    }
                }
                catch (System.IO.IOException e)
                {
                    Debug.Log($"[Package Manager Window] Cannot add package from disk {path}: {e.Message}");
                }
            };

            dropdownItem          = addMenu.AddBuiltInDropdownItem();
            dropdownItem.text     = L10n.Tr("Add package from tarball...");
            dropdownItem.userData = "AddFromTarball";
            dropdownItem.action   = () =>
            {
                var path = m_Application.OpenFilePanelWithFilters(L10n.Tr("Select package on disk"), "", new[] { "Package tarball", "tgz, tar.gz" });
                if (!string.IsNullOrEmpty(path) && !m_PackageDatabase.isInstallOrUninstallInProgress)
                {
                    m_PackageDatabase.InstallFromPath(path, out var tempPackageId);
                    PackageManagerWindowAnalytics.SendEvent("addFromTarball");

                    var package = m_PackageDatabase.GetPackage(tempPackageId);
                    if (package != null)
                    {
                        m_PackageFiltering.currentFilterTab = PackageFilterTab.InProject;
                        m_PageManager.SetSelected(package);
                    }
                }
            };

            dropdownItem          = addMenu.AddBuiltInDropdownItem();
            dropdownItem.text     = L10n.Tr("Add package from git URL...");
            dropdownItem.userData = "AddFromGit";
            dropdownItem.action   = () =>
            {
                var args = new InputDropdownArgs
                {
                    title            = L10n.Tr("Add package from git URL"),
                    iconUssClass     = "git",
                    placeholderText  = L10n.Tr("URL"),
                    submitButtonText = L10n.Tr("Add"),
                    onInputSubmitted = url =>
                    {
                        if (!m_PackageDatabase.isInstallOrUninstallInProgress)
                        {
                            m_PackageDatabase.InstallFromUrl(url);
                            PackageManagerWindowAnalytics.SendEvent("addFromGitUrl", url);

                            var package = m_PackageDatabase.GetPackage(url);
                            if (package != null)
                            {
                                m_PackageFiltering.currentFilterTab = PackageFilterTab.InProject;
                                m_PageManager.SetSelected(package);
                            }
                        }
                    },
                    windowSize = new Vector2(resolvedStyle.width, 50)
                };
                addMenu.ShowInputDropdown(args);
            };

            dropdownItem          = addMenu.AddBuiltInDropdownItem();
            dropdownItem.text     = L10n.Tr("Add package by name...");
            dropdownItem.userData = "AddByName";
            dropdownItem.action   = () =>
            {
                // Same as above, the worldBound of the toolbar is used rather than the addMenu
                var rect     = GUIUtility.GUIToScreenRect(worldBound);
                var dropdown = new AddPackageByNameDropdown(m_ResourceLoader, m_PackageFiltering, m_UpmClient, m_PackageDatabase, m_PageManager, PackageManagerWindow.instance)
                {
                    position = rect
                };
                DropdownContainer.ShowDropdown(dropdown);
            };
        }