11using System ;
2- using System . Collections ;
32using System . Collections . Generic ;
43using System . IO ;
4+ using System . Linq ;
55using System . Reflection ;
6- using System . Xml ;
76using UnityEditor ;
7+ using UnityEditor . PackageManager ;
8+ using UnityEditor . PackageManager . Requests ;
89using UnityEditor . SceneManagement ;
910using UnityEngine ;
1011
@@ -17,7 +18,7 @@ public class InitializeProject : EditorWindow
1718 // settings
1819 static string [ ] folders = new string [ ] { "Fonts" , "Materials" , "Models" , "Prefabs" , "Scenes" , "Scripts" , "Shaders" , "Sounds" , "Textures" } ;
1920
20- static Dictionary < string , string > addPackages = new Dictionary < string , string > ( ) { { "com.unity.ide.visualstudio" , "2.0.22" } } ;
21+ static Dictionary < string , string > addPackages = new Dictionary < string , string > ( ) { { "com.unity.ide.visualstudio" , "2.0.23" } , { "com.unity.ugui" , null } } ;
2122 static string [ ] blackListedPackages = new string [ ] { "com.unity.modules.unityanalytics" , "com.unity.modules.director" , "com.unity.collab-proxy" , "com.unity.ide.rider" , "com.unity.ide.vscode" , "com.unity.test-framework" , "com.unity.timeline" , "com.unity.visualscripting" } ;
2223
2324 static InitializeProject window ;
@@ -31,6 +32,11 @@ public class InitializeProject : EditorWindow
3132 static List < string > items ;
3233 static List < bool > checkedStates ;
3334
35+ static SearchRequest currentSearch ;
36+ static List < string > packagesToResolve = new List < string > ( ) ;
37+ static int currentPackageIndex = 0 ;
38+ static bool isSearching = false ;
39+
3440 [ MenuItem ( "Tools/UnityLibrary/Initialize Project" ) ]
3541 public static void InitManually ( )
3642 {
@@ -45,6 +51,12 @@ public static void Init()
4551 window = ( InitializeProject ) EditorWindow . GetWindow ( typeof ( InitializeProject ) ) ;
4652 window . titleContent = new GUIContent ( "Initialize Project" ) ;
4753 window . minSize = new Vector2 ( 450 , 550 ) ;
54+
55+ // fetch latest package numbers> only scan those with null version
56+ packagesToResolve = addPackages . Where ( kvp => kvp . Value == null ) . Select ( kvp => kvp . Key ) . ToList ( ) ;
57+ currentPackageIndex = 0 ;
58+ StartNextPackageVersionSearch ( ) ;
59+
4860 LoadSettings ( ) ;
4961 window . Show ( ) ;
5062 }
@@ -380,6 +392,7 @@ static void UpdatePackages()
380392 {
381393 jsonConvertType = Assembly . Load ( "Newtonsoft.Json" ) . GetType ( "Newtonsoft.Json.JsonConvert" ) ;
382394 }
395+
383396 IJsonSerializer jsonSerializer ;
384397 if ( jsonConvertType != null )
385398 {
@@ -397,6 +410,7 @@ static void UpdatePackages()
397410 //Debug.Log("We have Newtonsoft.Json");
398411 var fromJson = jsonSerializer . Deserialize < DependenciesManifest > ( json ) ;
399412
413+ // remove blacklisted packages
400414 for ( int i = fromJson . dependencies . Count ; i > - 1 ; i -- )
401415 {
402416 for ( int k = 0 ; k < blackListedPackages . Length ; k ++ )
@@ -412,6 +426,13 @@ static void UpdatePackages()
412426 // add wanted packages, if missing
413427 foreach ( KeyValuePair < string , string > item in addPackages )
414428 {
429+ // skip packages with null or empty version
430+ if ( string . IsNullOrEmpty ( item . Value ) )
431+ {
432+ Debug . LogWarning ( "Skipped adding '" + item . Key + "' because version is null or empty." ) ;
433+ continue ;
434+ }
435+
415436 if ( fromJson . dependencies . ContainsKey ( item . Key ) == false )
416437 {
417438 fromJson . dependencies . Add ( item . Key , item . Value ) ;
@@ -430,19 +451,70 @@ static void UpdatePackages()
430451
431452 // TODO add pretty print
432453 var toJson = jsonSerializer . Serialize ( fromJson ) ;
454+
433455 // FIXME temporary pretty print, by adding new lines and tabs
434456 toJson = toJson . Replace ( "," , ",\n " ) ;
435457 toJson = toJson . Replace ( "{" , "{\n " ) ;
436458 toJson = toJson . Replace ( "}" , "\n }" ) ;
437459 toJson = toJson . Replace ( "\" dependencies" , "\t \" dependencies" ) ;
438460 toJson = toJson . Replace ( "\" com." , "\t \t \" com." ) ;
439461 //Debug.Log(toJson);
462+
440463 File . WriteAllText ( packagesPath , toJson ) ;
441464 }
442465 else
443466 {
444467 Debug . Log ( "Newtonsoft.Json is not available, cannot remove packages.." ) ;
445468 }
469+ } // UpdatePackages()
470+
471+ static void StartNextPackageVersionSearch ( )
472+ {
473+ if ( currentPackageIndex >= packagesToResolve . Count )
474+ {
475+ Debug . Log ( "Finished resolving all null-version packages." ) ;
476+ return ;
477+ }
478+
479+ var packageName = packagesToResolve [ currentPackageIndex ] ;
480+ Debug . Log ( $ "Searching for latest version of { packageName } ...") ;
481+ currentSearch = Client . Search ( packageName ) ;
482+ EditorApplication . update += PackageVersionFetchProgress ;
483+ isSearching = true ;
484+ }
485+
486+ static void PackageVersionFetchProgress ( )
487+ {
488+ if ( ! isSearching || currentSearch == null || ! currentSearch . IsCompleted ) return ;
489+
490+ var packageName = packagesToResolve [ currentPackageIndex ] ;
491+
492+ if ( currentSearch . Status == StatusCode . Success )
493+ {
494+ var latestVersion = currentSearch . Result
495+ . OrderByDescending ( p => p . version ) // Might not always be semver-safe
496+ . FirstOrDefault ( ) ? . version ;
497+
498+ if ( latestVersion != null )
499+ {
500+ Debug . Log ( $ "Resolved { packageName } to version { latestVersion } ") ;
501+ addPackages [ packageName ] = latestVersion ;
502+ }
503+ else
504+ {
505+ Debug . LogWarning ( $ "No version found for { packageName } ") ;
506+ }
507+ }
508+ else
509+ {
510+ Debug . LogError ( $ "Failed to resolve { packageName } : { currentSearch . Error . message } ") ;
511+ }
512+
513+ // Move to next
514+ isSearching = false ;
515+ EditorApplication . update -= PackageVersionFetchProgress ;
516+ currentPackageIndex ++ ;
517+ StartNextPackageVersionSearch ( ) ;
446518 }
447519
448520 // toggle with clickable label text
0 commit comments