Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question Need help scripting a unityPackage Importer through a menuitem.

Discussion in 'Scripting' started by lmaoroot2, May 6, 2021.

  1. lmaoroot2

    lmaoroot2

    Joined:
    May 6, 2021
    Posts:
    17
    Hey guys, its my first time in Unity Forums.

    I was wondering if you guys can give me some help on writing this script. It's just a couple lines, I'm sure. I'm trying to download a unitypackage off of the web and import it into the project. I'd very much appreciate the help. Thanks in advance.

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using System.Net;
    3. using System.IO;
    4. using UnityEngine;
    5. using UnityEditor;
    6. public static class Importer
    7. {
    8.     [MenuItem("Importer/Import Package 1", false, 12)]
    9.     private static void importPackage1(){
    10.         string package = "https://somewebsite.com/pkg.unitypackage"; // I wanted to leave out specifics, so this is just a sample link.
    11.         AssetDatabase.ImportPackage(package, false);
    12.     }
    13. }
    This gives me errors like "Failed to import package: Couldn't decompress package". Although, I don't even believe it downloaded anything.

    I also have tried using the DownloadFile() function or method inside through a WebClient, but I got UnauthorizedAccessExceptions.

    Any help would be appreciated.
    Thanks again.
     
    Last edited: May 6, 2021
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,920
    This sounds like you don't have the rights to download that file. Are you able to download the file from your browser?

    The
    AssetDatabase.ImportPackage
    alone is expecting a local path, it's not capable of downloading from the internet, so you need to do that.
     
  3. lmaoroot2

    lmaoroot2

    Joined:
    May 6, 2021
    Posts:
    17
    Ya, I have access to the unitypackage in the webbrowser. I can download it through the website and everything, but I cant figure out how to get it to download from the web and import it. I was trying to download it to the assets folder using Application.dataPath but it wasn't letting me access that path for some reason.
     
  4. lmaoroot2

    lmaoroot2

    Joined:
    May 6, 2021
    Posts:
    17
    but ya, idk what could be going wrong.