Skip to content

Instantly share code, notes, and snippets.

@olegmrzv
Created November 1, 2020 21:14
Show Gist options
  • Save olegmrzv/a77490507fc61a86cc881e9199ba547b to your computer and use it in GitHub Desktop.
Save olegmrzv/a77490507fc61a86cc881e9199ba547b to your computer and use it in GitHub Desktop.
Fix packages manager to show DOTS and etc
//https://forum.unity.com/threads/package-manager-showing-hidden-packages-script.995506/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEditor.PackageManager.UI;
using UnityEngine;
using Object = UnityEngine.Object;
public static class DisablePreviewPackageWarning
{
[InitializeOnLoadMethod]
static void Init()
{
var prefsType = typeof(PackageManagerExtensions).Assembly.GetType("UnityEditor.PackageManager.UI.PackageManagerPrefs");
#if UNITY_2020_2_OR_NEWER
var serviceContainerType = typeof(PackageManagerExtensions).Assembly.GetType("UnityEditor.PackageManager.UI.ServicesContainer");
var serviceContainer = serviceContainerType.BaseType.GetProperty("instance", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static).GetValue(null);
var prefsInstance = serviceContainerType.GetMethod("Resolve").MakeGenericMethod(prefsType).Invoke(serviceContainer, null);
#else
var prefsInstance = prefsType.GetProperty("instance", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static).GetValue(null) as Object;
#endif
var property = prefsInstance.GetType().GetProperty("dismissPreviewPackagesInUse", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
property.SetValue(prefsInstance, true);
}
}
//https://forum.unity.com/threads/package-manager-showing-hidden-packages-script.995506/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEditor.PackageManager;
using UnityEditor.PackageManager.Requests;
using UnityEditor.PackageManager.UI;
using Object = UnityEngine.Object;
using PackageInfo = UnityEditor.PackageManager.PackageInfo;
public static class DiscoverExtraPackages
{
static string[] m_extraPackages = new string[]
{
"com.ptc.vuforia.engine",
"com.unity.2d.entities",
"com.unity.ai.planner",
"com.unity.aovrecorder",
"com.unity.assetbundlebrowser",
"com.unity.assetgraph",
"com.unity.barracuda",
"com.unity.barracuda.burst",
"com.unity.build-report-inspector",
"com.unity.cloud.userreporting",
"com.unity.collections",
"com.unity.connect.share",
"com.unity.dots.editor",
"com.unity.entities",
"com.unity.film-tv.toolbox",
"com.unity.google.resonance.audio",
"com.unity.immediate-window",
"com.unity.mathematics",
"com.unity.meshsync",
"com.unity.multiplayer-hlapi",
"com.unity.package-manager-doctools",
"com.unity.package-manager-ui",
"com.unity.package-validation-suite",
"com.unity.physics",
"com.unity.platforms",
"com.unity.platforms.android",
"com.unity.platforms.linux",
"com.unity.platforms.macos",
"com.unity.platforms.web",
"com.unity.platforms.windows",
"com.unity.playablegraph-visualizer",
"com.unity.render-pipelines.lightweight",
"com.unity.rendering.hybrid",
"com.unity.renderstreaming",
"com.unity.scene-template",
"com.unity.simulation.client",
"com.unity.simulation.core",
"com.unity.simulation.capture",
"com.unity.simulation.games",
"com.unity.standardevents",
"com.unity.streaming-image-sequence",
"com.unity.test-framework.performance",
"com.unity.tiny.all",
"com.unity.transport",
"com.unity.upm.develop",
"com.unity.vectorgraphics",
"com.unity.webrtc",
"com.unity.xr.googlevr.android",
"com.unity.xr.googlevr.ios",
"com.unity.xr.legacyinputhelpers",
"com.unity.xr.oculus.android",
"com.unity.xr.oculus.standalone",
"com.unity.xr.openvr.standalone",
"com.unity.xr.arsubsystems",
"com.unity.xr.interactionsubsystems",
"com.unity.xr.windowsmr.metro",
};
static readonly Type m_cacheType = typeof(PackageManagerExtensions).Assembly.GetType("UnityEditor.PackageManager.UI.UpmCache");
static readonly Type m_cacheInterfaceType = typeof(PackageManagerExtensions).Assembly.GetType("UnityEditor.PackageManager.UI.IUpmCache");
static readonly PropertyInfo m_getPackageInfos = m_cacheType.GetProperty("searchPackageInfos") ?? m_cacheInterfaceType.GetProperty("searchPackageInfos");
static readonly MethodInfo m_setPackageInfos = m_cacheType.GetMethod("SetSearchPackageInfos") ?? m_cacheInterfaceType.GetMethod("SetSearchPackageInfos");
static readonly PropertyInfo m_operationInProgress = typeof(PackageManagerExtensions).Assembly.GetType("UnityEditor.PackageManager.UI.IOperation").GetProperty("isInProgress");
static readonly Type serviceContainerType = typeof(PackageManagerExtensions).Assembly.GetType("UnityEditor.PackageManager.UI.ServicesContainer");
static readonly Func<object> m_serviceContainerGetter = (Func<object>)serviceContainerType?.BaseType.GetProperty("instance", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static).GetGetMethod().CreateDelegate(typeof(Func<object>));
static readonly Func<object> m_upmCacheGetter = CreateUpmCacheGetter();
static readonly EditorApplication.CallbackFunction m_callback = OnProgress;
static readonly List<SearchRequest> m_requests = new List<SearchRequest>(32);
static object m_searchAllOperation;
static Func<object> CreateUpmCacheGetter()
{
#if UNITY_2020_2_OR_NEWER
return (Func<object>)serviceContainerType.GetMethod("Resolve").MakeGenericMethod(m_cacheType).CreateDelegate(typeof(Func<object>), m_serviceContainerGetter());
#else
return (Func<object>)m_cacheType.GetProperty("instance").GetGetMethod().CreateDelegate(typeof(Func<object>));
#endif
}
[InitializeOnLoadMethod]
static void Init()
{
// Hook package manager SearchAll operation
var clientType = typeof(PackageManagerExtensions).Assembly.GetType("UnityEditor.PackageManager.UI.UpmClient");
#if UNITY_2020_2_OR_NEWER
var instance = serviceContainerType.GetMethod("Resolve").MakeGenericMethod(clientType).Invoke(m_serviceContainerGetter(), null);
#else
var instance = clientType.GetProperty("instance", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static).GetValue(null) as Object;
#endif
var evnt = instance.GetType().GetEvent("onSearchAllOperation");
evnt.AddEventHandler(instance, Delegate.CreateDelegate(evnt.EventHandlerType, new Action<object>(OnSearchAll).Method));
}
static void OnSearchAll(object operation)
{
m_searchAllOperation = operation;
// Clear requests, in case any searches are running, they will finish without doing anything
m_requests.Clear();
// Kick off searching
foreach (var id in m_extraPackages)
{
m_requests.Add(Client.Search(id));
}
// Re-register update callback
EditorApplication.update -= m_callback;
EditorApplication.update += m_callback;
}
private static void OnProgress()
{
// Wait for all requests to complete and for SearchAll opeartion to finish
if (m_requests.All(s => s.IsCompleted) && !(bool)m_operationInProgress.GetValue(m_searchAllOperation))
{
// Then add results
var cache = m_upmCacheGetter();
var packages = ((IEnumerable<PackageInfo>)m_getPackageInfos.GetValue(cache));
var everything = m_requests.Where(s => s.Status == StatusCode.Success).SelectMany(s => s.Result).Union(packages);
m_setPackageInfos.Invoke(cache, new object[] { everything });
EditorApplication.update -= m_callback;
m_requests.Clear();
m_searchAllOperation = null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment