using System; using System.Collections.Generic; using System.IO; using UnityEditor; using UnityEditor.PackageManager.UI; using UnityEngine; using UnityEngine.UIElements; using PackageInfo = UnityEditor.PackageManager.PackageInfo; /// /// In the package.json, an array can be added after the path variable of the sample. The path should start from the Packages/ folder, as such: /// "samples": [ /// { /// "displayName": "Sample name", /// "description": "Sample description", /// "path": "Samples~/Stuff", /// "dependencies": /// [ /// "com.unity.render-pipelines.core/Samples~/CommonMeshes", /// "com.unity.render-pipelines.core/Samples~/CommonTextures", /// "com.unity.render-pipelines.universal/Samples~/CommonURPMaterials", /// "com.unity.render-pipelines.high-definition/Samples~/CommonHDRPMaterials", /// ] /// }, /// { /// [InitializeOnLoad] internal class SampleDependencyImporter : IPackageManagerExtension { internal static SampleDependencyImporter instance { get; private set; } static SampleDependencyImporter() { instance = new SampleDependencyImporter(); PackageManagerExtensions.RegisterExtension(instance); } bool importingTextMeshProEssentialResources = false; PackageInfo m_PackageInfo; SampleList m_SampleList; List m_Samples; VisualElement injectingElement; VisualElement _panelRoot; VisualElement panelRoot { get { _panelRoot ??= injectingElement?.panel?.visualTree; return _panelRoot; } } /// /// Use the extension UI to "inject" an invisible element in package manager UI /// that will serve as a base to hook up additional logic to the import buttons. /// VisualElement IPackageManagerExtension.CreateExtensionUI() { injectingElement = new VisualElement(); injectingElement.style.display = DisplayStyle.None; // This callback is called once the element is added to the UI, at this point we should have access to rest of the elements. injectingElement.RegisterCallback((callback) => { //Force clear the cached elements to fetch those from the newly openned window _panelRoot = null; samplesButton = null; RefreshSampleButtons(); }); return injectingElement; } Button samplesButton; const string samplesButtonName = "samplesButton"; const string sampleContainerClassName = "sampleContainer"; const string importButtonClassName = "importButton"; const string injectedButtonClassName = "importWithDependenciesButton"; void RefreshSampleButtons() { if (injectingElement == null || m_PackageInfo == null || m_SampleList == null || panelRoot == null) return; // Call refresh of samples and button injection when switching to the "Samples" tab. if (samplesButton == null) { samplesButton = panelRoot.Q