@@ -80,7 +80,7 @@ VisualElement IPackageManagerExtension.CreateExtensionUI()
8080 const string importButtonClassName = "actionButton" ;
8181 const string injectedButtonClassName = "importWithDependenciesButton" ;
8282
83- void RefreshSampleButtons ( )
83+ internal void RefreshSampleButtons ( )
8484 {
8585 if ( injectingElement == null || m_PackageInfo == null || m_SampleList == null || panelRoot == null )
8686 return ;
@@ -111,14 +111,38 @@ void RefreshSampleButtons()
111111 var sampleContainer = sampleContainers [ i ] ;
112112 var injectedButton = sampleContainer . Q < Button > ( className : injectedButtonClassName ) ;
113113
114- if ( injectedButton == null )
114+ // Get the original import button.
115+ Button importButton = null ;
116+ foreach ( var button in sampleContainer . Query < Button > ( className : importButtonClassName ) . ToList ( ) )
115117 {
116- // Get and hide the original import button.
117- // WARNING! There are now two buttons - "Import" and "Locate". The Import button is first in the hierarchy
118- // so this happens to work, but it's really brittle so we need to find a better way to do this.
119- var importButton = sampleContainer . Q < Button > ( className : importButtonClassName ) ;
120- importButton . style . display = DisplayStyle . None ;
118+ if ( button . text == "Import" || button . text == "Reimport" )
119+ {
120+ var classes = button . GetClasses ( ) ;
121+ bool skip = false ;
122+ foreach ( var c in classes )
123+ {
124+ if ( c == injectedButtonClassName )
125+ {
126+ skip = true ;
127+ break ;
128+ }
129+ }
130+ if ( skip )
131+ continue ;
121132
133+ importButton = button ;
134+ break ;
135+ }
136+ }
137+
138+ if ( importButton == null )
139+ continue ;
140+
141+ // Hide the original import button.
142+ importButton . style . display = DisplayStyle . None ;
143+
144+ if ( injectedButton == null )
145+ {
122146 // Create a new button copying the original one with our additional class.
123147 injectedButton = new Button ( ) ;
124148 foreach ( var c in importButton . GetClasses ( ) )
@@ -132,7 +156,8 @@ void RefreshSampleButtons()
132156 // Need to copy i for the lambda.
133157 var index = i ;
134158 // On click of the imported button, import the dependencies first then call the original button logic.
135- injectedButton . clicked += ( ) => {
159+ injectedButton . clicked += ( ) =>
160+ {
136161 ImportSampleDependencies ( index ) ;
137162
138163 using ( var ev = NavigationSubmitEvent . GetPooled ( ) )
@@ -142,7 +167,11 @@ void RefreshSampleButtons()
142167 }
143168 } ;
144169 }
145- } ;
170+ else // We may need to update the button text after the sample import here.
171+ {
172+ injectedButton . text = importButton . text ;
173+ }
174+ }
146175 }
147176
148177 public void OnPackageAddedOrUpdated ( PackageInfo packageInfo ) { }
@@ -336,3 +365,11 @@ static void CopyDirectory(string sourcePath, string targetPath)
336365 }
337366}
338367
368+ internal class SampleDependencyImporterPostProcessor : AssetPostprocessor
369+ {
370+ private static void OnPostprocessAllAssets ( string [ ] importedAssets , string [ ] deletedAssets , string [ ] movedAssets , string [ ] movedFromAssetPaths )
371+ {
372+ SampleDependencyImporter . instance ? . RefreshSampleButtons ( ) ;
373+ }
374+ }
375+
0 commit comments