Adding a Custom Package Using a Script to a Project

Here is a script with a link to a custom package but it doesn’t want to be added to the project. What is the problem ?

using UnityEditor;
using UnityEditor.PackageManager.Requests;
using UnityEditor.PackageManager;
using UnityEngine;

namespace Unity.Editor.Example
{
    [InitializeOnLoad]
    static class AddLibrary
    {
        static AddRequest Request;

        [MenuItem("Window/Add Library")]
        static void Add()
        {
            // Add a package to the project
            Request = Client.Add("file:\\C:\\Users\\111\\Desktop\\com.prorok.testpack");
            EditorApplication.update += Progress;
        }

        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;
            }
        }
    }
}

Hi @Proroke ,

The problem is likely the leading “\” after “file:”. The proper string would be "file:C:\\Users\\111\\Desktop\\com.prorok.testpack".

Thanks