Skip to content

Instantly share code, notes, and snippets.

@h1ddengames
Last active November 16, 2023 11:40
Show Gist options
  • Save h1ddengames/d5793fc49404887c257438655f1fcffc to your computer and use it in GitHub Desktop.
Save h1ddengames/d5793fc49404887c257438655f1fcffc to your computer and use it in GitHub Desktop.
Import Assets and Packages in Unity all at once.
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEditor;
using UnityEditor.PackageManager.Requests;
using UnityEditor.PackageManager;
// Place this script in YOUR_PROJECT_FOLDER/Assets/Editor
// Run this script by clicking on the Importer menu item below the Unity title bar (File, Edit ... Importer)
public class ImportAssets : EditorWindow {
static string unityPackagesPath = "C:/Users/user/Downloads/UnityAssets";
static AddRequest Request;
static int index = 0;
// Add the packages you want to install here.
// Package versions and names can be obtained by opening the package manager
// then clicking on the package you are interested in and clicking on "view changelog"
// A browser window will open with a URL similar to the following:
// https://docs.unity3d.com/Packages/com.unity.2d.animation@5.0/changelog/CHANGELOG.html
// Obtain the name and the version from the string between Packages/ ... /changelog
// Omitting the version will install the latest version available.
// Specific versions can be installed by adding the version after the package name:
// com.unity.visualeffectgraph@10.2
// package.name@version
static List<string> packages = new List<string> {
"com.unity.textmeshpro",
"com.unity.shadergraph",
"com.unity.visualeffectgraph",
"com.unity.terrain-tools",
"com.unity.polybrush",
"com.unity.postprocessing",
"com.unity.cinemachine"
};
[MenuItem("Importers/Asset Importer")]
public static void ImportAllAssets() {
string[] unityPackages = Directory.GetFiles(unityPackagesPath);
for(int i = 0; i < unityPackages.Length; i++) {
if(unityPackages[i].EndsWith(".unitypackage")) {
AssetDatabase.ImportPackage(unityPackages[i], false);
}
}
Debug.Log("Assets have been imported");
}
// Example of a for loop through methods rather than the for loop built into the language.
[MenuItem("Importers/Smart Package Importer")]
public static void SmartImportAllPackages() {
if(index >= packages.Count) { return; }
Request = Client.Add(packages[index]);
EditorApplication.update += Progress;
index++;
}
static void Progress() {
if(Request.IsCompleted) {
if(Request.Status == StatusCode.Success)
Debug.Log("Installed: " + Request.Result.packageId);
else if(Request.Status >= StatusCode.Failure)
Debug.Log(Request.Error.message);
EditorApplication.update -= Progress;
if(index >= packages.Count) {
Complete();
} else {
SmartImportAllPackages();
}
}
}
static void Complete() {
Debug.Log("Completed importing all packages.");
Request = null;
index = 0;
}
[MenuItem("Importers/Dumb Package Importer")]
public static void DumbImportAllPackages() {
// NOTE: This will freeze the editor. Please wait until the process has completed.
while(index != packages.Count) {
Request = Client.Add(packages[index]);
while(!Request.IsCompleted) {
System.Threading.Thread.Sleep(2000);
}
if(Request.Status == StatusCode.Success)
Debug.Log("Installed: " + Request.Result.packageId);
else if(Request.Status >= StatusCode.Failure)
Debug.Log(Request.Error.message);
index++;
}
Complete();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment