Always thankful for your support, Lotte💖
Last active
July 23, 2024 03:25
-
-
Save LotteMakesStuff/2247e8a7743a1ceb52331f8e2f5531f7 to your computer and use it in GitHub Desktop.
As of v1.9.3, Unity has added a simple extention API to the PackageManager UI. This small example shows you how to implment it and add a new window to the package manager. Note: you will need to change to the Staging package repository as time of writing. This code needs to go in an Editor folder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
using UnityEditor; | |
using UnityEditor.PackageManager.UI; | |
using UnityEngine; | |
using UnityEngine.Experimental.UIElements; | |
using UnityEngine.Experimental.UIElements.StyleEnums; | |
using PackageInfo = UnityEditor.PackageManager.PackageInfo; | |
[InitializeOnLoad] | |
public class PackageManagerExtentionExample : IPackageManagerExtension | |
{ | |
// This class implments the new IPackageManagerExtension interface, allowing us to plug it | |
// into the package manager window. this feature is still very much under development | |
static PackageManagerExtentionExample() | |
{ | |
Debug.Log("CALLED STATIC CTOR"); | |
// This static constructor gets called becuase we have the InitializeOnLoad attribute | |
// on our class. Here we register this PackageManager extention so our exta version list | |
// shows up in the manager window | |
PackageManagerExtensions.RegisterExtension(new PackageManagerExtentionExample()); | |
} | |
// this is the scrollview were going to put the version list into | |
ScrollView tasksContainer; | |
public VisualElement CreateExtensionUI() | |
{ | |
Debug.Log("CALLED CreateExtensionUI"); | |
VisualContainer root = new VisualContainer() | |
{ | |
style = | |
{ | |
backgroundColor = Color.grey, | |
alignSelf = Align.FlexStart, | |
flexDirection = FlexDirection.Row | |
} | |
}; | |
tasksContainer = new ScrollView() | |
{ | |
style = | |
{ | |
width = 400, | |
height = 100, | |
flexDirection = FlexDirection.Column, | |
} | |
}; | |
tasksContainer.showHorizontal = false; | |
root.Add(tasksContainer); | |
return root; | |
} | |
public void OnPackageSelectionChange(PackageInfo packageInfo) | |
{ | |
// setup the version list for the currently selected package | |
Debug.Log("CALLED OnPackageSelectionChange"); | |
tasksContainer.contentContainer.Clear(); | |
foreach (var version in packageInfo.versions.all.Reverse()) | |
{ | |
tasksContainer.contentContainer.Add(NewVersion(version, packageInfo)); | |
} | |
} | |
private VisualElement NewVersion(string version, PackageInfo packageInfo) | |
{ | |
// create a list item for the passed in version | |
var versionElement = new VisualContainer() | |
{ | |
style = | |
{ | |
width = 400, | |
flexDirection = FlexDirection.Row | |
} | |
}; | |
versionElement.name = version; | |
// fist, lets put in the version name | |
versionElement.Add(new Label(version)); | |
// annnd now a bunch of interesting info | |
versionElement.Add(new Toggle(null){text = "Recomended", on = (packageInfo.versions.recommended == version)}); | |
versionElement.Add(new Toggle(null){text = "Compatible", on = (packageInfo.versions.compatible.Contains(version))}); | |
versionElement.Add(new Toggle(null){text = "Latest Compatible", on = (packageInfo.versions.latestCompatible == version)}); | |
Debug.Log("ADDED " + version); | |
return versionElement; | |
} | |
public void OnPackageAddedOrUpdated(PackageInfo packageInfo) | |
{ | |
Debug.Log("CALLED OnPackageAddedOrUpdated"); | |
} | |
public void OnPackageRemoved(PackageInfo packageInfo) | |
{ | |
Debug.Log("CALLED OnPackageRemoved"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Lotte - your gist page is a goldmine! Thanks so much for doing this!! Coffee bought!